Skip to content

Commit 7662ccd

Browse files
committed
fix
1 parent f725f85 commit 7662ccd

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

scripts/content-scripts/document_end.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(async () => {
2-
let ids = localStorage.getItem("activeScripts") || "";
2+
let key = "activeScripts";
3+
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
34
window.dispatchEvent(
45
new CustomEvent("ufs-run-page-scripts", {
56
detail: {

scripts/content-scripts/document_idle.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(async () => {
2-
let ids = localStorage.getItem("activeScripts") || "";
2+
let key = "activeScripts";
3+
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
34
window.dispatchEvent(
45
new CustomEvent("ufs-run-page-scripts", {
56
detail: {

scripts/content-scripts/document_start.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@
3434
"/scripts/content-scripts/scripts/ufs_global_webpage_context.js"
3535
)
3636
);
37-
let ids = localStorage.getItem("activeScripts") || "";
37+
38+
let key = "activeScripts";
39+
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
3840
let path = chrome.runtime.getURL("/scripts/");
39-
let search = new URLSearchParams({
40-
ids: ids,
41-
path: path,
42-
event: "onDocumentStart",
43-
}).toString();
41+
42+
localStorage.setItem(
43+
"ufs-auto-run-scripts",
44+
JSON.stringify({
45+
ids: ids,
46+
path: path,
47+
event: "onDocumentStart",
48+
})
49+
);
4450

4551
injectScript(
46-
chrome.runtime.getURL("/scripts/content-scripts/run_scripts.js") +
47-
"?" +
48-
search
52+
chrome.runtime.getURL("/scripts/content-scripts/run_scripts.js")
4953
);
5054
})();
5155

scripts/content-scripts/run_scripts.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
// Quá trình maintain sẽ khó hơn 1 chút, nhưng script sẽ chạy chính xác hơn
99

1010
(() => {
11-
let search = new URLSearchParams(getCurrentScriptSrc().split("?")?.[1]);
12-
let path = search.get("path");
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+
);
1317

1418
// run script on receive event
1519
window.addEventListener("ufs-run-page-scripts", ({ detail }) => {
@@ -18,14 +22,10 @@
1822
});
1923

2024
// auto run initial event defined in URL search params
21-
(() => {
22-
let ids = search.get("ids");
23-
let event = search.get("event");
24-
if (ids && event) {
25-
let scriptIds = ids.split(",");
26-
runScripts(scriptIds, event, path);
27-
}
28-
})();
25+
if (ids && event) {
26+
let scriptIds = ids.split(",");
27+
runScripts(scriptIds, event, path);
28+
}
2929
})();
3030

3131
function getCurrentScriptSrc() {
@@ -39,7 +39,7 @@ function getCurrentScriptSrc() {
3939
}
4040

4141
function runScripts(scriptIds, event, path) {
42-
for (let id of scriptIds) {
42+
for (let id of scriptIds.filter((_) => _)) {
4343
let scriptPath = `${path}/${id}.js`;
4444
import(scriptPath).then(({ default: script }) => {
4545
try {

scripts/helpers/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export function setActiveScript(scriptId, isActive = true) {
2525
if (isActive) list.push(scriptId);
2626
else list = list.filter((_) => _ != scriptId);
2727
list = list.filter((_) => _);
28-
localStorage.setItem(listActiveScriptsKey, list.join(","));
28+
let valToSave = list.join(",");
29+
localStorage.setItem(listActiveScriptsKey, valToSave);
30+
chrome.storage.sync.set({ [listActiveScriptsKey]: valToSave }); // save to storage => content script can access
2931
return list;
3032
}
3133

0 commit comments

Comments
 (0)