Skip to content

Commit ab1c790

Browse files
Merge pull request #16 from HoangTran0410/dev
Fix clickContentScript, onDocumentStart/Idle/End by correct world: MAIN/ISOLATED
2 parents c2351b9 + 07aad60 commit ab1c790

15 files changed

+112
-174
lines changed

manifest.json

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,38 @@
2929
"content_scripts": [
3030
{
3131
"matches": ["<all_urls>"],
32-
"js": ["scripts/content-scripts/scripts/ufs_global_webpage_context.js"],
32+
"js": ["scripts/content-scripts/content_script.js"],
3333
"run_at": "document_start",
34-
"world": "MAIN"
34+
"world": "ISOLATED",
35+
"match_origin_as_fallback": true,
36+
"match_about_blank": true
3537
},
3638
{
3739
"matches": ["<all_urls>"],
38-
"js": ["scripts/content-scripts/document_start.js"],
40+
"js": [
41+
"scripts/content-scripts/scripts/ufs_global_webpage_context.js",
42+
"scripts/content-scripts/run_scripts.js"
43+
],
3944
"run_at": "document_start",
40-
"world": "ISOLATED"
45+
"world": "MAIN",
46+
"match_origin_as_fallback": true,
47+
"match_about_blank": true
4148
},
4249
{
4350
"matches": ["<all_urls>"],
4451
"js": ["scripts/content-scripts/document_idle.js"],
4552
"run_at": "document_idle",
46-
"world": "ISOLATED"
53+
"world": "MAIN",
54+
"match_origin_as_fallback": true,
55+
"match_about_blank": true
4756
},
4857
{
4958
"matches": ["<all_urls>"],
5059
"js": ["scripts/content-scripts/document_end.js"],
5160
"run_at": "document_end",
52-
"world": "ISOLATED"
61+
"world": "MAIN",
62+
"match_origin_as_fallback": true,
63+
"match_about_blank": true
5364
}
5465
],
5566
"web_accessible_resources": [

popup/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ function createTabs() {
6868
// show scripts count
6969
if (tab.showCount) {
7070
let avaiCount = tab.scripts.filter((script) => !isTitle(script)).length;
71-
let allCount = Object.keys(allScripts).length;
72-
tabBtn.innerHTML += ` (${avaiCount}/${allCount})`;
71+
tabBtn.innerHTML += ` (${avaiCount})`;
7372
}
7473

7574
// custom style

popup/tabs.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const tabs = [
2828
{
2929
...CATEGORY.search,
3030
scripts: [
31-
// s._test,
31+
s._test,
3232
s.search_userscript,
3333
s.whatFont,
3434
s.similarWeb,
@@ -49,10 +49,10 @@ const tabs = [
4949
...CATEGORY.download,
5050
scripts: [
5151
createTitle("--- All in one ---", "--- Tổng hợp ---"),
52-
s.savevideo_me,
52+
s.vuiz_getLink,
5353
s.saveAllVideo,
54+
s.savevideo_me,
5455
s.getLinkLuanxt,
55-
s.vuiz_getLink,
5656
// s.bookmark_exporter,
5757
s.twitter_downloadButton,
5858
createTitle("--- Music ---", "--- Nhạc ---"),
@@ -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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import("./scripts/ufs_global_webpage_context.js");
2+
3+
// communication between page-script and content-script
4+
(() => {
5+
function sendToPageScript(event, data) {
6+
window.dispatchEvent(
7+
new CustomEvent("ufs-contentscript-sendto-pagescript", {
8+
detail: { event, data },
9+
})
10+
);
11+
}
12+
window.addEventListener("ufs-pagescript-sendto-contentscript", async (e) => {
13+
let { event, data } = e.detail;
14+
switch (event) {
15+
case "getURL":
16+
sendToPageScript(event, chrome.runtime.getURL(data));
17+
break;
18+
case "getActiveScripts":
19+
const key = "activeScripts";
20+
let ids = (await chrome.storage.sync.get([key]))?.[key] || "";
21+
let path = chrome.runtime.getURL("/scripts/");
22+
sendToPageScript(event, { ids, path });
23+
break;
24+
}
25+
});
26+
})();
27+
28+
// Run script on user click (if clicked script has onClickContentScript event)
29+
(async () => {
30+
try {
31+
const { MsgType, ClickType } = await import("../helpers/constants.js");
32+
const { isFunction } = await import("../helpers/utils.js");
33+
34+
chrome.runtime.onMessage.addListener(async function (
35+
message,
36+
sender,
37+
sendResponse
38+
) {
39+
console.log("> Received message:", message);
40+
41+
switch (message.type) {
42+
case MsgType.runScript:
43+
let scriptId = message.scriptId;
44+
const script = (
45+
await import(chrome.runtime.getURL("/scripts/") + scriptId + ".js")
46+
)?.default;
47+
48+
if (script && isFunction(script[ClickType.onClickContentScript])) {
49+
script[ClickType.onClickContentScript]();
50+
console.log("> Run script " + scriptId);
51+
}
52+
break;
53+
}
54+
});
55+
} catch (e) {
56+
console.log("ERROR: ", e);
57+
}
58+
})();
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
})();

scripts/content-scripts/document_start.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

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 & 5 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
@@ -1341,8 +1346,3 @@ const UsefulScriptsUtils = {
13411346
downloadData: UsefulScriptGlobalPageContext.Utils.downloadData,
13421347
};
13431348
window.UsefulScriptsUtils = UsefulScriptsUtils;
1344-
1345-
// ================================= Polyfill =================================
1346-
// Chrome pre-34
1347-
if (!Element.prototype.matches)
1348-
Element.prototype.matches = Element.prototype.webkitMatchesSelector;

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 {

scripts/fb_whoIsTyping.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export default {
2020
websocket_instant.addEventListener("message", async function (achunk) {
2121
let utf8_str = textDecoder.decode(achunk.data);
2222

23+
console.log(utf8_str, achunk);
24+
2325
if (
2426
utf8_str.startsWith("1") &&
2527
utf8_str.includes("updateTypingIndicator")

0 commit comments

Comments
 (0)