Skip to content

Commit d6268f8

Browse files
committed
some optimize
1 parent 0c3657f commit d6268f8

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

scripts/_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export default {
346346
},
347347

348348
pageScript: {
349-
onClick: () => {
349+
_onClick: () => {
350350
fetchGraphQl();
351351
},
352352
_onClick: async () => {

scripts/background-scripts/background_script.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ async function runScriptsTab(eventChain, world, details, silent = false) {
8181
return;
8282
}
8383
const context = world === "MAIN" ? "pageScript" : "contentScript";
84-
const scriptIds = CACHED.activeScriptIds.filter((id) =>
85-
checkWillRun(id, context, eventChain, details)
86-
);
84+
const scriptIds = CACHED.activeScriptIds
85+
.filter((id) => checkWillRun(id, context, eventChain, details))
86+
.sort((a, b) => (a?.priority || Infinity) - (b?.priority || Infinity));
8787

8888
if (scriptIds.length === 0) return;
8989

@@ -323,15 +323,15 @@ function listenNavigation() {
323323
try {
324324
const { tabId, frameId } = getDetailIds(details);
325325

326+
runScriptsTab(eventChain, MAIN, details);
327+
runScriptsTab(eventChain, ISOLATED, details);
328+
runScriptsBackground(eventChain, details);
329+
326330
if (eventChain === "onDocumentStart") {
327331
// clear badge cache on main frame load
328332
if (details.frameId === 0) CACHED.badges[tabId] = [];
329333
injectUfsGlobal(tabId, frameId, details);
330334
}
331-
332-
runScriptsTab(eventChain, MAIN, details);
333-
runScriptsTab(eventChain, ISOLATED, details);
334-
runScriptsBackground(eventChain, details);
335335
} catch (e) {
336336
console.log("ERROR:", e);
337337
}

scripts/content-scripts/ufs_global.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const UfsGlobal = {
4848
getWatchingVideoSrc,
4949
},
5050
Utils: {
51+
waitFor,
5152
hashString,
5253
lerp,
5354
getNumberFormatter,
@@ -194,10 +195,7 @@ function checkElementvisibility(elem) {
194195
if (!elem.offsetHeight && !elem.offsetWidth) {
195196
return false;
196197
}
197-
if (getComputedStyle(elem).visibility === "hidden") {
198-
return false;
199-
}
200-
return true;
198+
return !(getComputedStyle(elem).visibility === "hidden");
201199
}
202200
function closest(element, selector) {
203201
let el = element;
@@ -927,6 +925,33 @@ function getWatchingVideoSrc() {
927925

928926
// #region Utils
929927

928+
/**
929+
* Waits for a condition to be true within a specified timeout.
930+
*
931+
* @param {function} condition - The condition to be evaluated.
932+
* @param {number} [timeout=1000] - The timeout in milliseconds.
933+
* @return {Promise} A Promise that resolves when the condition is true.
934+
*/
935+
function waitFor(condition, timeout = 0) {
936+
// return new Promise((resolve) => {
937+
// let timer = setInterval(() => {
938+
// if (condition()) {
939+
// clearInterval(timer);
940+
// resolve();
941+
// }
942+
// }, timeout);
943+
// });
944+
return new Promise(async (resolve) => {
945+
while (true) {
946+
if (condition()) {
947+
resolve();
948+
break;
949+
}
950+
await new Promise((resolve) => setTimeout(resolve, timeout));
951+
}
952+
});
953+
}
954+
930955
// https://stackoverflow.com/a/7616484/23648002
931956
function hashString(str) {
932957
let hash = 0,
@@ -1207,29 +1232,28 @@ function replaceUsingRegex(str, r, s) {
12071232
let results = [];
12081233

12091234
if (!Array.isArray(r) && !Array.isArray(s)) {
1210-
if (r && r.test && r.test(str)) {
1235+
if (r?.test?.(str)) {
12111236
results.push(str.replace(r, s));
12121237
}
12131238
} else if (!Array.isArray(r) && Array.isArray(s)) {
1214-
if (r && r.test && r.test(str)) {
1215-
for (let si = 0; si < s.length; si++) {
1216-
results.push(str.replace(r, s[si]));
1239+
if (r?.test?.(str)) {
1240+
for (const si of s) {
1241+
results.push(str.replace(r, si));
12171242
}
12181243
}
12191244
} else if (Array.isArray(r) && !Array.isArray(s)) {
1220-
for (let ri = 0; ri < r.length; ri++) {
1221-
let _r = r[ri];
1222-
if (_r && _r.test && _r.test(str)) {
1223-
results.push(str.replace(_r, s));
1245+
for (const ri of r) {
1246+
if (ri?.test?.(str)) {
1247+
results.push(str.replace(ri, s));
12241248
}
12251249
}
12261250
} else if (Array.isArray(r) && Array.isArray(s)) {
12271251
for (let ri = 0; ri < r.length; ri++) {
12281252
let _r = r[ri];
1229-
if (_r && _r.test && _r.test(str)) {
1253+
if (_r?.test?.(str)) {
12301254
let _s = Array.isArray(s[ri]) ? s[ri] : [s[ri]];
1231-
for (let si = 0; si < _s.length; si++) {
1232-
results.push(str.replace(_r, _s[si]));
1255+
for (const si of _s) {
1256+
results.push(str.replace(_r, si));
12331257
}
12341258
}
12351259
}
@@ -1240,7 +1264,7 @@ function replaceUsingRegex(str, r, s) {
12401264
function testRegex(str, regexs) {
12411265
if (!Array.isArray(regexs)) regexs = [regexs];
12421266
for (let regex of regexs) {
1243-
if (regex && regex.test && regex.test(str)) {
1267+
if (regex?.test?.(str)) {
12441268
return true;
12451269
}
12461270
}
@@ -1540,7 +1564,7 @@ async function isImageSrc(src) {
15401564
if (res?.ok) {
15411565
// const type = res.headers.get("content-type");
15421566
const type = res.headers?.["content-type"];
1543-
if (type && type.startsWith("image/")) {
1567+
if (type?.startsWith?.("image/")) {
15441568
return true;
15451569
}
15461570
}

working_note.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 30/05/2024 - ?
44

5+
- [ ] decrypt in js <https://github.com/diafygi/webcrypto-examples>
6+
57
- [ ] how tiktok make floating video? <https://developer.chrome.com/docs/web-platform/document-picture-in-picture> <https://web.swipeinsight.app/posts/tiktok-introduces-new-feature-floating-player-for-desktop-users>
68

79
- [x] preview any html file on github <https://htmlpreview.github.io/>?

0 commit comments

Comments
 (0)