Skip to content

Commit a5b5d60

Browse files
author
hoang.tran12
committed
content script work now
1 parent 3a53b15 commit a5b5d60

File tree

9 files changed

+379
-178
lines changed

9 files changed

+379
-178
lines changed

manifest.json

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,33 @@
2424
"declarativeNetRequestFeedback",
2525
"declarativeNetRequestWithHostAccess"
2626
],
27-
"host_permissions": [
28-
"<all_urls>"
29-
],
27+
"host_permissions": ["<all_urls>"],
3028
"options_page": "./pages/options/options.html",
3129
"background": {
3230
"service_worker": "scripts/background-scripts/index.js",
3331
"type": "module"
3432
},
3533
"content_scripts": [
3634
{
37-
"matches": [
38-
"<all_urls>"
39-
],
40-
"js": [
41-
"scripts/content-scripts/document_start.js"
42-
],
35+
"matches": ["<all_urls>"],
36+
"js": ["scripts/content-scripts/document_start.js"],
4337
"run_at": "document_start"
4438
},
4539
{
46-
"matches": [
47-
"<all_urls>"
48-
],
49-
"js": [
50-
"scripts/content-scripts/document_idle.js"
51-
],
40+
"matches": ["<all_urls>"],
41+
"js": ["scripts/content-scripts/document_idle.js"],
5242
"run_at": "document_idle"
5343
},
5444
{
55-
"matches": [
56-
"<all_urls>"
57-
],
58-
"js": [
59-
"scripts/content-scripts/document_end.js"
60-
],
45+
"matches": ["<all_urls>"],
46+
"js": ["scripts/content-scripts/document_end.js"],
6147
"run_at": "document_end"
6248
}
6349
],
6450
"web_accessible_resources": [
6551
{
66-
"resources": [
67-
"scripts/*.js",
68-
"scripts/*.css"
69-
],
70-
"matches": [
71-
"<all_urls>"
72-
]
52+
"resources": ["scripts/*.js", "scripts/*.css"],
53+
"matches": ["<all_urls>"]
7354
}
7455
],
7556
"declarative_net_request": {
@@ -81,4 +62,4 @@
8162
}
8263
]
8364
}
84-
}
65+
}
Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
import { allScripts } from "../index.js";
2-
import { Events } from "../helpers/constants.js";
3-
import { MsgType } from "../helpers/constants.js";
4-
import {
5-
checkBlackWhiteList,
6-
isActiveScript,
7-
isEmptyFunction,
8-
isFunction,
9-
runScriptInTab,
10-
} from "../helpers/utils.js";
11-
12-
const CACHED = {
13-
runCount: {},
14-
};
15-
16-
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
17-
console.log("> Received message:", message, sender?.tab?.url);
18-
19-
switch (message.type) {
20-
case MsgType.runScript:
21-
runScript(message.event, sender.tab);
22-
break;
23-
}
24-
});
25-
26-
async function runScript(event, tab) {
27-
let funcName = event,
28-
tabId = tab.id,
29-
url = tab.url;
30-
31-
if (!(tabId in CACHED.runCount) || event === Events.onDocumentStart)
32-
CACHED.runCount[tabId] = 0;
33-
34-
for (let script of Object.values(allScripts)) {
35-
try {
36-
if (!checkBlackWhiteList(script, url)) continue;
37-
38-
let func = script[funcName];
39-
if (isFunction(func) && !isEmptyFunction(func)) {
40-
let isActive = (await isActiveScript(script.id)) ?? true;
41-
if (isActive) {
42-
runScriptInTab({ func, tabId });
43-
console.log(
44-
`%c > Run ${script.id} ${funcName} in ${url}`,
45-
"background: #222; color: #bada55"
46-
);
47-
CACHED.runCount[tabId]++;
48-
}
49-
}
50-
} catch (e) {
51-
console.log("ERROR at script " + script?.id, e);
52-
}
53-
}
54-
55-
updateBadge(tabId, CACHED.runCount[tabId]);
56-
}
57-
58-
function updateBadge(tabId, text = "", bgColor = "#666") {
59-
text = text.toString();
60-
chrome.action.setBadgeText({ tabId, text: text == "0" ? "" : text });
61-
chrome.action.setBadgeBackgroundColor({ color: bgColor });
62-
}
1+
// import { allScripts } from "../index.js";
2+
// import { Events } from "../helpers/constants.js";
3+
// import { MsgType } from "../helpers/constants.js";
4+
// import {
5+
// checkBlackWhiteList,
6+
// isActiveScript,
7+
// isEmptyFunction,
8+
// isFunction,
9+
// runScriptInTab,
10+
// } from "../helpers/utils.js";
11+
12+
// const CACHED = {
13+
// runCount: {},
14+
// };
15+
16+
// chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
17+
// console.log("> Received message:", message, sender?.tab?.url);
18+
19+
// switch (message.type) {
20+
// case MsgType.runScript:
21+
// runScript(message.event, sender.tab);
22+
// break;
23+
// }
24+
// });
25+
26+
// async function runScript(event, tab) {
27+
// let funcName = event,
28+
// tabId = tab.id,
29+
// url = tab.url;
30+
31+
// if (!(tabId in CACHED.runCount) || event === Events.onDocumentStart)
32+
// CACHED.runCount[tabId] = 0;
33+
34+
// for (let script of Object.values(allScripts)) {
35+
// try {
36+
// if (!checkBlackWhiteList(script, url)) continue;
37+
38+
// let func = script[funcName];
39+
// if (isFunction(func) && !isEmptyFunction(func)) {
40+
// let isActive = (await isActiveScript(script.id)) ?? true;
41+
// if (isActive) {
42+
// runScriptInTab({ func, tabId });
43+
// console.log(
44+
// `%c > Run ${script.id} ${funcName} in ${url}`,
45+
// "background: #222; color: #bada55"
46+
// );
47+
// CACHED.runCount[tabId]++;
48+
// }
49+
// }
50+
// } catch (e) {
51+
// console.log("ERROR at script " + script?.id, e);
52+
// }
53+
// }
54+
55+
// updateBadge(tabId, CACHED.runCount[tabId]);
56+
// }
57+
58+
// function updateBadge(tabId, text = "", bgColor = "#666") {
59+
// text = text.toString();
60+
// chrome.action.setBadgeText({ tabId, text: text == "0" ? "" : text });
61+
// chrome.action.setBadgeBackgroundColor({ color: bgColor });
62+
// }
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
(async () => {
2-
try {
3-
const { MsgType, Events } = await import("../helpers/constants.js");
4-
const { sendEventToBackground } = await import("../helpers/utils.js");
5-
sendEventToBackground({
6-
type: MsgType.runScript,
7-
event: Events.onDocumentEnd,
8-
});
9-
} catch (e) {
10-
console.log("ERROR: ", e);
2+
function injectScript(url) {
3+
var s = document.createElement("script");
4+
s.src = url;
5+
(document.head || document.documentElement).appendChild(s);
6+
console.log("Useful-scripts injected " + url);
7+
s.remove();
118
}
9+
10+
let key = "activeScripts";
11+
let ids = (await chrome.storage.sync.get([key]))?.[key];
12+
let search = new URLSearchParams({
13+
ids: ids,
14+
path: chrome.runtime.getURL("/scripts/"),
15+
event: "onDocumentEnd",
16+
}).toString();
17+
18+
injectScript(
19+
chrome.runtime.getURL("/scripts/content-scripts/run_scripts.js") +
20+
"?" +
21+
search
22+
);
1223
})();
24+
25+
// (async () => {
26+
// try {
27+
// const { MsgType, Events } = await import("../helpers/constants.js");
28+
// const { sendEventToBackground } = await import("../helpers/utils.js");
29+
// sendEventToBackground({
30+
// type: MsgType.runScript,
31+
// event: Events.onDocumentEnd,
32+
// });
33+
// } catch (e) {
34+
// console.log("ERROR: ", e);
35+
// }
36+
// })();
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
(async () => {
2-
try {
3-
const { MsgType, Events } = await import("../helpers/constants.js");
4-
const { sendEventToBackground } = await import("../helpers/utils.js");
5-
sendEventToBackground({
6-
type: MsgType.runScript,
7-
event: Events.onDocumentIdle,
8-
});
9-
} catch (e) {
10-
console.log("ERROR: ", e);
2+
function injectScript(url) {
3+
var s = document.createElement("script");
4+
s.src = url;
5+
(document.head || document.documentElement).appendChild(s);
6+
console.log("Useful-scripts injected " + url);
7+
s.remove();
118
}
9+
10+
let key = "activeScripts";
11+
let ids = (await chrome.storage.sync.get([key]))?.[key];
12+
let search = new URLSearchParams({
13+
ids: ids,
14+
path: chrome.runtime.getURL("/scripts/"),
15+
event: "onDocumentIdle",
16+
}).toString();
17+
18+
injectScript(
19+
chrome.runtime.getURL("/scripts/content-scripts/run_scripts.js") +
20+
"?" +
21+
search
22+
);
1223
})();
24+
25+
// (async () => {
26+
// try {
27+
// const { MsgType, Events } = await import("../helpers/constants.js");
28+
// const { sendEventToBackground } = await import("../helpers/utils.js");
29+
// sendEventToBackground({
30+
// type: MsgType.runScript,
31+
// event: Events.onDocumentIdle,
32+
// });
33+
// } catch (e) {
34+
// console.log("ERROR: ", e);
35+
// }
36+
// })();

scripts/content-scripts/document_start.js

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,50 @@
1919
}).toString();
2020

2121
injectScript(
22-
chrome.runtime.getURL("/scripts/content-scripts/load_scripts.js") +
22+
chrome.runtime.getURL("/scripts/content-scripts/run_scripts.js") +
2323
"?" +
2424
search
2525
);
2626
})();
2727

28-
// (async () => {
29-
// try {
30-
// const { MsgType, Events, ClickType } = await import(
31-
// "../helpers/constants.js"
32-
// );
33-
// const { sendEventToBackground, isFunction } = await import(
34-
// "../helpers/utils.js"
35-
// );
28+
(async () => {
29+
try {
30+
const { MsgType, Events, ClickType } = await import(
31+
"../helpers/constants.js"
32+
);
33+
const { sendEventToBackground, isFunction } = await import(
34+
"../helpers/utils.js"
35+
);
3636

37-
// sendEventToBackground({
38-
// type: MsgType.runScript,
39-
// event: Events.onDocumentStart,
40-
// });
37+
// sendEventToBackground({
38+
// type: MsgType.runScript,
39+
// event: Events.onDocumentStart,
40+
// });
4141

42-
// chrome.runtime.onMessage.addListener(async function (
43-
// message,
44-
// sender,
45-
// sendResponse
46-
// ) {
47-
// console.log("> Received message:", message);
42+
chrome.runtime.onMessage.addListener(async function (
43+
message,
44+
sender,
45+
sendResponse
46+
) {
47+
console.log("> Received message:", message);
4848

49-
// switch (message.type) {
50-
// case MsgType.runScript:
51-
// let scriptId = message.scriptId;
52-
// const script = (await import("../" + scriptId + ".js"))?.default;
49+
switch (message.type) {
50+
case MsgType.runScript:
51+
let scriptId = message.scriptId;
52+
const script = (await import("../" + scriptId + ".js"))?.default;
5353

54-
// if (script && isFunction(script[ClickType.onClickContentScript])) {
55-
// script[ClickType.onClickContentScript]();
56-
// console.log("> Run script " + scriptId);
57-
// }
58-
// break;
59-
// }
60-
// });
54+
if (script && isFunction(script[ClickType.onClickContentScript])) {
55+
script[ClickType.onClickContentScript]();
56+
console.log("> Run script " + scriptId);
57+
}
58+
break;
59+
}
60+
});
6161

62-
// // https://stackoverflow.com/a/53033388
63-
// const { getURL, injectScript, injectCss } = await import("./utils.js");
64-
// injectScript(getURL("content-scripts/scripts/useful-scripts-utils.js"));
65-
// // injectScript(getURL("content-scripts/load_script.js"), "module");
66-
// } catch (e) {
67-
// console.log("ERROR: ", e);
68-
// }
69-
// })();
62+
// https://stackoverflow.com/a/53033388
63+
const { getURL, injectScript, injectCss } = await import("./utils.js");
64+
injectScript(getURL("content-scripts/scripts/useful-scripts-utils.js"));
65+
} catch (e) {
66+
console.log("ERROR: ", e);
67+
}
68+
})();

scripts/content-scripts/load_scripts.js

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

0 commit comments

Comments
 (0)