Skip to content

Commit 99a4262

Browse files
committed
fix load inline script
1 parent 427f089 commit 99a4262

2 files changed

Lines changed: 45 additions & 27 deletions

File tree

‎popup/index.js‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,29 @@ import {
4848
// Cache for dynamically loaded scripts
4949
const scriptCache = new Map();
5050

51+
function isInlineOnlyScript(scriptOrId) {
52+
const scriptId = typeof scriptOrId === "string" ? scriptOrId : scriptOrId?.id;
53+
return typeof scriptOrId === "object" && scriptOrId?._isInlineOnly === true;
54+
}
55+
5156
/**
5257
* Lazy load a full script when needed (on-demand loading)
53-
* @param {string} scriptId - The script ID
58+
* @param {string|Object} scriptOrId - The script ID or inline script object
5459
* @returns {Promise<Object>} The full script object with all functions
5560
*/
56-
async function loadFullScript(scriptId) {
61+
async function loadFullScript(scriptOrId) {
62+
const scriptId = typeof scriptOrId === "string" ? scriptOrId : scriptOrId?.id;
63+
5764
// Return from cache if already loaded
5865
if (scriptCache.has(scriptId)) {
5966
return scriptCache.get(scriptId);
6067
}
6168

69+
if (isInlineOnlyScript(scriptOrId)) {
70+
scriptCache.set(scriptId, scriptOrId);
71+
return scriptOrId;
72+
}
73+
6274
try {
6375
// Dynamic import - only loads this one script
6476
const module = await import(`../scripts/${scriptId}.js`);
@@ -196,7 +208,7 @@ async function createTabContent(tab) {
196208
tab.placeholder || {
197209
en: `<i class="fa-solid fa-circle-info"></i> Nothing here yet...`,
198210
vi: `<i class="fa-solid fa-circle-info"></i> Chưa có gì ở đây hết...`,
199-
}
211+
},
200212
);
201213
contentContainer.appendChild(emptyText);
202214
} else {
@@ -283,7 +295,7 @@ function createScriptButton(script, isFavorite = false) {
283295
<li>Turn on/off autorun by click the left checkmark. </li>
284296
<li>Then reload the webpage. </li>
285297
`,
286-
})
298+
}),
287299
);
288300
};
289301
} else {
@@ -293,7 +305,7 @@ function createScriptButton(script, isFavorite = false) {
293305
t({
294306
vi: "Chức năng chưa hoàn thành " + script.id,
295307
en: "Coming soon " + script.id,
296-
})
308+
}),
297309
);
298310
};
299311
}
@@ -361,9 +373,9 @@ function createScriptButton(script, isFavorite = false) {
361373
e.preventDefault();
362374
trackEvent(script.id + "-BUTTON-" + title);
363375

364-
// Special case: infoLink button has inline onClick
365-
if (btnConfig._isInfoLink && btnConfig.onClick) {
366-
btnConfig.onClick();
376+
// Special case: inline popup-only buttons do not have script files.
377+
if (typeof btnConfig.onClick === "function") {
378+
await btnConfig.onClick();
367379
return;
368380
}
369381

@@ -382,13 +394,13 @@ function createScriptButton(script, isFavorite = false) {
382394
await fullBtnConfig.onClick();
383395
} else {
384396
console.error(
385-
`Button onClick not found in script ${script.id} at index ${fullScriptBtnIndex}`
397+
`Button onClick not found in script ${script.id} at index ${fullScriptBtnIndex}`,
386398
);
387399
}
388400
} catch (error) {
389401
console.error(
390402
`Failed to execute button onClick for ${script.id}:`,
391-
error
403+
error,
392404
);
393405
}
394406
};
@@ -404,7 +416,7 @@ function createScriptButton(script, isFavorite = false) {
404416
t({
405417
en: "View script source",
406418
vi: "Xem mã nguồn",
407-
})
419+
}),
408420
);
409421
viewSourceBtn.setAttribute("data-flow", "left");
410422
viewSourceBtn.onclick = (e) => {
@@ -415,7 +427,9 @@ function createScriptButton(script, isFavorite = false) {
415427
trackEvent(script.id + "-VIEW-SOURCE");
416428
viewScriptSource(script);
417429
};
418-
more.appendChild(viewSourceBtn);
430+
if (!isInlineOnlyScript(script)) {
431+
more.appendChild(viewSourceBtn);
432+
}
419433

420434
// add to favorite button
421435
const addFavoriteBtn = document.createElement("div");
@@ -521,7 +535,7 @@ function updateFavBtn(btn, isFavorite) {
521535
: t({
522536
en: "Add to farovite",
523537
vi: "Thêm vào yêu thích",
524-
})
538+
}),
525539
);
526540
}
527541

@@ -577,7 +591,7 @@ function checkIsPreview(script) {
577591
if (res.isConfirmed) {
578592
window.open(
579593
"https://www.facebook.com/groups/1154059318582088/posts/1453443235310360/",
580-
"_blank"
594+
"_blank",
581595
);
582596
}
583597
});
@@ -593,7 +607,7 @@ async function runScript(scriptMetadata) {
593607
let willRun = checkBlackWhiteList(scriptMetadata, tab.url);
594608

595609
// ⚡ LAZY LOAD: Load full script on-demand
596-
const script = await loadFullScript(scriptMetadata.id);
610+
const script = await loadFullScript(scriptMetadata);
597611

598612
if (willRun) {
599613
recentScriptsSaver.add(scriptMetadata);
@@ -653,7 +667,7 @@ async function runScript(scriptMetadata) {
653667
en: `Script not supported in current website`,
654668
vi: `Script không hỗ trợ website hiện tại`,
655669
}),
656-
text
670+
text,
657671
);
658672
}
659673
}
@@ -667,11 +681,11 @@ async function runScript(scriptMetadata) {
667681
function initTooltip() {
668682
settingsBtn.setAttribute(
669683
"data-tooltip",
670-
t({ vi: "Cài đặt", en: "Settings" })
684+
t({ vi: "Cài đặt", en: "Settings" }),
671685
);
672686
openInNewTabBtn.setAttribute(
673687
"data-tooltip",
674-
t({ vi: "Mở trong cửa sổ mới", en: "Open in new window" })
688+
t({ vi: "Mở trong cửa sổ mới", en: "Open in new window" }),
675689
);
676690
}
677691

@@ -749,7 +763,7 @@ function initSettings() {
749763
(key) =>
750764
`<option value="${key}" ${key === curLang ? "selected" : ""}>
751765
${LANG[key]}
752-
</option>`
766+
</option>`,
753767
).join("")}
754768
</select>
755769
</div>
@@ -830,7 +844,7 @@ function initSettings() {
830844
const checkbox = smoothScrollRow.querySelector("button");
831845
checkbox.classList.toggle(
832846
"active",
833-
!(disableSmoothScrollSaver.get() ?? false)
847+
!(disableSmoothScrollSaver.get() ?? false),
834848
);
835849
checkbox.onclick = async () => {
836850
let curVal = disableSmoothScrollSaver.get();
@@ -869,7 +883,7 @@ function initSettings() {
869883
</button>
870884
`;
871885
const [backupBtn, restoreBtn] = Array.from(
872-
backupRestoreRow.querySelectorAll("button")
886+
backupRestoreRow.querySelectorAll("button"),
873887
);
874888
backupBtn.onclick = backup;
875889
restoreBtn.onclick = restore;
@@ -905,7 +919,7 @@ function initSettings() {
905919
</button>
906920
`;
907921
const [reloadBtn, resetBtn] = Array.from(
908-
resetRow.querySelectorAll("button")
922+
resetRow.querySelectorAll("button"),
909923
);
910924
reloadBtn.onclick = reload;
911925
resetBtn.onclick = reset;
@@ -916,7 +930,7 @@ function initSettings() {
916930
en: "Settings",
917931
vi: "Cài đặt",
918932
}),
919-
body
933+
body,
920934
);
921935
};
922936
}
@@ -959,7 +973,7 @@ async function restore() {
959973
t({
960974
vi: " chức năng Tự động khoá trang web",
961975
en: " feature Auto lock websites",
962-
})
976+
}),
963977
))
964978
)
965979
return;
@@ -1053,7 +1067,7 @@ async function reset() {
10531067
t({
10541068
vi: " chức năng Tự động khoá trang web",
10551069
en: " feature Auto lock websites",
1056-
})
1070+
}),
10571071
))
10581072
)
10591073
return;
@@ -1158,13 +1172,13 @@ async function initShowDonate() {
11581172
Storage.set("clickedDonate", true);
11591173
window.open(
11601174
"https://hoangtran0410.github.io/HoangTran0410/DONATE",
1161-
"_blank"
1175+
"_blank",
11621176
);
11631177
}
11641178
if (res.isDenied) {
11651179
window.open(
11661180
"https://github.com/Useful-Scripts-Extension/useful-script",
1167-
"_blank"
1181+
"_blank",
11681182
);
11691183
}
11701184
}

‎popup/recommend.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,3 +919,7 @@ export const Recommend = {
919919
},
920920
},
921921
};
922+
923+
Object.values(Recommend).forEach((script) => {
924+
script._isInlineOnly = true;
925+
});

0 commit comments

Comments
 (0)