[extension_name]
Web Request
[insert_external]
Web request V3 is installed.
JSONPath 0.8.0 - XPath for JSON
Copyright (c) 2007 Stefan Goessner (goessner.net)
Licensed under the MIT (MIT-LICENSE.txt) licence
[insert_command]
lioranboardclient.send('{"type":"MESSAGE","topic":"ExtensionCommand","name":"Web Request","boxcount":6,"boxname1":"variable","boxtype1":"string","boxname2":"save_in_variable|yes|no|","boxtype2":"string","boxname3":"method|GET|POST|PUT|DELETE|","boxtype3":"string","boxname4":"proxy|yes|no|","boxtype4":"string","boxname5":"url","boxtype5":"string", "boxname6":"expression","boxtype6":"string"}');
[insert_hook]
case "WebRequest":{
LBWebRequest(LioranBoardJSON.variable,LioranBoardJSON.url,LioranBoardJSON.save_in_variable,LioranBoardJSON.method,LioranBoardJSON.proxy,LioranBoardJSON.expression)
}break
[insert_script]
async function LBWebRequest(variable, url, save, method, proxy, expression) {
const headers = {
Accept: 'application/json',
};
async function fetchURL() {
if (proxy === 'yes') {
url = `https://cors.bridged.cc/${url}`;
headers['x-cors-grida-api-key'] = 'cbdfbbe4-f84f-4ed7-a748-200afe041b5e';
}
const response = await fetch(url, {
method,
headers,
});
let content = await response.text();
if (!response.ok) {
try {
content = JSON.parse(content);
} catch (e) {}
const error = content.message || content.error || 'Something went wrong.';
throw new Error(error);
} else {
try {
content = JSON.parse(content);
} catch (e) {}
}
return content;
}
await fetchURL()
.then((data) => {
lioranboardclient.send('{"type":"MESSAGE","topic":"AlertMessage","message":"Web request successful!"}');
if (save === 'yes') {
if (expression) {
const pathData = jsonPath(data, expression);
if (Array.isArray(pathData)) {
if (pathData.length === 1) {
lioranboardclient.send(`{"type":"MESSAGE","topic":"SetValue","valuename":"${variable}","value":"${pathData[0]}","real":false}`);
} else {
lioranboardclient.send(`{"type":"MESSAGE","topic":"DeleteStack","name":"${variable}"}`);
for (let index = 0; index < pathData.length; index++) {
lioranboardclient.send(`{"type":"MESSAGE","topic":"AddStack","name":"${variable}","position":"top","value":"${pathData[index]}"}`);
}
}
} else {
lioranboardclient.send(`{"type":"MESSAGE","topic":"SetValue","valuename":"${variable}","value":"0","real":false}`);
}
} else {
lioranboardclient.send(`{"type":"MESSAGE","topic":"SetValue","valuename":"${variable}","value":"${ReplaceSymbols(data)}","real":false}`);
}
}
})
.catch((e) => {
if (!e || e.message == 'Failed to fetch') e = 'A network error occured. Try enabling proxy.';
lioranboardclient.send(`{"type":"MESSAGE","topic":"AlertMessage","message":"Web request failed! ${ReplaceSymbols(e)}"}`);
});
function ReplaceSymbols(tw) {
tw = JSON.stringify(tw);
tw = tw.replace(/(?:\r\n|\r|\n)/g, ' ').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
tw = tw.replace(/"/g, "'").replace(/\//g, '/').replace(/\\/g, '\\\\');
return tw;
}
function jsonPath(obj, expr, arg) {
var P = {
resultType: arg && arg.resultType || 'VALUE',
result: [],
normalize(expr) {
const subx = [];
return expr.replace(/[\['](\??\(.*?\))[\]']/g, ($0, $1) => `[#${subx.push($1) - 1}]`)
.replace(/'?\.'?|\['?/g, ';')
.replace(/;;;|;;/g, ';..;')
.replace(/;$|'?\]|'$/g, '')
.replace(/#([0-9]+)/g, ($0, $1) => subx[$1]);
},
asPath(path) {
const x = path.split(';'); let
p = '$';
for (let i = 1, n = x.length; i < n; i++) p += /^[0-9*]+$/.test(x[i]) ? (`[${x[i]}]`) : (`['${x[i]}']`);
return p;
},
store(p, v) {
if (p) P.result[P.result.length] = P.resultType == 'PATH' ? P.asPath(p) : v;
return !!p;
},
trace(expr, val, path) {
if (expr) {
let x = expr.split(';'); const
loc = x.shift();
x = x.join(';');
if (val && val.hasOwnProperty(loc)) P.trace(x, val[loc], `${path};${loc}`);
else if (loc === '*') P.walk(loc, x, val, path, (m, l, x, v, p) => { P.trace(`${m};${x}`, v, p); });
else if (loc === '..') {
P.trace(x, val, path);
P.walk(loc, x, val, path, (m, l, x, v, p) => { typeof v[m] === 'object' && P.trace(`..;${x}`, v[m], `${p};${m}`); });
} else if (/,/.test(loc)) { // [name1,name2,...]
for (let s = loc.split(/'?,'?/), i = 0, n = s.length; i < n; i++) P.trace(`${s[i]};${x}`, val, path);
} else if (/^\(.*?\)$/.test(loc)) // [(expr)]
{ P.trace(`${P.eval(loc, val, path.substr(path.lastIndexOf(';') + 1))};${x}`, val, path); } else if (/^\?\(.*?\)$/.test(loc)) // [?(expr)]
{ P.walk(loc, x, val, path, (m, l, x, v, p) => { if (P.eval(l.replace(/^\?\((.*?)\)$/, '$1'), v[m], m)) P.trace(`${m};${x}`, v, p); }); } else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) // [start:end:step] phyton slice syntax
{ P.slice(loc, x, val, path); }
} else P.store(path, val);
},
walk(loc, expr, val, path, f) {
if (val instanceof Array) {
for (let i = 0, n = val.length; i < n; i++) if (i in val) f(i, loc, expr, val, path);
} else if (typeof val === 'object') {
for (const m in val) if (val.hasOwnProperty(m)) f(m, loc, expr, val, path);
}
},
slice(loc, expr, val, path) {
if (val instanceof Array) {
const len = val.length; let start = 0; let end = len; let
step = 1;
loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, ($0, $1, $2, $3) => { start = parseInt($1 || start); end = parseInt($2 || end); step = parseInt($3 || step); });
start = (start < 0) ? Math.max(0, start + len) : Math.min(len, start);
end = (end < 0) ? Math.max(0, end + len) : Math.min(len, end);
for (let i = start; i < end; i += step) P.trace(`${i};${expr}`, val, path);
}
},
eval(x, _v, _vname) {
try { return $ && _v && eval(x.replace(/@/g, '_v')); } catch (e) { throw new SyntaxError(`jsonPath: ${e.message}: ${x.replace(/@/g, '_v').replace(/\^/g, '_a')}`); }
},
};
var $ = obj;
if (expr && obj && (P.resultType == 'VALUE' || P.resultType == 'PATH')) {
P.trace(P.normalize(expr).replace(/^\$;/, ''), obj, '$');
return P.result.length ? P.result : false;
}
}
}
[insert_over]
[buttons_0x0]
activepress="0.000000"
border_size="0.000000"
combine="0.000000"
type="websocketaction+"
picture=""
text="web request"
color="12632256.000000"
button_id="4445.000000"
websocketdelay6="2000.000000"
websocketvalue8_6=""
websocketvalue7_6=""
websocketvalue6_6=""
websocketvalue5_6=""
websocketvalue4_6=""
websocketvalue3_6=""
websocketvalue2_6=""
websocketvalue1_6="/$latest_news$/"
websocketaction6="36.000000"
websocketdelay5="0.000000"
websocketvalue8_5=""
websocketvalue7_5=""
websocketvalue6_5="https://decapi.me/twitter/latest/nytimes"
websocketvalue5_5="no"
websocketvalue4_5="GET"
websocketvalue3_5="yes"
websocketvalue2_5="latest_news"
websocketvalue1_5="Web Request"
websocketaction5="71.000000"
websocketdelay4="0.000000"
websocketvalue8_4=""
websocketvalue7_4=""
websocketvalue6_4=""
websocketvalue5_4=""
websocketvalue4_4=""
websocketvalue3_4=""
websocketvalue2_4=""
websocketvalue1_4="The extension doesn't support cutom headers or passing parameters (except for query string)"
websocketaction4="92.000000"
websocketdelay3="0.000000"
websocketvalue8_3=""
websocketvalue7_3=""
websocketvalue6_3=""
websocketvalue5_3=""
websocketvalue4_3=""
websocketvalue3_3=""
websocketvalue2_3=""
websocketvalue1_3="you can check for CORS errors by opening Transmitter in your browser and pressing F12 to see the log"
websocketaction3="92.000000"
websocketdelay2="0.000000"
websocketvalue8_2=""
websocketvalue7_2=""
websocketvalue6_2=""
websocketvalue5_2=""
websocketvalue4_2=""
websocketvalue3_2=""
websocketvalue2_2=""
websocketvalue1_2="Use proxy if your requests are failing due to CORS errors"
websocketaction2="92.000000"
websocketdelay1="0.000000"
websocketvalue8_1=""
websocketvalue7_1=""
websocketvalue6_1=""
websocketvalue5_1=""
websocketvalue4_1=""
websocketvalue3_1=""
websocketvalue2_1=""
websocketvalue1_1="Decide whether you want to save the response in a variable or not "
websocketaction1="92.000000"
websocketdelay0="0.000000"
websocketvalue8_0=""
websocketvalue7_0=""
websocketvalue6_0=""
websocketvalue5_0=""
websocketvalue4_0=""
websocketvalue3_0=""
websocketvalue2_0=""
websocketvalue1_0="Make a simple web request. Choose a method (GET is most likely what you want) "
websocketaction0="92.000000"
[configs]
name="Web Request Example"
background_color="8947848.000000"
grid_height="2.000000"
grid_width="2.000000"