diff --git a/src/background/main.js b/src/background/main.js index 986aedb..74c09dd 100644 --- a/src/background/main.js +++ b/src/background/main.js @@ -712,13 +712,7 @@ async function openContentView(message, view) { return; } - const [isContentModule] = await executeScript({ - func: () => typeof initContent !== 'undefined', - code: `typeof initContent !== 'undefined'`, - tabId - }); - - if (!isContentModule) { + if (!(await hasModule({tabId, module: 'content'}))) { await executeScript({files: ['/src/content/script.js'], tabId}); } @@ -1043,7 +1037,7 @@ async function execEngine(tabId, engine, taskId) { await executeScript({ func: taskId => (self.taskId = taskId), args: [taskId], - code: `var taskId = '${taskId}';`, + code: `self.taskId = '${taskId}'`, tabId }); await executeScript({files: ['/src/commons-engine/script.js'], tabId}); @@ -1051,12 +1045,12 @@ async function execEngine(tabId, engine, taskId) { } async function searchClickTarget(session) { - const [isParseModule] = await executeScript({ - func: () => typeof initParse !== 'undefined', - code: `typeof initParse !== 'undefined'`, + const isParseModule = await hasModule({ tabId: session.sourceTabId, - frameIds: [session.sourceFrameId] + frameId: session.sourceFrameId, + module: 'parse' }); + if (!isParseModule) { await executeScript({ files: ['/src/parse/script.js'], diff --git a/src/base/main.js b/src/base/main.js index 1a81b2b..afb39a9 100644 --- a/src/base/main.js +++ b/src/base/main.js @@ -1,14 +1,8 @@ import storage from 'storage/storage'; import {isIncomingShareContext, processIncomingShare} from 'utils/app'; +import {runOnce} from 'utils/common'; function main() { - // Script may be injected multiple times. - if (self.baseModule) { - return; - } else { - self.baseModule = true; - } - self.touchTarget = { node: null, dx: 0, @@ -156,4 +150,6 @@ function main() { } } -main(); +if (runOnce('baseModule')) { + main(); +} diff --git a/src/content/main.js b/src/content/main.js index cbd0133..cbaeab5 100644 --- a/src/content/main.js +++ b/src/content/main.js @@ -5,10 +5,10 @@ import { getAppTheme, addThemeListener } from 'utils/app'; -import {addCssClass} from 'utils/common'; +import {addCssClass, runOnce} from 'utils/common'; import {targetEnv} from 'utils/config'; -var contentStorage = { +const contentStorage = { viewFrame: null, viewFrameId: 0, viewMessagePort: null, @@ -163,13 +163,15 @@ async function configTheme(viewFrame) { await setTheme(); } -self.initContent = function () { +function main() { addViewFrame(); browser.runtime.onMessage.addListener(onMessage); if (targetEnv !== 'firefox') { browser.runtime.onConnect.addListener(onConnect); } -}; +} -initContent(); +if (runOnce('contentModule')) { + main(); +} diff --git a/src/engines/123rf.js b/src/engines/123rf.js index c70954a..baddc10 100644 --- a/src/engines/123rf.js +++ b/src/engines/123rf.js @@ -1,4 +1,5 @@ import {validateUrl} from 'utils/app'; +import {runOnce} from 'utils/common'; import {initSearch, prepareImageForUpload, sendReceipt} from 'utils/engines'; const engine = '123rf'; @@ -45,4 +46,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/adobestock.js b/src/engines/adobestock.js index 6deb9d8..1e04dc4 100644 --- a/src/engines/adobestock.js +++ b/src/engines/adobestock.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'adobestock'; @@ -20,4 +20,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/alamy.js b/src/engines/alamy.js index 261822d..1ac94e5 100644 --- a/src/engines/alamy.js +++ b/src/engines/alamy.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'alamy'; @@ -20,4 +20,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/alibabaChina.js b/src/engines/alibabaChina.js index dc9123f..d5071ba 100644 --- a/src/engines/alibabaChina.js +++ b/src/engines/alibabaChina.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'alibabaChina'; @@ -28,4 +28,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/ascii2d.js b/src/engines/ascii2d.js index 4bd120d..8bf6c11 100644 --- a/src/engines/ascii2d.js +++ b/src/engines/ascii2d.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'ascii2d'; @@ -18,4 +18,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/auDesign.js b/src/engines/auDesign.js index f4e93f3..ac4fce3 100644 --- a/src/engines/auDesign.js +++ b/src/engines/auDesign.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'auDesign'; @@ -27,4 +27,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/auTrademark.js b/src/engines/auTrademark.js index 9a4354e..650277b 100644 --- a/src/engines/auTrademark.js +++ b/src/engines/auTrademark.js @@ -1,4 +1,4 @@ -import {findNode, processNode} from 'utils/common'; +import {findNode, processNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'auTrademark'; @@ -40,4 +40,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/baidu.js b/src/engines/baidu.js index 10cb1f0..78ebd9c 100644 --- a/src/engines/baidu.js +++ b/src/engines/baidu.js @@ -1,5 +1,5 @@ import {validateUrl} from 'utils/app'; -import {findNode, isMobile} from 'utils/common'; +import {findNode, isMobile, runOnce} from 'utils/common'; import { initSearch, prepareImageForUpload, @@ -76,4 +76,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/bing.js b/src/engines/bing.js index cd15606..83fe588 100644 --- a/src/engines/bing.js +++ b/src/engines/bing.js @@ -1,4 +1,4 @@ -import {findNode, isMobile} from 'utils/common'; +import {findNode, isMobile, runOnce} from 'utils/common'; import { initSearch, prepareImageForUpload, @@ -59,4 +59,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/branddb.js b/src/engines/branddb.js index 34f58aa..5199213 100644 --- a/src/engines/branddb.js +++ b/src/engines/branddb.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'branddb'; @@ -33,4 +33,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/clipretrieval.js b/src/engines/clipretrieval.js index cb68ca8..f052068 100644 --- a/src/engines/clipretrieval.js +++ b/src/engines/clipretrieval.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'clipretrieval'; @@ -20,4 +20,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/depositphotos.js b/src/engines/depositphotos.js index abc0062..fea15c1 100644 --- a/src/engines/depositphotos.js +++ b/src/engines/depositphotos.js @@ -1,4 +1,4 @@ -import {findNode, makeDocumentVisible} from 'utils/common'; +import {findNode, makeDocumentVisible, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'depositphotos'; @@ -28,4 +28,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/dreamstime.js b/src/engines/dreamstime.js index 85041af..bef610e 100644 --- a/src/engines/dreamstime.js +++ b/src/engines/dreamstime.js @@ -2,7 +2,8 @@ import { findNode, processNode, isMobile, - executeScriptMainContext + executeScriptMainContext, + runOnce } from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; import {targetEnv} from 'utils/config'; @@ -39,4 +40,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/enterpix.js b/src/engines/enterpix.js index 6218bbb..3200f07 100644 --- a/src/engines/enterpix.js +++ b/src/engines/enterpix.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'enterpix'; @@ -24,4 +24,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/esearch.js b/src/engines/esearch.js index 7ca0a12..cc204fa 100644 --- a/src/engines/esearch.js +++ b/src/engines/esearch.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'esearch'; @@ -22,4 +22,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/freepik.js b/src/engines/freepik.js index 2785a08..0256cf9 100644 --- a/src/engines/freepik.js +++ b/src/engines/freepik.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'freepik'; @@ -20,4 +20,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/getty.js b/src/engines/getty.js index 73ec96a..cbf0694 100644 --- a/src/engines/getty.js +++ b/src/engines/getty.js @@ -1,4 +1,4 @@ -import {findNode, sleep} from 'utils/common'; +import {findNode, runOnce, sleep} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'getty'; @@ -29,4 +29,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/google.js b/src/engines/google.js index b13e0f9..39cb45f 100644 --- a/src/engines/google.js +++ b/src/engines/google.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {initSearch, sendReceipt} from 'utils/engines'; const engine = 'google'; @@ -35,4 +35,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/googleLens.js b/src/engines/googleLens.js index 9d32de5..c1d1634 100644 --- a/src/engines/googleLens.js +++ b/src/engines/googleLens.js @@ -1,4 +1,4 @@ -import {findNode, processNode, sleep} from 'utils/common'; +import {findNode, processNode, runOnce, sleep} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'googleLens'; @@ -62,4 +62,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/haveibeentrained.js b/src/engines/haveibeentrained.js index efd7768..2cf1984 100644 --- a/src/engines/haveibeentrained.js +++ b/src/engines/haveibeentrained.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'haveibeentrained'; @@ -18,4 +18,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/icons8.js b/src/engines/icons8.js index 7282ac2..573e4fa 100644 --- a/src/engines/icons8.js +++ b/src/engines/icons8.js @@ -1,4 +1,4 @@ -import {findNode, makeDocumentVisible} from 'utils/common'; +import {findNode, makeDocumentVisible, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'icons8'; @@ -21,4 +21,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/ikea.js b/src/engines/ikea.js index 5dbfe87..3242379 100644 --- a/src/engines/ikea.js +++ b/src/engines/ikea.js @@ -1,4 +1,9 @@ -import {findNode, processNode, makeDocumentVisible} from 'utils/common'; +import { + findNode, + processNode, + makeDocumentVisible, + runOnce +} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'ikea'; @@ -26,4 +31,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/immerse.js b/src/engines/immerse.js index d4fadda..3ee7d8f 100644 --- a/src/engines/immerse.js +++ b/src/engines/immerse.js @@ -1,4 +1,4 @@ -import {findNode, makeDocumentVisible, sleep} from 'utils/common'; +import {findNode, makeDocumentVisible, runOnce, sleep} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'immerse'; @@ -23,4 +23,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/iqdb.js b/src/engines/iqdb.js index 89abc73..ae8f1ce 100644 --- a/src/engines/iqdb.js +++ b/src/engines/iqdb.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'iqdb'; @@ -18,4 +18,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/istock.js b/src/engines/istock.js index 2316a75..80e2a3f 100644 --- a/src/engines/istock.js +++ b/src/engines/istock.js @@ -1,4 +1,4 @@ -import {findNode, sleep} from 'utils/common'; +import {findNode, runOnce, sleep} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'istock'; @@ -29,4 +29,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/jpDesign.js b/src/engines/jpDesign.js index 68fae97..3673339 100644 --- a/src/engines/jpDesign.js +++ b/src/engines/jpDesign.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'jpDesign'; @@ -26,4 +26,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/kagi.js b/src/engines/kagi.js index 3cf0d2f..d61535d 100644 --- a/src/engines/kagi.js +++ b/src/engines/kagi.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {initSearch, setFileInputData, sendReceipt} from 'utils/engines'; const engine = 'kagi'; @@ -47,4 +47,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/karmaDecay.js b/src/engines/karmaDecay.js index a23cb1f..12fccbc 100644 --- a/src/engines/karmaDecay.js +++ b/src/engines/karmaDecay.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'karmaDecay'; @@ -24,4 +24,6 @@ function init() { } } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/lexica.js b/src/engines/lexica.js index 5053039..bed9e7d 100644 --- a/src/engines/lexica.js +++ b/src/engines/lexica.js @@ -1,4 +1,4 @@ -import {findNode, executeScriptMainContext} from 'utils/common'; +import {findNode, executeScriptMainContext, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'lexica'; @@ -22,4 +22,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/lykdat.js b/src/engines/lykdat.js index daa3ade..14a9b1c 100644 --- a/src/engines/lykdat.js +++ b/src/engines/lykdat.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'lykdat'; @@ -20,4 +20,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/madridMonitor.js b/src/engines/madridMonitor.js index deb13c2..0236380 100644 --- a/src/engines/madridMonitor.js +++ b/src/engines/madridMonitor.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; import {targetEnv} from 'utils/config'; @@ -41,4 +41,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/nzTrademark.js b/src/engines/nzTrademark.js index 225dcee..fdb2315 100644 --- a/src/engines/nzTrademark.js +++ b/src/engines/nzTrademark.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'nzTrademark'; @@ -38,4 +38,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/pimeyes.js b/src/engines/pimeyes.js index bef1a88..bfb330d 100644 --- a/src/engines/pimeyes.js +++ b/src/engines/pimeyes.js @@ -1,4 +1,4 @@ -import {findNode, processNode} from 'utils/common'; +import {findNode, processNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'pimeyes'; @@ -55,4 +55,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/pixta.js b/src/engines/pixta.js index 96dafc8..06e8b1d 100644 --- a/src/engines/pixta.js +++ b/src/engines/pixta.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'pixta'; @@ -34,4 +34,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/pond5.js b/src/engines/pond5.js index 4718ea0..7b2d22d 100644 --- a/src/engines/pond5.js +++ b/src/engines/pond5.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'pond5'; @@ -22,4 +22,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/qihoo.js b/src/engines/qihoo.js index aa9e555..f0478f7 100644 --- a/src/engines/qihoo.js +++ b/src/engines/qihoo.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'qihoo'; @@ -27,4 +27,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/repostSleuth.js b/src/engines/repostSleuth.js index 5677006..34e9611 100644 --- a/src/engines/repostSleuth.js +++ b/src/engines/repostSleuth.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'repostSleuth'; @@ -37,4 +37,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/saucenao.js b/src/engines/saucenao.js index 3aeeeae..8e62060 100644 --- a/src/engines/saucenao.js +++ b/src/engines/saucenao.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'saucenao'; @@ -34,4 +34,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/shein.js b/src/engines/shein.js index cac63af..11d5b98 100644 --- a/src/engines/shein.js +++ b/src/engines/shein.js @@ -1,4 +1,9 @@ -import {findNode, processNode, makeDocumentVisible} from 'utils/common'; +import { + findNode, + processNode, + makeDocumentVisible, + runOnce +} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'shein'; @@ -30,4 +35,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/shutterstock.js b/src/engines/shutterstock.js index 77ef635..33ebfda 100644 --- a/src/engines/shutterstock.js +++ b/src/engines/shutterstock.js @@ -1,4 +1,4 @@ -import {findNode, isMobile} from 'utils/common'; +import {findNode, isMobile, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'shutterstock'; @@ -29,4 +29,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/sogou.js b/src/engines/sogou.js index d72f6fc..3480fd9 100644 --- a/src/engines/sogou.js +++ b/src/engines/sogou.js @@ -1,4 +1,4 @@ -import {findNode, isMobile} from 'utils/common'; +import {findNode, isMobile, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'sogou'; @@ -22,4 +22,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/stocksy.js b/src/engines/stocksy.js index 6b104b1..76dab65 100644 --- a/src/engines/stocksy.js +++ b/src/engines/stocksy.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'stocksy'; @@ -20,4 +20,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/taobao.js b/src/engines/taobao.js index 6d4d41a..7bb477d 100644 --- a/src/engines/taobao.js +++ b/src/engines/taobao.js @@ -1,4 +1,4 @@ -import {findNode, executeScriptMainContext} from 'utils/common'; +import {findNode, executeScriptMainContext, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'taobao'; @@ -27,4 +27,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/tineye.js b/src/engines/tineye.js index dc73d9e..c945d43 100644 --- a/src/engines/tineye.js +++ b/src/engines/tineye.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'tineye'; @@ -18,4 +18,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/tmview.js b/src/engines/tmview.js index 9e96a6a..a894469 100644 --- a/src/engines/tmview.js +++ b/src/engines/tmview.js @@ -1,4 +1,4 @@ -import {findNode, processNode} from 'utils/common'; +import {findNode, processNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'tmview'; @@ -35,4 +35,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/whatanime.js b/src/engines/whatanime.js index f549273..8df9aa3 100644 --- a/src/engines/whatanime.js +++ b/src/engines/whatanime.js @@ -1,4 +1,4 @@ -import {findNode} from 'utils/common'; +import {findNode, runOnce} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'whatanime'; @@ -18,4 +18,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/wildberries.js b/src/engines/wildberries.js index f513255..2ed8233 100644 --- a/src/engines/wildberries.js +++ b/src/engines/wildberries.js @@ -1,4 +1,4 @@ -import {findNode, sleep} from 'utils/common'; +import {findNode, runOnce, sleep} from 'utils/common'; import {setFileInputData, initSearch, sendReceipt} from 'utils/engines'; const engine = 'wildberries'; @@ -21,4 +21,6 @@ function init() { initSearch(search, engine, taskId); } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/engines/yandex.js b/src/engines/yandex.js index f142375..14aeff5 100644 --- a/src/engines/yandex.js +++ b/src/engines/yandex.js @@ -4,7 +4,8 @@ import {validateUrl, getContentXHR} from 'utils/app'; import { findNode, makeDocumentVisible, - executeScriptMainContext + executeScriptMainContext, + runOnce } from 'utils/common'; import { initSearch, @@ -171,4 +172,6 @@ function init() { } } -init(); +if (runOnce('search')) { + init(); +} diff --git a/src/parse/main.js b/src/parse/main.js index 4718547..c7181df 100644 --- a/src/parse/main.js +++ b/src/parse/main.js @@ -22,7 +22,8 @@ import { getBlankCanvasDataUrl, canvasToDataUrl, drawElementOnCanvas, - getAbsoluteUrl + getAbsoluteUrl, + runOnce } from 'utils/common'; import {targetEnv} from 'utils/config'; @@ -365,7 +366,7 @@ async function parseDocument({root = null, touchRect = null, session} = {}) { } async function parse(session) { - if (typeof baseModule === 'undefined') { + if (!self.runStore?.baseModule) { throw new Error('Base module missing'); } @@ -408,7 +409,7 @@ async function parse(session) { return processResults(results, session); } -self.initParse = async function (session) { +async function initParse(session) { try { const images = await parse(session); @@ -442,7 +443,7 @@ self.initParse = async function (session) { throw err; } -}; +} function onMessage(request, sender) { // Samsung Internet 13: extension messages are sometimes also dispatched @@ -456,4 +457,10 @@ function onMessage(request, sender) { } } -browser.runtime.onMessage.addListener(onMessage); +function main() { + browser.runtime.onMessage.addListener(onMessage); +} + +if (runOnce('parseModule')) { + main(); +} diff --git a/src/utils/app.js b/src/utils/app.js index f7234a4..0c48404 100755 --- a/src/utils/app.js +++ b/src/utils/app.js @@ -255,9 +255,9 @@ function getListItems(data, {scope = '', shortScope = ''} = {}) { async function hasModule({tabId, frameId = 0, module, insert = false} = {}) { try { const [isModule] = await executeScript({ - func: name => typeof self[`${name}Module`] !== 'undefined', + func: module => self.runStore?.[`${module}Module`], args: [module], - code: `typeof ${module}Module !== 'undefined'`, + code: `self.runStore?.['${module}Module']`, tabId, frameIds: [frameId] }); @@ -273,7 +273,9 @@ async function hasModule({tabId, frameId = 0, module, insert = false} = {}) { if (isModule || insert) { return true; } - } catch (err) {} + } catch (err) { + console.log(err); + } return false; } diff --git a/src/utils/common.js b/src/utils/common.js index da2fa10..922da94 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -531,13 +531,6 @@ function waitForDocumentLoad() { } function makeDocumentVisible() { - // Script may be injected multiple times. - if (self.documentVisibleModule) { - return; - } else { - self.documentVisibleModule = true; - } - const eventName = uuidv4(); function dispatchVisibilityState() { @@ -803,11 +796,21 @@ function filenameToFileExt(name) { return (/(?:\.([^.]+))?$/.exec(name)[1] || '').toLowerCase(); } -function runOnce(name, func) { - name = `${name}Run`; +function getStore(name, {content = null} = {}) { + name = `${name}Store`; if (!self[name]) { - self[name] = true; + self[name] = content || {}; + } + + return self[name]; +} + +function runOnce(name, func) { + const store = getStore('run'); + + if (!store[name]) { + store[name] = true; if (!func) { return true; @@ -868,6 +871,7 @@ export { getAbsoluteUrl, getDataFromUrl, filenameToFileExt, + getStore, runOnce, sleep }; diff --git a/src/utils/engines.js b/src/utils/engines.js index dd8daaf..a379636 100644 --- a/src/utils/engines.js +++ b/src/utils/engines.js @@ -98,16 +98,9 @@ async function sendReceipt(storageIds) { } async function initSearch(searchFn, engine, taskId) { - // Script may be injected multiple times. - if (typeof self.task === 'undefined') { - self.task = null; - } else { - return; - } - await waitForDocumentLoad(); - self.task = await browser.runtime.sendMessage({ + const task = await browser.runtime.sendMessage({ id: 'storageRequest', asyncResponse: true, storageId: taskId