${script.func?.toString()}`); + window.open(script.infoLink); }; - button.appendChild(viewSourceBtn); + button.appendChild(infoBtn); } + // add to favorite button + const addFavoriteBtn = document.createElement("i"); + addFavoriteBtn.className = isFavorite + ? "fa-solid fa-star star active" + : "fa-regular fa-star star"; + addFavoriteBtn.title = isFavorite + ? t({ + en: "Remove from favorite", + vi: "Xoá khỏi yêu thích", + }) + : t({ + en: "Add to farovite", + vi: "Thêm vào yêu thích", + }); + addFavoriteBtn.onclick = (e) => { + e.stopPropagation(); + e.preventDefault(); + favoriteScriptsSaver.toggle(script); + createTabs(); + }; + button.appendChild(addFavoriteBtn); + + // view source button + const viewSourceBtn = document.createElement("i"); + viewSourceBtn.title = t({ + en: "View script source", + vi: "Xem mã nguồn", + }); + viewSourceBtn.className = "fa-solid fa-code view-source"; + viewSourceBtn.onclick = (e) => { + e.stopPropagation(); + e.preventDefault(); + + viewScriptSource(script); + }; + button.appendChild(viewSourceBtn); + // tooltip const tooltip = document.createElement("span"); tooltip.classList.add("tooltiptext"); tooltip.innerText = t(script.description); button.appendChild(tooltip); - return button; + buttonContainer.appendChild(button); + return buttonContainer; +} + +function updateButtonChecker(script, button, val) { + let checkmark = button.querySelector(".checkmark"); + if (!checkmark) return; + if (val ?? isActiveScript(script.id)) { + checkmark.classList.add("active"); + checkmark.title = t({ + vi: "Tắt tự động chạy", + en: "Turn off Autorun", + }); + } else { + checkmark.classList.remove("active"); + checkmark.title = t({ + vi: "Bật tự động chạy", + en: "Turn on Autorun", + }); + } } async function runScript(script) { - let willRun = await checkBlackWhiteList(script); + let tab = await getCurrentTab(); + let willRun = checkBlackWhiteList(script, tab.url); if (willRun) { - recentScriptsSaver.add(script); - if (script.runInExtensionContext) script.func(); - else runScriptInCurrentTab(script.func); + try { + recentScriptsSaver.add(script); + updateScriptClickCount(script.id); + if (isFunction(script.onClickExtension)) await script.onClickExtension(); + if (isFunction(script.onClick)) + await runScriptInCurrentTab(script.onClick); + if (isFunction(script.onClickContentScript)) + await sendEventToTab(tab.id, { + type: MsgType.runScript, + scriptId: script.id, + }); + } catch (e) { + console.log("ERROR: run script", e); + } } else { let w = script?.whiteList?.join(", "); - let b = [...(script?.blackList || []), ...GlobalBlackList]?.join(", "); + let b = script?.blackList?.join(", "); openModal( t({ - en: `Script not supported in current website`, - vi: `Script không hỗ trợ website hiện tại`, + en: `Script not supported in current website (${tab.url})`, + vi: `Script không hỗ trợ website hiện tại (${tab.url})`, }), t({ en: @@ -269,21 +351,41 @@ async function runScript(script) { } } -function initOpenInNewTabBtn() { - if (isExtensionInSeperatedPopup()) { - document.title = "Useful Scripts"; - openInNewTabBtn.remove(); - } else { - openInNewTabBtn.onclick = () => { - openExtensionInSeparatedPopup(); - window.close(); - }; - } +function initSearch() { + searchInput.addEventListener("input", (event) => { + let keyword = event.target.value; + let found = 0; + let childrens = document + .querySelector(".tabcontent") + .querySelectorAll(".buttonContainer"); + + childrens.forEach((child) => { + let willShow = true; + let text = removeAccents(child.textContent).toLowerCase(); + let searchStr = removeAccents(keyword) + .toLowerCase() + .split(" ") + .filter((_) => _); + + for (let s of searchStr) { + if (text.indexOf(s) == -1) { + willShow = false; + break; + } + } + child.style.display = willShow ? "block" : "none"; + if (willShow) found++; + }); + searchFound.innerText = keyword + ? `${found}/${childrens.length} scripts` + : ""; + }); } (async function () { - initOpenInNewTabBtn(); - await initLanguage(); - await createTabs(); + // initOpenInNewTabBtn(); + initSearch(); + initLanguage(); + createTabs(); await checkForUpdate(); })(); diff --git a/popup/main.js b/popup/main.js new file mode 100644 index 00000000..c19f610c --- /dev/null +++ b/popup/main.js @@ -0,0 +1,3 @@ +import("./index.js").then(() => { + document.querySelector("#loading-fullscreen")?.remove(); +}); diff --git a/popup/popup.html b/popup/popup.html index 4a49d4a5..ff5200cd 100644 --- a/popup/popup.html +++ b/popup/popup.html @@ -1,10 +1,11 @@ -
Make with by Hoang Tran
-# | Hình ảnh | Chi tiết |
---|
{{ message }}
- - - - - -- Hover your mouse over me for a few seconds - to see my dynamically bound title! -
- -Now you see me
- -