MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / runScript

Function runScript

popup/index.js:589–659  ·  view source on GitHub ↗
(scriptMetadata)

Source from the content-addressed store, hash-verified

587}
588
589async function runScript(scriptMetadata) {
590 if (checkIsPreview(scriptMetadata)) return;
591
592 let tab = await getCurrentTab();
593 let willRun = checkBlackWhiteList(scriptMetadata, tab.url);
594
595 // ⚡ LAZY LOAD: Load full script on-demand
596 const script = await loadFullScript(scriptMetadata.id);
597
598 if (willRun) {
599 recentScriptsSaver.add(scriptMetadata);
600 trackEvent(scriptMetadata.id);
601
602 try {
603 if (isFunction(script.popupScript?.onClick)) {
604 await script.popupScript.onClick();
605 }
606 } catch (e) {
607 showError(e);
608 }
609
610 let isRunInTab = false;
611 [
612 ["MAIN", "pageScript", "onClick", false],
613 ["MAIN", "pageScript", "onClick_", true],
614 ["ISOLATED", "contentScript", "onClick", false],
615 ["ISOLATED", "contentScript", "onClick_", true],
616 ].forEach(([world, context, func, allFrames]) => {
617 if (isFunction(script?.[context]?.[func])) {
618 isRunInTab = true;
619 runScriptInTabWithEventChain({
620 target: {
621 tabId: tab.id,
622 ...(allFrames ? { allFrames: true } : {}),
623 },
624 scriptIds: [script.id],
625 eventChain: context + "." + func,
626 world: world,
627 }).catch(showError);
628 }
629 });
630
631 if (isInNewTab && isRunInTab) {
632 // focus to targeTab
633 await chrome.windows.update(tab.windowId, {
634 focused: true,
635 });
636 }
637 } else {
638 let text = "";
639 [
640 [script?.whiteList, t({ vi: "Chỉ chạy tại", en: "Only run at" })],
641 [script?.blackList, t({ vi: "Không chạy tại", en: "Not run at" })],
642 ].forEach(([list, title]) => {
643 if (list?.length) {
644 text += `${title}:<br/>
645 <ul>
646 ${list.map((_) => "<li><i>" + _ + "</i></li>").join("")}

Callers 1

createScriptButtonFunction · 0.85

Calls 12

getCurrentTabFunction · 0.90
checkBlackWhiteListFunction · 0.90
trackEventFunction · 0.90
isFunctionFunction · 0.90
tFunction · 0.90
openModalFunction · 0.90
checkIsPreviewFunction · 0.85
loadFullScriptFunction · 0.85
showErrorFunction · 0.85
updateMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected