Skip to content

Commit

Permalink
Use fetch and eval to implement requires
Browse files Browse the repository at this point in the history
import() only works for side-effects-only scripts
  • Loading branch information
JingMatrix committed Jul 18, 2023
1 parent f9b9f39 commit 960dffe
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/src/main/assets/local_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,22 @@ function runScript(meta) {
meta.sync_code = meta.code;
meta.code = async () => {
for (const url of meta.requires) {
let script;
try {
await import(url);
script = await (await fetch(url)).text();
} catch {
let script = await new Promise((resolve, reject) => {
script = await new Promise((resolve, reject) => {
GM_xmlhttpRequest({
url,
onload: (res) => resolve(res.responseText),
onerror: (e) => reject(e),
ontimeout: (e) => reject(e),
});
});
}
try {
new Function(script)();
} catch {
const uuid = Math.random();
const detail = JSON.stringify({
detail: { uuid, id: GM_info.script.id },
Expand Down

0 comments on commit 960dffe

Please sign in to comment.