Skip to content

Commit

Permalink
fix: workflow not execute
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholid060 committed May 26, 2022
1 parent 0b1d46a commit 9f238b1
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 115 deletions.
6 changes: 2 additions & 4 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 8 additions & 15 deletions src/background/workflowEngine/blocksHandler/handlerTrigger.js
Original file line number Diff line number Diff line change
@@ -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;
51 changes: 0 additions & 51 deletions src/background/workflowEngine/executeContentScript.js

This file was deleted.

27 changes: 20 additions & 7 deletions src/background/workflowEngine/helper.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand All @@ -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'));
Expand Down
6 changes: 1 addition & 5 deletions src/background/workflowEngine/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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')) {
Expand Down
4 changes: 0 additions & 4 deletions src/content/blocksHandler/handlerJavascriptCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/content/services/shortcutListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function workflowShortcutsListener(findWorkflow, shortcutsObj) {
});
}

(async () => {
export default async function () {
try {
const { shortcuts, workflows, workflowHosts } =
await browser.storage.local.get([
Expand All @@ -83,4 +83,4 @@ function workflowShortcutsListener(findWorkflow, shortcutsObj) {
} catch (error) {
console.error(error);
}
})();
}
5 changes: 1 addition & 4 deletions src/content/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,18 @@ 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);
};

timeout = setTimeout(() => {
if (isResolved) return;

isResolved = true;
windowCtx.removeEventListener('message', messageListener);
resolve(null);
}, 5000);
Expand Down
5 changes: 2 additions & 3 deletions src/manifest.chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
"<all_urls>"
],
"js": [
"shortcutListener.bundle.js"
"contentScript.bundle.js"
],
"run_at": "document_end",
"all_frames": false
"all_frames": true
},
{
"matches": [
Expand Down
5 changes: 2 additions & 3 deletions src/manifest.firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
"<all_urls>"
],
"js": [
"shortcutListener.bundle.js"
"contentScript.bundle.js"
],
"run_at": "document_end",
"all_frames": false
"all_frames": true
},
{
"matches": [
Expand Down
10 changes: 8 additions & 2 deletions src/newtab/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 2 additions & 0 deletions src/popup/pages/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
8 changes: 0 additions & 8 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ const options = {
'recordWorkflow',
'index.js'
),
shortcutListener: path.join(
__dirname,
'src',
'content',
'services',
'shortcutListener.js'
),
webService: path.join(
__dirname,
'src',
Expand All @@ -79,7 +72,6 @@ const options = {
'contentScript',
'recordWorkflow',
'elementSelector',
'shortcutListener',
],
},
output: {
Expand Down

0 comments on commit 9f238b1

Please sign in to comment.