Skip to content

Commit 534ac7f

Browse files
committed
fix document start - wip
1 parent b815289 commit 534ac7f

File tree

7 files changed

+121
-137
lines changed

7 files changed

+121
-137
lines changed
Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
(async () => {
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();
8-
}
9-
102
let key = "activeScripts";
113
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
4+
window.dispatchEvent(
5+
new CustomEvent("ufs-run-page-scripts", {
6+
detail: {
7+
event: "onDocumentEnd",
8+
ids: ids.split(","),
9+
},
10+
})
2211
);
2312
})();
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: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
(async () => {
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();
8-
}
9-
102
let key = "activeScripts";
113
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
4+
window.dispatchEvent(
5+
new CustomEvent("ufs-run-page-scripts", {
6+
detail: {
7+
event: "onDocumentIdle",
8+
ids: ids.split(","),
9+
},
10+
})
2211
);
2312
})();
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: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
// https://stackoverflow.com/a/70949953
22
// https://stackoverflow.com/a/9517879
3+
// https://stackoverflow.com/a/2920207/11898496
34

45
(async () => {
5-
function injectScript(url) {
6-
var s = document.createElement("script");
7-
s.src = url;
8-
(document.head || document.documentElement).appendChild(s);
9-
console.log("Useful-scripts injected " + url);
10-
s.remove();
6+
// https://stackoverflow.com/a/8578840/11898496
7+
function injectScript(src) {
8+
let s = document.createElement("script");
9+
s.type = "text/javascript";
10+
s.async = false;
11+
s.defer = false;
12+
s.addEventListener("load", () => {
13+
console.log("Useful-scripts injected " + src);
14+
// s.remove();
15+
});
16+
s.src = src;
17+
let head =
18+
document.head ||
19+
document.getElementsByTagName("head")[0] ||
20+
document.documentElement;
21+
head.insertBefore(s, head.firstChild);
22+
// (document.head || document.documentElement).prepend(s);
1123
}
1224

13-
injectScript(
14-
chrome.runtime.getURL(
15-
"/scripts/content-scripts/scripts/ufs_global_webpage_context.js"
16-
)
17-
);
25+
console.log(window.__d);
26+
27+
// function injectScript(url) {
28+
// var s = document.createElement("script");
29+
// s.src = url;
30+
// s.async = !1;
31+
// (document.head || document.documentElement).appendChild(s);
32+
// console.log("Useful-scripts injected " + url);
33+
// s.remove();
34+
// }
1835

1936
let key = "activeScripts";
2037
let ids = (await chrome.storage.sync.get([key]))?.[key];
@@ -29,21 +46,18 @@
2946
"?" +
3047
search
3148
);
49+
50+
injectScript(
51+
chrome.runtime.getURL(
52+
"/scripts/content-scripts/scripts/ufs_global_webpage_context.js"
53+
)
54+
);
3255
})();
3356

3457
(async () => {
3558
try {
36-
const { MsgType, Events, ClickType } = await import(
37-
"../helpers/constants.js"
38-
);
39-
const { sendEventToBackground, isFunction } = await import(
40-
"../helpers/utils.js"
41-
);
42-
43-
// sendEventToBackground({
44-
// type: MsgType.runScript,
45-
// event: Events.onDocumentStart,
46-
// });
59+
const { MsgType, ClickType } = await import("../helpers/constants.js");
60+
const { isFunction } = await import("../helpers/utils.js");
4761

4862
chrome.runtime.onMessage.addListener(async function (
4963
message,

scripts/content-scripts/run_scripts.js

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,55 @@
55

66
// Do đó các hàm cần thiết nên ghi hết vào trong file này
77
// Kể cả việc nó đã được viết ở file khác (utils, helper, ...)
8-
// Qúa trình maintain sẽ khó hơn 1 chút, nhưng script sẽ chạy chính xác hơn
8+
// 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-
(async () => {
10+
console.log(window.__d);
11+
12+
(() => {
1113
const params = new URLSearchParams(
1214
document.currentScript.src.split("?")?.[1]
1315
);
1416

15-
let ids = params.get("ids");
1617
let path = params.get("path");
17-
let event = params.get("event");
1818

19-
if (ids) {
20-
let scriptIds = ids.split(",");
19+
// run script on receive event
20+
window.addEventListener("ufs-run-page-scripts", ({ detail }) => {
21+
const { event, ids } = detail;
22+
runScripts(ids, event, path);
23+
});
2124

22-
for (let id of scriptIds) {
23-
// import and run script with event name
24-
import(`${path}/${id}.js`).then((module) => {
25-
try {
26-
let script = module.default;
27-
if (typeof script[event] === "function" && checkWillRun(script)) {
28-
console.log("> Useful-script: Run script " + id, script);
29-
script[event]();
30-
}
31-
} catch (e) {
32-
console.log("ERROR run script " + id, e);
33-
}
34-
});
25+
// auto run onDocumentStart
26+
(() => {
27+
let ids = params.get("ids");
28+
let event = params.get("event");
29+
if (ids) {
30+
let scriptIds = ids.split(",");
31+
runScripts(scriptIds, event, path);
3532
}
36-
}
33+
})();
3734
})();
3835

36+
function runScripts(scriptIds, event, path) {
37+
for (let id of scriptIds) {
38+
// import and run script with event name
39+
let scriptPath = `${path}/${id}.js`;
40+
import(scriptPath).then(({ default: script }) => {
41+
try {
42+
if (
43+
event in script &&
44+
typeof script[event] === "function" &&
45+
checkWillRun(script)
46+
) {
47+
console.log("> Useful-script: Run script " + id + " " + event);
48+
script[event]();
49+
}
50+
} catch (e) {
51+
console.log("ERROR run script " + id + " " + event, e);
52+
}
53+
});
54+
}
55+
}
56+
3957
function checkWillRun(script) {
4058
let url = location.href;
4159
let hasWhiteList = script.whiteList?.length > 0;

scripts/fb_revealDeletedMessages.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ export default {
1111
whiteList: ["https://*.facebook.com/*", "https://*.messenger.com/*"],
1212

1313
onDocumentStart: () => {
14-
// const WebSocketOrig = window.WebSocket;
14+
const WebSocketOrig = window.WebSocket;
1515

16-
// window.WebSocket = function fakeConstructor(dt, config) {
17-
// const websocket_instant = new WebSocketOrig(dt, config);
18-
// websocket_instant.addEventListener("message", async function (achunk) {
19-
// // const utf8_str = new TextDecoder("utf-8").decode(achunk.data);
20-
// // Do something here
21-
// // console.log(utf8_str);
22-
// });
23-
// return websocket_instant;
24-
// };
16+
window.WebSocket = function fakeConstructor(dt, config) {
17+
const websocket_instant = new WebSocketOrig(dt, config);
18+
websocket_instant.addEventListener("message", async function (achunk) {
19+
// const utf8_str = new TextDecoder("utf-8").decode(achunk.data);
20+
// Do something here
21+
// console.log(utf8_str);
22+
});
23+
return websocket_instant;
24+
};
2525

26-
// window.WebSocket.prototype = WebSocketOrig.prototype;
27-
// window.WebSocket.prototype.constructor = window.WebSocket;
26+
window.WebSocket.prototype = WebSocketOrig.prototype;
27+
window.WebSocket.prototype.constructor = window.WebSocket;
2828

2929
// window.addEventListener(
3030
// "message",
@@ -34,19 +34,21 @@ export default {
3434
// !1
3535
// );
3636

37-
let emptyFunc = void 0;
38-
Object.defineProperty(window, "__d", {
39-
get: () => emptyFunc,
40-
set: (i) => {
41-
const c = new Proxy(i, {
42-
apply: async function (moduleName, dependencies, args) {
43-
console.log(arguments);
44-
return moduleName(...args);
45-
},
46-
});
47-
emptyFunc = c;
48-
},
49-
});
37+
console.log(window.__d);
38+
// let emptyFunc = void 0;
39+
// Object.defineProperty(window, "__d", {
40+
// get: () => emptyFunc,
41+
// set: (i) => {
42+
// const c = new Proxy(i, {
43+
// apply: async function (moduleName, dependencies, args) {
44+
// console.log(arguments);
45+
// // return moduleName(...args);
46+
// return i.apply(this, arguments);
47+
// },
48+
// });
49+
// emptyFunc = c;
50+
// },
51+
// });
5052
},
5153

5254
onDocumentIdle: () => {

scripts/helpers/utils.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
export async function sendEventToBackground(data) {
2-
// console.log("... Sending ", data, " to background...");
3-
const response = await chrome.runtime.sendMessage(data);
4-
// console.log("> ", data, " sent to background successfully", response);
5-
return response;
6-
}
7-
81
export async function sendEventToTab(tabId, data) {
92
console.log("... Sending ", data, " to tab...");
103
const response = await chrome.tabs.sendMessage(tabId, data);

scripts/injectScriptToWebsite.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
en: "",
99
vi: "",
1010
},
11-
onClick: function () {
11+
onClick: function () {
1212
// https://stackoverflow.com/a/26573284
1313
function injectScriptAndUse(src) {
1414
var script = document.createElement("script");
@@ -46,3 +46,8 @@ export default {
4646
}
4747
},
4848
};
49+
50+
function backup() {
51+
// https://stackoverflow.com/a/38840724/11898496
52+
// Script loader: https://plnkr.co/edit/b9O19f?p=preview&preview
53+
}

0 commit comments

Comments
 (0)