From 9f238b167b8324c791d43b1d50501b785f3580ff Mon Sep 17 00:00:00 2001 From: Ahmad Kholid Date: Fri, 27 May 2022 06:56:45 +0800 Subject: [PATCH] fix: workflow not execute --- src/background/index.js | 6 +-- .../blocksHandler/handlerActiveTab.js | 4 -- .../blocksHandler/handlerSwitchTo.js | 4 +- .../blocksHandler/handlerTrigger.js | 23 +++------ .../workflowEngine/executeContentScript.js | 51 ------------------- src/background/workflowEngine/helper.js | 27 +++++++--- src/background/workflowEngine/worker.js | 6 +-- .../blocksHandler/handlerJavascriptCode.js | 4 -- src/content/index.js | 3 ++ src/content/services/shortcutListener.js | 4 +- src/content/utils.js | 5 +- src/manifest.chrome.json | 5 +- src/manifest.firefox.json | 5 +- src/newtab/App.vue | 10 +++- src/popup/pages/Home.vue | 2 + webpack.config.js | 8 --- 16 files changed, 52 insertions(+), 115 deletions(-) delete mode 100644 src/background/workflowEngine/executeContentScript.js diff --git a/src/background/index.js b/src/background/index.js index ddc9b3b1d..889a7faee 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -151,7 +151,7 @@ async function checkVisitWebTriggers(tabId, tabUrl) { state.tabIds.includes(tabId) ); const visitWebTriggers = await storage.get('visitWebTriggers'); - const triggeredWorkflow = visitWebTriggers.find(({ url, isRegex, id }) => { + const triggeredWorkflow = visitWebTriggers?.find(({ url, isRegex, id }) => { if (url.trim() === '') return false; const matchUrl = tabUrl.match(isRegex ? new RegExp(url, 'g') : url); @@ -421,8 +421,6 @@ message.on('collection:execute', (collection) => { message.on('workflow:execute', (workflowData) => { workflow.execute(workflowData, workflowData?.options || {}); }); -message.on('workflow:stop', async (id) => { - await workflow.states.stop(id); -}); +message.on('workflow:stop', (id) => workflow.states.stop(id)); browser.runtime.onMessage.addListener(message.listener()); diff --git a/src/background/workflowEngine/blocksHandler/handlerActiveTab.js b/src/background/workflowEngine/blocksHandler/handlerActiveTab.js index 0df09099a..1bcc08064 100644 --- a/src/background/workflowEngine/blocksHandler/handlerActiveTab.js +++ b/src/background/workflowEngine/blocksHandler/handlerActiveTab.js @@ -1,6 +1,5 @@ import browser from 'webextension-polyfill'; import { getBlockConnection } from '../helper'; -import executeContentScript from '../executeContentScript'; async function activeTab(block) { const nextBlockId = getBlockConnection(block); @@ -29,11 +28,8 @@ async function activeTab(block) { throw error; } - const frames = await executeContentScript(tab.id); - this.activeTab = { ...this.activeTab, - frames, frameId: 0, id: tab.id, url: tab.url, diff --git a/src/background/workflowEngine/blocksHandler/handlerSwitchTo.js b/src/background/workflowEngine/blocksHandler/handlerSwitchTo.js index 1372bc373..71266335e 100644 --- a/src/background/workflowEngine/blocksHandler/handlerSwitchTo.js +++ b/src/background/workflowEngine/blocksHandler/handlerSwitchTo.js @@ -1,6 +1,5 @@ import { objectHasKey } from '@/utils/helper'; -import { getBlockConnection } from '../helper'; -import executeContentScript, { getFrames } from '../executeContentScript'; +import { getBlockConnection, getFrames } from '../helper'; async function switchTo(block) { const nextBlockId = getBlockConnection(block); @@ -35,7 +34,6 @@ async function switchTo(block) { if (objectHasKey(frames, url)) { this.activeTab.frameId = frames[url]; - await executeContentScript(this.activeTab.id, this.activeTab.frameId); await new Promise((resolve) => setTimeout(resolve, 1000)); return { diff --git a/src/background/workflowEngine/blocksHandler/handlerTrigger.js b/src/background/workflowEngine/blocksHandler/handlerTrigger.js index 27fc858f5..1941578a2 100644 --- a/src/background/workflowEngine/blocksHandler/handlerTrigger.js +++ b/src/background/workflowEngine/blocksHandler/handlerTrigger.js @@ -1,21 +1,14 @@ import { getBlockConnection } from '../helper'; -import executeContentScript from '../executeContentScript'; async function trigger(block) { - const nextBlockId = getBlockConnection(block); - - try { - if (block.data.type === 'visit-web' && this.activeTab.id) { - await executeContentScript(this.activeTab.id); - } - - return { nextBlockId, data: '' }; - } catch (error) { - const errorInstance = new Error(error); - errorInstance.nextBlockId = nextBlockId; - - throw errorInstance; - } + return new Promise((resolve) => { + const nextBlockId = getBlockConnection(block); + + resolve({ + data: '', + nextBlockId, + }); + }); } export default trigger; diff --git a/src/background/workflowEngine/executeContentScript.js b/src/background/workflowEngine/executeContentScript.js deleted file mode 100644 index fb19a96dc..000000000 --- a/src/background/workflowEngine/executeContentScript.js +++ /dev/null @@ -1,51 +0,0 @@ -import browser from 'webextension-polyfill'; - -export async function getFrames(tabId) { - try { - const frames = await browser.webNavigation.getAllFrames({ tabId }); - const framesObj = frames.reduce((acc, { frameId, url }) => { - const key = url === 'about:blank' ? '' : url; - - acc[key] = frameId; - - return acc; - }, {}); - - return framesObj; - } catch (error) { - console.error(error); - return {}; - } -} - -async function contentScriptExist(tabId, frameId = 0) { - try { - await browser.tabs.sendMessage( - tabId, - { type: 'content-script-exists' }, - { frameId } - ); - - return true; - } catch (error) { - return false; - } -} - -export default async function (tabId, frameId = 0) { - try { - const currentFrameId = typeof frameId !== 'number' ? 0 : frameId; - const isScriptExists = await contentScriptExist(tabId, currentFrameId); - - if (!isScriptExists) { - await browser.tabs.executeScript(tabId, { - allFrames: true, - runAt: 'document_end', - file: './contentScript.bundle.js', - }); - } - } catch (error) { - console.error(error); - throw error; - } -} diff --git a/src/background/workflowEngine/helper.js b/src/background/workflowEngine/helper.js index bf09b76c2..849c1fa38 100644 --- a/src/background/workflowEngine/helper.js +++ b/src/background/workflowEngine/helper.js @@ -1,5 +1,23 @@ import browser from 'webextension-polyfill'; +export async function getFrames(tabId) { + try { + const frames = await browser.webNavigation.getAllFrames({ tabId }); + const framesObj = frames.reduce((acc, { frameId, url }) => { + const key = url === 'about:blank' ? '' : url; + + acc[key] = frameId; + + return acc; + }, {}); + + return framesObj; + } catch (error) { + console.error(error); + return {}; + } +} + export function sendDebugCommand(tabId, method, params = {}) { return new Promise((resolve) => { chrome.debugger.sendCommand({ tabId }, method, params, resolve); @@ -19,20 +37,17 @@ export function attachDebugger(tabId, prevTab) { export function waitTabLoaded(tabId, ms = 10000) { return new Promise((resolve, reject) => { - const timeout = null; - let isResolved = false; + let timeout = null; const onErrorOccurred = (details) => { if (details.tabId !== tabId || details.error.includes('ERR_ABORTED')) return; - isResolved = true; browser.webNavigation.onErrorOccurred.removeListener(onErrorOccurred); reject(new Error(details.error)); }; if (ms > 0) { - setTimeout(() => { - isResolved = true; + timeout = setTimeout(() => { browser.webNavigation.onErrorOccurred.removeListener(onErrorOccurred); reject(new Error('Timeout')); }, ms); @@ -41,8 +56,6 @@ export function waitTabLoaded(tabId, ms = 10000) { browser.webNavigation.onErrorOccurred.addListener(onErrorOccurred); const activeTabStatus = () => { - if (isResolved) return; - browser.tabs.get(tabId).then((tab) => { if (!tab) { reject(new Error('no-tab')); diff --git a/src/background/workflowEngine/worker.js b/src/background/workflowEngine/worker.js index f887285e1..dc75f973d 100644 --- a/src/background/workflowEngine/worker.js +++ b/src/background/workflowEngine/worker.js @@ -5,7 +5,6 @@ import { toCamelCase, sleep, objectHasKey, isObject } from '@/utils/helper'; import { tasks } from '@/utils/shared'; import referenceData from '@/utils/referenceData'; import { convertData, waitTabLoaded, getBlockConnection } from './helper'; -import executeContentScript from './executeContentScript'; class Worker { constructor(engine) { @@ -315,10 +314,6 @@ class Worker { this.activeTab.id, this.settings?.tabLoadTimeout ?? 30000 ); - await executeContentScript( - this.activeTab.id, - this.activeTab.frameId || 0 - ); const { executedBlockOnWeb, debugMode } = this.settings; const messagePayload = { @@ -338,6 +333,7 @@ class Worker { return data; } catch (error) { + console.error(error); if (error.message?.startsWith('Could not establish connection')) { error.message = 'Could not establish connection to the active tab'; } else if (error.message?.startsWith('No tab')) { diff --git a/src/content/blocksHandler/handlerJavascriptCode.js b/src/content/blocksHandler/handlerJavascriptCode.js index caf6bcbc6..f008d995a 100644 --- a/src/content/blocksHandler/handlerJavascriptCode.js +++ b/src/content/blocksHandler/handlerJavascriptCode.js @@ -107,12 +107,8 @@ function javascriptCode(block) { if (!block.data.everyNewTab) { let timeout; - let isResolved = false; const cleanUp = (detail = {}) => { - if (isResolved) return; - isResolved = true; - script.remove(); preloadScripts.forEach((item) => { if (item.removeAfterExec) item.script.remove(); diff --git a/src/content/index.js b/src/content/index.js index 9ec6a063a..e4dfe8e36 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -3,6 +3,7 @@ import { toCamelCase } from '@/utils/helper'; import blocksHandler from './blocksHandler'; import showExecutedBlock from './showExecutedBlock'; import handleTestCondition from './handleTestCondition'; +import shortcutListener from './services/shortcutListener'; const isMainFrame = window.self === window.top; @@ -131,6 +132,8 @@ function messageListener({ data, source }) { window.isAutomaInjected = true; window.addEventListener('message', messageListener); + if (isMainFrame) shortcutListener(); + browser.runtime.onMessage.addListener((data) => { return new Promise((resolve, reject) => { if (data.isBlock) { diff --git a/src/content/services/shortcutListener.js b/src/content/services/shortcutListener.js index 5ebe55e59..cbbefa55f 100644 --- a/src/content/services/shortcutListener.js +++ b/src/content/services/shortcutListener.js @@ -56,7 +56,7 @@ function workflowShortcutsListener(findWorkflow, shortcutsObj) { }); } -(async () => { +export default async function () { try { const { shortcuts, workflows, workflowHosts } = await browser.storage.local.get([ @@ -83,4 +83,4 @@ function workflowShortcutsListener(findWorkflow, shortcutsObj) { } catch (error) { console.error(error); } -})(); +} diff --git a/src/content/utils.js b/src/content/utils.js index 27c330506..be7676bcb 100644 --- a/src/content/utils.js +++ b/src/content/utils.js @@ -99,13 +99,11 @@ function automaRefData(keyword, path = '') { function messageTopFrame(windowCtx) { return new Promise((resolve) => { let timeout = null; - let isResolved = false; const messageListener = ({ data }) => { - if (data.type !== 'automa:the-frame-rect' || isResolved) return; + if (data.type !== 'automa:the-frame-rect') return; clearTimeout(timeout); - isResolved = true; windowCtx.removeEventListener('message', messageListener); resolve(data.frameRect); }; @@ -113,7 +111,6 @@ function messageTopFrame(windowCtx) { timeout = setTimeout(() => { if (isResolved) return; - isResolved = true; windowCtx.removeEventListener('message', messageListener); resolve(null); }, 5000); diff --git a/src/manifest.chrome.json b/src/manifest.chrome.json index 3e47c1538..92933a510 100644 --- a/src/manifest.chrome.json +++ b/src/manifest.chrome.json @@ -29,10 +29,9 @@ "" ], "js": [ - "shortcutListener.bundle.js" + "contentScript.bundle.js" ], - "run_at": "document_end", - "all_frames": false + "all_frames": true }, { "matches": [ diff --git a/src/manifest.firefox.json b/src/manifest.firefox.json index b7828f0e5..52cfe336f 100644 --- a/src/manifest.firefox.json +++ b/src/manifest.firefox.json @@ -34,10 +34,9 @@ "" ], "js": [ - "shortcutListener.bundle.js" + "contentScript.bundle.js" ], - "run_at": "document_end", - "all_frames": false + "all_frames": true }, { "matches": [ diff --git a/src/newtab/App.vue b/src/newtab/App.vue index 784132fdc..6c3000fe0 100644 --- a/src/newtab/App.vue +++ b/src/newtab/App.vue @@ -297,11 +297,17 @@ window.addEventListener('beforeunload', () => { (async () => { try { - checkModal(); - const { isFirstTime } = await browser.storage.local.get('isFirstTime'); isUpdated.value = !isFirstTime && compare(currentVersion, prevVersion, '>'); + if (isFirstTime) { + modalState.show = false; + localStorage.setItem('has-testimonial', true); + localStorage.setItem('has-survey', Date.now()); + } else { + checkModal(); + } + await Promise.allSettled([ store.dispatch('retrieve', ['workflows', 'logs', 'collections']), store.dispatch('retrieveWorkflowState'), diff --git a/src/popup/pages/Home.vue b/src/popup/pages/Home.vue index a1b001e52..bf3cc5839 100644 --- a/src/popup/pages/Home.vue +++ b/src/popup/pages/Home.vue @@ -216,6 +216,8 @@ async function initElementSelector() { await browser.tabs.sendMessage(tab.id, { type: 'automa-element-selector', }); + + window.close(); } catch (error) { if (error.message.includes('Could not establish connection.')) { await browser.tabs.executeScript(tab.id, { diff --git a/webpack.config.js b/webpack.config.js index 3004fe4f4..67e69e808 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -51,13 +51,6 @@ const options = { 'recordWorkflow', 'index.js' ), - shortcutListener: path.join( - __dirname, - 'src', - 'content', - 'services', - 'shortcutListener.js' - ), webService: path.join( __dirname, 'src', @@ -79,7 +72,6 @@ const options = { 'contentScript', 'recordWorkflow', 'elementSelector', - 'shortcutListener', ], }, output: {