Skip to content

Commit

Permalink
fix: run content scripts only once
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Jun 22, 2024
1 parent 26252fe commit 40aa16f
Show file tree
Hide file tree
Showing 55 changed files with 253 additions and 146 deletions.
18 changes: 6 additions & 12 deletions src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

Expand Down Expand Up @@ -1043,20 +1037,20 @@ 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});
await executeScript({files: [`/src/engines/${engine}/script.js`], tabId});
}

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'],
Expand Down
12 changes: 4 additions & 8 deletions src/base/main.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -156,4 +150,6 @@ function main() {
}
}

main();
if (runOnce('baseModule')) {
main();
}
12 changes: 7 additions & 5 deletions src/content/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
5 changes: 4 additions & 1 deletion src/engines/123rf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {validateUrl} from 'utils/app';
import {runOnce} from 'utils/common';
import {initSearch, prepareImageForUpload, sendReceipt} from 'utils/engines';

const engine = '123rf';
Expand Down Expand Up @@ -45,4 +46,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/adobestock.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,4 +20,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/alamy.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,4 +20,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/alibabaChina.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -28,4 +28,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/ascii2d.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -18,4 +18,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/auDesign.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -27,4 +27,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/auTrademark.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -40,4 +40,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/baidu.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -76,4 +76,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/bing.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {findNode, isMobile} from 'utils/common';
import {findNode, isMobile, runOnce} from 'utils/common';
import {
initSearch,
prepareImageForUpload,
Expand Down Expand Up @@ -59,4 +59,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/branddb.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -33,4 +33,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/clipretrieval.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,4 +20,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/depositphotos.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -28,4 +28,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
7 changes: 5 additions & 2 deletions src/engines/dreamstime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -39,4 +40,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/enterpix.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,4 +24,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/esearch.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,4 +22,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/freepik.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,4 +20,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/getty.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -29,4 +29,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/google.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {findNode} from 'utils/common';
import {findNode, runOnce} from 'utils/common';
import {initSearch, sendReceipt} from 'utils/engines';

const engine = 'google';
Expand Down Expand Up @@ -35,4 +35,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/googleLens.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -62,4 +62,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/haveibeentrained.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -18,4 +18,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
6 changes: 4 additions & 2 deletions src/engines/icons8.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,4 +21,6 @@ function init() {
initSearch(search, engine, taskId);
}

init();
if (runOnce('search')) {
init();
}
Loading

0 comments on commit 40aa16f

Please sign in to comment.