Spaces:
Build error
Build error
File size: 9,253 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
const formatMessage = require('format-message');
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const AHHHHHHHHHHHHHH = require('../../util/array buffer');
const BufferStuff = new AHHHHHHHHHHHHHH();
// const Cast = require('../../util/cast');
/**
* Class for Website Request blocks
* @constructor
*/
class JgWebsiteRequestBlocks {
constructor (runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo () {
return {
id: 'jgWebsiteRequests',
name: 'Website Requests',
color1: '#004299',
color2: '#003478',
blocks: [
{
opcode: 'encodeTextForURL',
text: formatMessage({
id: 'jgWebsiteRequests.blocks.encodeTextForURL',
default: 'encode [TEXT] for URL',
description: 'Encodes text to be usable in a URL.'
}),
disableMonitor: true,
blockType: BlockType.REPORTER,
arguments: {
TEXT: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'jgWebsiteRequests.text_encode_for_url',
default: 'Text here',
description: 'The text to encode.'
})
}
}
},
{
opcode: 'decodeUrlForText',
text: formatMessage({
id: 'jgWebsiteRequests.blocks.decodeUrlForText',
default: 'decode [TEXT] for text',
description: 'Decodes text used in query parameters and other areas.'
}),
disableMonitor: true,
blockType: BlockType.REPORTER,
arguments: {
TEXT: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'jgWebsiteRequests.text_decode_for_url',
default: 'Text%20here',
description: 'The text to decode.'
})
}
}
},
{
opcode: 'getWebsiteContent',
text: formatMessage({
id: 'jgWebsiteRequests.blocks.getWebsiteContent',
default: 'get [WEBSITE]\'s content',
description: 'Gets the contents of the specified website. Includes HTML if it\'s a normal website.'
}),
disableMonitor: true,
blockType: BlockType.REPORTER,
arguments: {
WEBSITE: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'jgWebsiteRequests.website_fetch_content',
default: 'https://www.google.com',
description: 'The website to get the content of.'
})
}
}
},
{
opcode: 'getWebsiteBinaryData',
text: formatMessage({
id: 'jgWebsiteRequests.blocks.getWebsiteBinaryData',
default: 'get binary data from [WEBSITE]',
description: 'Gets the data of the specified website.'
}),
disableMonitor: true,
blockType: BlockType.REPORTER,
arguments: {
WEBSITE: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'jgWebsiteRequests.website_fetch_content',
default: 'https://www.google.com',
description: 'The website to get the content of.'
})
}
}
},
{
opcode: 'postWithContentToWebsite',
text: formatMessage({
id: 'jgWebsiteRequests.blocks.postWithContentToWebsite',
default: 'post [CONTENT] as [KEY] to [WEBSITE]',
description: 'Posts to a website using a JSON body with the key text set to the content.'
}),
disableMonitor: true,
blockType: BlockType.REPORTER,
arguments: {
CONTENT: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'jgWebsiteRequests.website_post_content',
default: 'value',
description: 'The content of the key to post.'
})
},
KEY: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'jgWebsiteRequests.website_post_key',
default: 'key',
description: 'The key in the request body to post.'
})
},
WEBSITE: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'jgWebsiteRequests.website_post_website',
default: 'https://httpbin.org/post',
description: 'The website to post the key and content to.'
})
}
}
}
]
};
}
encodeTextForURL (args) {
return encodeURIComponent(String(args.TEXT));
}
decodeUrlForText (args) {
return decodeURI(String(args.TEXT));
}
getWebsiteContent (args) {
return new Promise(resolve => {
if (window && !window.fetch) return resolve("");
const fetchingUrl = args.WEBSITE.replace("rawRequest()", "");
fetch(fetchingUrl, {cache: "no-cache"}).then(r => {
r.text().then(text => {
resolve(String(text));
})
.catch(() => {
resolve("");
});
})
.catch(() => {
resolve("");
});
});
}
getWebsiteBinaryData (args) {
return new Promise(resolve => {
if (window && !window.fetch) return resolve("[]");
const fetchingUrl = args.WEBSITE.replace("rawRequest()", "");
fetch(fetchingUrl, {cache: "no-cache"}).then(r => {
r.arrayBuffer().then(buffer => {
resolve(String(JSON.stringify(BufferStuff.bufferToArray(buffer))));
})
.catch(() => {
resolve("[]");
});
})
.catch(() => {
resolve("[]");
});
});
}
postWithContentToWebsite (args) {
return new Promise(resolve => {
if (window && !window.fetch) return resolve("");
const body = {};
const checking = String(args.CONTENT);
let canJSONParse = true;
try {
JSON.parse(checking);
} catch {
canJSONParse = false;
}
body[String(args.KEY)] = checking === "true" ? true :
checking === "false" ? false :
Number(checking) ? Number(checking) :
checking === "null" ? null :
canJSONParse ? JSON.parse(checking) :
checking;
fetch(args.WEBSITE, {
method: "POST",
headers: { "Content-Type": "application/json" },
cache: "no-cache",
body: JSON.stringify(body)
}).then(r => {
r.text().then(text => {
resolve(String(text));
})
.catch(() => {
resolve("");
});
})
.catch(() => {
resolve("");
});
});
}
}
module.exports = JgWebsiteRequestBlocks;
|