|
7 | 7 | // Kể cả việc nó đã được viết ở file khác (utils, helper, ...) |
8 | 8 | // Quá trình maintain sẽ khó hơn 1 chút, nhưng script sẽ chạy chính xác hơn |
9 | 9 |
|
10 | | -(() => { |
11 | | - // let search = new URLSearchParams(getCurrentScriptSrc().split("?")?.[1]); |
12 | | - // let path = search.get("path"); |
13 | | - |
14 | | - let { path, ids, event } = JSON.parse( |
15 | | - localStorage.getItem("ufs-auto-run-scripts") ?? "{}" |
16 | | - ); |
| 10 | +(async () => { |
| 11 | + let ids = [], |
| 12 | + path = ""; |
17 | 13 |
|
18 | 14 | // run script on receive event |
19 | 15 | window.addEventListener("ufs-run-page-scripts", ({ detail }) => { |
20 | | - const { event, ids } = detail; |
21 | | - runScripts(ids, event, path); |
| 16 | + runScripts(ids, detail.event, path); |
22 | 17 | }); |
23 | 18 |
|
24 | | - // auto run initial event defined in URL search params |
25 | | - if (ids && event) { |
26 | | - let scriptIds = ids.split(","); |
27 | | - runScripts(scriptIds, event, path); |
28 | | - } |
29 | | -})(); |
| 19 | + const data = await UsefulScriptGlobalPageContext.Extension.getActiveScripts(); |
| 20 | + console.log(data); |
| 21 | + ids = data?.ids?.split(",") || []; |
| 22 | + path = data?.path || ""; |
30 | 23 |
|
31 | | -function getCurrentScriptSrc() { |
32 | | - try { |
33 | | - // cannot get currentScript if script type is module: https://stackoverflow.com/a/45845801/11898496 |
34 | | - // return import.meta.url; |
35 | | - throw false; |
36 | | - } catch (e) { |
37 | | - return document.currentScript.src; |
| 24 | + // auto run documentStart |
| 25 | + if (ids) { |
| 26 | + runScripts(ids, "onDocumentStart", path); |
38 | 27 | } |
39 | | -} |
| 28 | +})(); |
40 | 29 |
|
41 | 30 | function runScripts(scriptIds, event, path) { |
42 | 31 | for (let id of scriptIds.filter((_) => _)) { |
|
0 commit comments