|
| 1 | +// https://stackoverflow.com/a/70949953 |
| 2 | +// https://stackoverflow.com/a/9517879 |
| 3 | + |
1 | 4 | (async () => { |
2 | | - try { |
3 | | - const { MsgType, Events, ClickType } = await import( |
4 | | - "../helpers/constants.js" |
5 | | - ); |
6 | | - const { sendEventToBackground, isFunction } = await import( |
7 | | - "../helpers/utils.js" |
8 | | - ); |
9 | | - |
10 | | - sendEventToBackground({ |
11 | | - type: MsgType.runScript, |
12 | | - event: Events.onDocumentStart, |
13 | | - }); |
14 | | - |
15 | | - chrome.runtime.onMessage.addListener(async function ( |
16 | | - message, |
17 | | - sender, |
18 | | - sendResponse |
19 | | - ) { |
20 | | - console.log("> Received message:", message); |
21 | | - |
22 | | - switch (message.type) { |
23 | | - case MsgType.runScript: |
24 | | - let scriptId = message.scriptId; |
25 | | - const script = (await import("../" + scriptId + ".js"))?.default; |
26 | | - |
27 | | - if (script && isFunction(script[ClickType.onClickContentScript])) { |
28 | | - script[ClickType.onClickContentScript](); |
29 | | - console.log("> Run script " + scriptId); |
30 | | - } |
31 | | - break; |
32 | | - } |
33 | | - }); |
34 | | - |
35 | | - // https://stackoverflow.com/a/53033388 |
36 | | - const { getURL, injectScript, injectCss } = await import("./utils.js"); |
37 | | - injectScript(getURL("useful-scripts-utils.js")); |
38 | | - } catch (e) { |
39 | | - console.log("ERROR: ", e); |
| 5 | + function injectScript(url) { |
| 6 | + var s = document.createElement("script"); |
| 7 | + s.src = url; |
| 8 | + (document.head || document.documentElement).appendChild(s); |
| 9 | + console.log("Useful-scripts injected " + url); |
| 10 | + s.remove(); |
40 | 11 | } |
| 12 | + |
| 13 | + let key = "activeScripts"; |
| 14 | + let ids = (await chrome.storage.sync.get([key]))?.[key]; |
| 15 | + |
| 16 | + injectScript( |
| 17 | + chrome.runtime.getURL("/scripts/content-scripts/load_scripts.js") + |
| 18 | + "?ids=" + |
| 19 | + ids |
| 20 | + ); |
41 | 21 | })(); |
| 22 | + |
| 23 | +// (async () => { |
| 24 | +// try { |
| 25 | +// const { MsgType, Events, ClickType } = await import( |
| 26 | +// "../helpers/constants.js" |
| 27 | +// ); |
| 28 | +// const { sendEventToBackground, isFunction } = await import( |
| 29 | +// "../helpers/utils.js" |
| 30 | +// ); |
| 31 | + |
| 32 | +// sendEventToBackground({ |
| 33 | +// type: MsgType.runScript, |
| 34 | +// event: Events.onDocumentStart, |
| 35 | +// }); |
| 36 | + |
| 37 | +// chrome.runtime.onMessage.addListener(async function ( |
| 38 | +// message, |
| 39 | +// sender, |
| 40 | +// sendResponse |
| 41 | +// ) { |
| 42 | +// console.log("> Received message:", message); |
| 43 | + |
| 44 | +// switch (message.type) { |
| 45 | +// case MsgType.runScript: |
| 46 | +// let scriptId = message.scriptId; |
| 47 | +// const script = (await import("../" + scriptId + ".js"))?.default; |
| 48 | + |
| 49 | +// if (script && isFunction(script[ClickType.onClickContentScript])) { |
| 50 | +// script[ClickType.onClickContentScript](); |
| 51 | +// console.log("> Run script " + scriptId); |
| 52 | +// } |
| 53 | +// break; |
| 54 | +// } |
| 55 | +// }); |
| 56 | + |
| 57 | +// // https://stackoverflow.com/a/53033388 |
| 58 | +// const { getURL, injectScript, injectCss } = await import("./utils.js"); |
| 59 | +// injectScript(getURL("content-scripts/scripts/useful-scripts-utils.js")); |
| 60 | +// // injectScript(getURL("content-scripts/load_script.js"), "module"); |
| 61 | +// } catch (e) { |
| 62 | +// console.log("ERROR: ", e); |
| 63 | +// } |
| 64 | +// })(); |
0 commit comments