Skip to content

Commit 720293a

Browse files
committed
optimize run in all frames
1 parent 4d241fc commit 720293a

File tree

13 files changed

+272
-536
lines changed

13 files changed

+272
-536
lines changed

popup/helpers/utils.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ const CONTEXTS = [
66
];
77

88
export const canClick = (script) => {
9-
for (let s of CONTEXTS) {
10-
if (typeof script[s]?.onClick === "function") return true;
9+
for (let context of CONTEXTS) {
10+
for (let fn of injectAllFramesFns(["onClick"])) {
11+
if (typeof script[context]?.[fn] === "function") {
12+
return true;
13+
}
14+
}
1115
}
1216
return false;
1317
};
1418

1519
function hasChildFunction(object, excludedNamesSet = new Set()) {
1620
for (let key in object) {
17-
if (key.startsWith("_")) continue;
21+
if (key.startsWith("_")) continue; // ignore private member
1822
if (!object[key]) continue;
1923
if (excludedNamesSet.has(key)) continue;
2024
if (typeof object[key] === "function") return true;
@@ -25,19 +29,24 @@ function hasChildFunction(object, excludedNamesSet = new Set()) {
2529
}
2630

2731
export const canAutoRun = (script) => {
28-
let excludedNamesSet = new Set([
29-
"onClick",
30-
"onInstalled",
31-
"onStartup",
32-
"onMessage",
33-
"contextMenus",
34-
]);
32+
let excludedNameSet = new Set(
33+
injectAllFramesFns([
34+
"onClick",
35+
"onInstalled",
36+
"onStartup",
37+
"onMessage",
38+
"contextMenus",
39+
])
40+
);
3541
for (let context of CONTEXTS) {
36-
if (hasChildFunction(script[context], excludedNamesSet)) return true;
42+
if (hasChildFunction(script[context], excludedNameSet)) return true;
3743
}
3844
return false;
3945
};
4046

47+
// input ["onClick", "abc"] => output ["onClick", "onClick_", "abc", "abc_"]
48+
const injectAllFramesFns = (fns) => fns.map((key) => [key, key + "_"]).flat();
49+
4150
export const isTitle = (script) => !(canClick(script) || canAutoRun(script));
4251

4352
export async function viewScriptSource(script) {

popup/index.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
setActiveScript,
77
trackEvent,
88
Storage,
9-
checkWillRun,
9+
checkBlackWhiteList,
1010
runScriptInTabWithEventChain,
1111
} from "../scripts/helpers/utils.js";
1212
import { UfsGlobal } from "../scripts/content-scripts/ufs_global.js";
@@ -402,7 +402,7 @@ async function updateButtonChecker(script, checkmarkContainer, val) {
402402

403403
async function runScript(script) {
404404
let tab = await getCurrentTab();
405-
let willRun = checkWillRun(script, tab.url);
405+
let willRun = checkBlackWhiteList(script, tab.url);
406406
if (willRun) {
407407
try {
408408
recentScriptsSaver.add(script);
@@ -411,25 +411,24 @@ async function runScript(script) {
411411
if (isFunction(script.popupScript?.onClick))
412412
await script.popupScript.onClick();
413413

414-
if (isFunction(script.pageScript?.onClick))
415-
await runScriptInTabWithEventChain({
416-
target: {
417-
tabId: tab.id,
418-
},
419-
scriptIds: [script.id],
420-
eventChain: "pageScript.onClick",
421-
world: "MAIN",
422-
});
423-
424-
if (isFunction(script.contentScript?.onClick))
425-
await runScriptInTabWithEventChain({
426-
target: {
427-
tabId: tab.id,
428-
},
429-
scriptIds: [script.id],
430-
eventChain: "contentScript.onClick",
431-
world: "ISOLATED",
432-
});
414+
[
415+
["MAIN", "pageScript", "onClick", false],
416+
["MAIN", "pageScript", "onClick_", true],
417+
["ISOLATED", "contentScript", "onClick", false],
418+
["ISOLATED", "contentScript", "onClick_", true],
419+
].forEach(([world, context, func, allFrames]) => {
420+
if (isFunction(script?.[context]?.[func])) {
421+
runScriptInTabWithEventChain({
422+
target: {
423+
tabId: tab.id,
424+
...(allFrames ? { allFrames: true } : {}),
425+
},
426+
scriptIds: [script.id],
427+
eventChain: context + "." + func,
428+
world: world,
429+
});
430+
}
431+
});
433432
} catch (e) {
434433
alert("ERROR: run script " + e);
435434
}

popup/tabs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,6 @@ const tabs = [
704704
s.studocu_bypassPreview,
705705
s.studyphim_unlimited,
706706
createTitle("--- Unlock function ---", "--- Mở khoá chức năng ---"),
707-
s.removeWebLimit,
708707
s.simpleAllowCopy,
709708
s.detect_zeroWidthCharacters,
710709
s.injectScriptToWebsite,

scripts/_test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010
},
1111

1212
popupScript: {
13-
onClick: async () => {
13+
_onClick: async () => {
1414
const { UfsGlobal } = await import("./content-scripts/ufs_global.js");
1515
console.log(UfsGlobal);
1616
},
@@ -162,6 +162,14 @@ export default {
162162
},
163163

164164
contentScript: {
165+
onClick_: () => {
166+
console.log(window.location.href);
167+
},
168+
169+
onDocumentStart_: () => {
170+
console.log("____onDocumentStart");
171+
},
172+
165173
// text size in KB
166174
_onClick: () => {
167175
function formatSize(size, fixed = 0) {

scripts/background-scripts/background_script.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,19 @@ function runScriptsBackground(
170170
function checkWillRun(scriptId, context, eventChain, details) {
171171
const script = allScripts[scriptId];
172172
const s = script?.[context];
173+
174+
let beforeFnName = eventChain.split(".");
175+
let fnName = beforeFnName.pop();
176+
173177
let fn = s;
174-
eventChain.split(".").forEach((e) => {
175-
fn = fn?.[e];
176-
});
178+
beforeFnName.forEach((e) => (fn = fn?.[e]));
179+
fn = fn?.[fnName + "_"] || fn?.[fnName];
180+
177181
if (
178182
typeof fn === "function" &&
179183
(!details ||
180-
((s.runInAllFrames || details.frameType == "outermost_frame") &&
181-
utils.checkWillRun(script, details.url)))
184+
((fn.name.endsWith("_") || details.frameType == "outermost_frame") &&
185+
utils.checkBlackWhiteList(script, details.url)))
182186
)
183187
return fn;
184188

@@ -318,10 +322,9 @@ function listenNavigation() {
318322
const { tabId, frameId } = getDetailIds(details);
319323

320324
if (eventChain === "onDocumentStart") {
321-
// clear badge cache on main frame
325+
// clear badge cache on main frame load
322326
if (details.frameId === 0) CACHED.badges[tabId] = [];
323-
// inject ufs global
324-
injectUfsGlobal(tabId, frameId);
327+
injectUfsGlobal(tabId, frameId, details);
325328
}
326329

327330
runScriptsTab(eventChain, MAIN, details);
@@ -389,27 +392,27 @@ function listenMessage() {
389392
});
390393
}
391394

392-
function injectUfsGlobal(tabId, frameId) {
395+
function injectUfsGlobal(tabId, frameId, details) {
393396
[
394-
{ files: ["ufs_global.js", "content_script.js"], world: ISOLATED },
395-
{ files: ["ufs_global.js"], world: MAIN },
396-
].forEach(({ files, world }) => {
397+
[["ufs_global.js", "content_script.js"], ISOLATED],
398+
[["ufs_global.js"], MAIN],
399+
].forEach(([files, world]) => {
397400
let paths = files.map((file) => CACHED.path + "content-scripts/" + file);
398401
utils.runScriptFile({
399402
target: {
400403
tabId: tabId,
401404
frameIds: [frameId],
402405
},
403-
func: (paths, frameId, world) => {
406+
func: (paths, frameId, world, url) => {
404407
paths.forEach((path) => {
405408
import(path)
406-
.then(() => console.log("Ufs import SUCCESS", frameId, world, path))
409+
.then(() => console.log("Ufs import SUCCESS", frameId, world, url))
407410
.catch((e) =>
408-
console.error("Ufs import FAILED", frameId, world, e)
411+
console.error("Ufs import FAILED", frameId, world, url, e)
409412
);
410413
});
411414
},
412-
args: [paths, frameId, world],
415+
args: [paths, frameId, world, details.url],
413416
world: world,
414417
});
415418
});

scripts/chongLuaDao.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ export default {
4545
},
4646

4747
contentScript: {
48+
// TODO analyze all iframes
4849
onDocumentIdle: (details) => {
49-
if (details.frameType === "outermost_frame") analyzeWeb(true);
50+
analyzeWeb(true);
5051
},
5152

5253
onClick: () => analyzeWeb(false),
5354
},
5455

5556
backgroundScript: {
56-
onDocumentStart: async (details, context) => {
57+
onDocumentStart_: async (details, context) => {
5758
if (!cached) await saveBgCache();
5859
if (!cached) return;
5960

@@ -98,7 +99,6 @@ export default {
9899
});
99100
}
100101
},
101-
runInAllFrames: true,
102102

103103
runtime: {
104104
onInStalled: (reason, context) => {

scripts/helpers/utils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const CACHED = {
99
userID: null,
1010
};
1111

12-
export function checkWillRun(script, url = location?.href) {
12+
export function checkBlackWhiteList(script, url = location?.href) {
1313
if (!url) return false;
1414
let hasWhiteList = script.whiteList?.length > 0;
1515
let hasBlackList = script.blackList?.length > 0;
@@ -293,9 +293,12 @@ export const runScriptInTabWithEventChain = ({
293293
import(url)
294294
.then(({ default: script }) => {
295295
let fn = script;
296-
eventChain.split(".").forEach((e) => {
297-
fn = fn?.[e];
298-
});
296+
297+
let beforeFnName = eventChain.split(".");
298+
let fnName = beforeFnName.pop();
299+
beforeFnName.forEach((e) => (fn = fn?.[e]));
300+
fn = fn?.[fnName + "_"] || fn?.[fnName]; // higher priority for allframes function
301+
299302
if (typeof fn === "function") {
300303
if (!silent)
301304
console.log(

scripts/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ import magnify_image from "./magnify_image.js";
148148
import auto_redirectLargestImageSrc from "./auto_redirectLargestImageSrc.js";
149149
import textToQrCode from "./textToQrCode.js";
150150
import insta_anonymousStoryViewer from "./insta_anonymousStoryViewer.js";
151-
import removeWebLimit from "./removeWebLimit.js";
152151
import _ufs_statistic from "./_ufs_statistic.js";
153152
import pip_fullWebsite from "./pip_fullWebsite.js";
154153
import similarWeb_bypassLimit from "./similarWeb_bypassLimit.js";
@@ -327,7 +326,6 @@ const allScripts = {
327326
),
328327
textToQrCode: addBadge(textToQrCode, BADGES.new),
329328
insta_anonymousStoryViewer: addBadge(insta_anonymousStoryViewer, BADGES.new),
330-
removeWebLimit: addBadge(removeWebLimit, BADGES.hot, BADGES.new),
331329
_ufs_statistic: _ufs_statistic,
332330
pip_fullWebsite: addBadge(pip_fullWebsite, BADGES.new),
333331
similarWeb_bypassLimit: addBadge(similarWeb_bypassLimit, BADGES.new),

0 commit comments

Comments
 (0)