Skip to content

Commit 7b3bd6c

Browse files
committed
fix
1 parent 204fb8e commit 7b3bd6c

11 files changed

+102
-152
lines changed

manifest.json

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,39 @@
3535
},
3636
{
3737
"matches": ["<all_urls>"],
38-
"js": ["scripts/content-scripts/document_start.js"],
38+
"js": ["scripts/content-scripts/scripts/ufs_global_webpage_context.js"],
3939
"run_at": "document_start",
4040
"world": "ISOLATED"
4141
},
42+
{
43+
"matches": ["<all_urls>"],
44+
"js": ["scripts/content-scripts/content_script.js"],
45+
"run_at": "document_start",
46+
"world": "ISOLATED"
47+
},
48+
{
49+
"matches": ["<all_urls>"],
50+
"js": ["scripts/content-scripts/run_scripts.js"],
51+
"run_at": "document_start",
52+
"world": "MAIN"
53+
},
54+
{
55+
"matches": ["<all_urls>"],
56+
"js": ["scripts/content-scripts/document_start.js"],
57+
"run_at": "document_start",
58+
"world": "MAIN"
59+
},
4260
{
4361
"matches": ["<all_urls>"],
4462
"js": ["scripts/content-scripts/document_idle.js"],
4563
"run_at": "document_idle",
46-
"world": "ISOLATED"
64+
"world": "MAIN"
4765
},
4866
{
4967
"matches": ["<all_urls>"],
5068
"js": ["scripts/content-scripts/document_end.js"],
5169
"run_at": "document_end",
52-
"world": "ISOLATED"
70+
"world": "MAIN"
5371
}
5472
],
5573
"web_accessible_resources": [

popup/tabs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ const tabs = [
106106
s.fb_downloadCommentVideo,
107107
s.fb_videoDownloader,
108108
s.fb_getAvatarFromUid,
109-
s.fb_storyInfo,
109+
// s.fb_storyInfo,
110110
createTitle("--- Bulk Download ---", "--- Tải hàng loạt ---"),
111-
s.fb_bulkDownload,
111+
// s.fb_bulkDownload,
112112
s.fb_downloadAlbumMedia,
113113
s.fb_downloadWallMediaFromPosts,
114114
s.fb_getAllAlbumInformation,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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", async (e) => {
11+
let { event, data } = e.detail;
12+
switch (event) {
13+
case "getURL":
14+
sendToPageScript(event, chrome.runtime.getURL(data));
15+
break;
16+
case "getActiveScripts":
17+
const key = "activeScripts";
18+
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
19+
let path = chrome.runtime.getURL("/scripts/");
20+
sendToPageScript(event, { ids, path });
21+
break;
22+
}
23+
});
24+
})();
25+
26+
// Run script on user click (if clicked script has onClickContentScript event)
27+
(async () => {
28+
try {
29+
const { MsgType, ClickType } = await import("../helpers/constants.js");
30+
const { isFunction } = await import("../helpers/utils.js");
31+
32+
chrome.runtime.onMessage.addListener(async function (
33+
message,
34+
sender,
35+
sendResponse
36+
) {
37+
console.log("> Received message:", message);
38+
39+
switch (message.type) {
40+
case MsgType.runScript:
41+
let scriptId = message.scriptId;
42+
const script = (await import("../" + scriptId + ".js"))?.default;
43+
44+
if (script && isFunction(script[ClickType.onClickContentScript])) {
45+
script[ClickType.onClickContentScript]();
46+
console.log("> Run script " + scriptId);
47+
}
48+
break;
49+
}
50+
});
51+
} catch (e) {
52+
console.log("ERROR: ", e);
53+
}
54+
})();
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
(async () => {
2-
let key = "activeScripts";
3-
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
42
window.dispatchEvent(
53
new CustomEvent("ufs-run-page-scripts", {
6-
detail: {
7-
event: "onDocumentEnd",
8-
ids: ids.split(","),
9-
},
4+
detail: { event: "onDocumentEnd" },
105
})
116
);
127
})();
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
(async () => {
2-
let key = "activeScripts";
3-
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
42
window.dispatchEvent(
53
new CustomEvent("ufs-run-page-scripts", {
6-
detail: {
7-
event: "onDocumentIdle",
8-
ids: ids.split(","),
9-
},
4+
detail: { event: "onDocumentIdle" },
105
})
116
);
127
})();
Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,8 @@
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-
201
// run all scripts that has onDocumentStart event
212
(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" },
386
})
397
);
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-
}
728
})();
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-
}

scripts/content-scripts/run_scripts.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,25 @@
77
// Kể cả việc nó đã được viết ở file khác (utils, helper, ...)
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

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 = "";
1713

1814
// run script on receive event
1915
window.addEventListener("ufs-run-page-scripts", ({ detail }) => {
20-
const { event, ids } = detail;
21-
runScripts(ids, event, path);
16+
runScripts(ids, detail.event, path);
2217
});
2318

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 || "";
3023

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);
3827
}
39-
}
28+
})();
4029

4130
function runScripts(scriptIds, event, path) {
4231
for (let id of scriptIds.filter((_) => _)) {

scripts/content-scripts/scripts/ufs_global_webpage_context.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const UsefulScriptGlobalPageContext = {
3030
);
3131
}
3232
},
33+
getActiveScripts: async function () {
34+
return await UsefulScriptGlobalPageContext.Extension.sendToContentScript(
35+
"getActiveScripts"
36+
);
37+
},
3338
},
3439
DOM: {
3540
// https://stackoverflow.com/a/3381522

scripts/fb_messengerCount.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ export default {
4747

4848
localStorage.ufs_fb_msg_kount = JSON.stringify(ranking);
4949

50-
window.open(
51-
await UsefulScriptGlobalPageContext.Extension.getURL(
52-
"scripts/fb_messengerCount.html"
53-
)
54-
);
50+
window.open(chrome.runtime.getURL("scripts/fb_messengerCount.html"));
5551
} catch (e) {
5652
alert("ERROR: " + e);
5753
} finally {

scripts/fb_searchGroupForOther.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ export default {
4646
console.log(allGroups);
4747
localStorage.ufs_fb_searchGroupForOther = JSON.stringify(allGroups);
4848
localStorage.ufs_fb_searchGroupForOther_owner = JSON.stringify(info);
49-
window.open(
50-
await UsefulScriptGlobalPageContext.Extension.getURL(
51-
"scripts/fb_searchGroupForOther.html"
52-
)
53-
);
49+
window.open(chrome.runtime.getURL("scripts/fb_searchGroupForOther.html"));
5450
} catch (e) {
5551
alert("ERROR: " + e);
5652
} finally {

scripts/fb_searchPageForOther.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ export default {
4747
localStorage.ufs_fb_searchPageForOther = JSON.stringify(allPages);
4848
localStorage.ufs_fb_searchPageForOther_owner = JSON.stringify(info);
4949

50-
window.open(
51-
await UsefulScriptGlobalPageContext.Extension.getURL(
52-
"scripts/fb_searchPageForOther.html"
53-
)
54-
);
50+
window.open(chrome.runtime.getURL("scripts/fb_searchPageForOther.html"));
5551
} catch (e) {
5652
alert("ERROR: " + e);
5753
} finally {

0 commit comments

Comments
 (0)