|
1 | | -// communication between page-script and content-script |
2 | | -(() => { |
3 | | - function sendToPageScript(event, data) { |
4 | | - window.dispatchEvent( |
5 | | - new CustomEvent("ufs-contentscript-sendto-pagescript", { |
6 | | - detail: { event, data }, |
7 | | - }) |
8 | | - ); |
9 | | - } |
10 | | - window.addEventListener("ufs-pagescript-sendto-contentscript", (e) => { |
11 | | - let { event, data } = e.detail; |
12 | | - switch (event) { |
13 | | - case "getURL": |
14 | | - sendToPageScript(event, chrome.runtime.getURL(data)); |
15 | | - break; |
16 | | - } |
17 | | - }); |
18 | | -})(); |
19 | | - |
20 | 1 | // run all scripts that has onDocumentStart event |
21 | 2 | (async () => { |
22 | | - import( |
23 | | - chrome.runtime.getURL( |
24 | | - "/scripts/content-scripts/scripts/ufs_global_webpage_context.js" |
25 | | - ) |
26 | | - ); |
27 | | - |
28 | | - let key = "activeScripts"; |
29 | | - let ids = (await chrome.storage.sync.get([key]))?.[key] || ""; |
30 | | - let path = chrome.runtime.getURL("/scripts/"); |
31 | | - |
32 | | - localStorage.setItem( |
33 | | - "ufs-auto-run-scripts", |
34 | | - JSON.stringify({ |
35 | | - ids: ids, |
36 | | - path: path, |
37 | | - event: "onDocumentStart", |
| 3 | + window.dispatchEvent( |
| 4 | + new CustomEvent("ufs-run-page-scripts", { |
| 5 | + detail: { event: "onDocumentStart" }, |
38 | 6 | }) |
39 | 7 | ); |
40 | | - |
41 | | - import(chrome.runtime.getURL("/scripts/content-scripts/run_scripts.js")); |
42 | | -})(); |
43 | | - |
44 | | -// Run script on user click (if clicked script has onClickContentScript event) |
45 | | -(async () => { |
46 | | - try { |
47 | | - const { MsgType, ClickType } = await import("../helpers/constants.js"); |
48 | | - const { isFunction } = await import("../helpers/utils.js"); |
49 | | - |
50 | | - chrome.runtime.onMessage.addListener(async function ( |
51 | | - message, |
52 | | - sender, |
53 | | - sendResponse |
54 | | - ) { |
55 | | - console.log("> Received message:", message); |
56 | | - |
57 | | - switch (message.type) { |
58 | | - case MsgType.runScript: |
59 | | - let scriptId = message.scriptId; |
60 | | - const script = (await import("../" + scriptId + ".js"))?.default; |
61 | | - |
62 | | - if (script && isFunction(script[ClickType.onClickContentScript])) { |
63 | | - script[ClickType.onClickContentScript](); |
64 | | - console.log("> Run script " + scriptId); |
65 | | - } |
66 | | - break; |
67 | | - } |
68 | | - }); |
69 | | - } catch (e) { |
70 | | - console.log("ERROR: ", e); |
71 | | - } |
72 | 8 | })(); |
73 | | - |
74 | | -// https://stackoverflow.com/a/70949953 |
75 | | -// https://stackoverflow.com/a/9517879 |
76 | | -// https://stackoverflow.com/a/2920207/11898496 |
77 | | - |
78 | | -// https://stackoverflow.com/a/8578840/11898496 |
79 | | -function injectScript( |
80 | | - src, |
81 | | - onload, |
82 | | - type = "text/javascript", |
83 | | - async = false, |
84 | | - defer = false |
85 | | -) { |
86 | | - let s = document.createElement("script"); |
87 | | - s.type = type; |
88 | | - s.async = async; |
89 | | - s.defer = defer; |
90 | | - s.addEventListener("load", () => { |
91 | | - console.log("Useful-scripts injected " + src); |
92 | | - onload?.(); |
93 | | - s.remove(); |
94 | | - }); |
95 | | - s.src = src; |
96 | | - let head = |
97 | | - document.head || |
98 | | - document.getElementsByTagName("head")[0] || |
99 | | - document.documentElement; |
100 | | - head.insertBefore(s, head.firstChild); |
101 | | - // (document.head || document.documentElement).prepend(s); |
102 | | -} |
0 commit comments