Skip to content

Commit

Permalink
v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholid060 authored Mar 28, 2022
2 parents 5a9bbf3 + c1be4db commit 6557466
Show file tree
Hide file tree
Showing 57 changed files with 2,589 additions and 1,543 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "automa",
"version": "1.5.1",
"version": "1.5.4",
"description": "An extension for automating your browser by connecting blocks",
"license": "MIT",
"repository": {
Expand All @@ -23,8 +23,9 @@
},
"dependencies": {
"@codemirror/basic-setup": "^0.19.1",
"@codemirror/lang-javascript": "0.19.1",
"@codemirror/lang-json": "^0.19.1",
"@codemirror/fold": "^0.19.3",
"@codemirror/lang-javascript": "^0.19.7",
"@codemirror/lang-json": "^0.19.2",
"@codemirror/theme-one-dark": "^0.19.1",
"@medv/finder": "^2.1.0",
"@tiptap/extension-character-count": "^2.0.0-beta.24",
Expand Down
2 changes: 1 addition & 1 deletion src/background/collection-engine/flow-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataExporter from '@/utils/data-exporter';
import WorkflowEngine from '../workflow-engine/engine';
import blocksHandler from '../workflow-engine/blocks-handler';
import dataExporter from '@/utils/data-exporter';

export function workflow(flow) {
return new Promise((resolve, reject) => {
Expand Down
4 changes: 2 additions & 2 deletions src/background/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import browser from 'webextension-polyfill';
import { MessageListener } from '@/utils/message';
import { registerSpecificDay } from '../utils/workflow-trigger';
import { parseJSON, findTriggerBlock } from '@/utils/helper';
import getFile from '@/utils/get-file';
import decryptFlow, { getWorkflowPass } from '@/utils/decrypt-flow';
import { registerSpecificDay } from '../utils/workflow-trigger';
import WorkflowState from './workflow-state';
import CollectionEngine from './collection-engine';
import WorkflowEngine from './workflow-engine/engine';
import blocksHandler from './workflow-engine/blocks-handler';
import WorkflowLogger from './workflow-logger';
import decryptFlow, { getWorkflowPass } from '@/utils/decrypt-flow';

const validateUrl = (str) => str?.startsWith('http');
const storage = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ async function activeTab(block) {
};
this.windowId = tab.windowId;

if (this.preloadScripts.length > 0) {
const preloadScripts = this.preloadScripts.map((script) =>
this._sendMessageToTab(script)
);
await Promise.allSettled(preloadScripts);
}

return data;
} catch (error) {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill';
import { getBlockConnection } from '../helper';
import { isWhitespace } from '@/utils/helper';
import { getBlockConnection } from '../helper';

function handleEventListener(target, validate) {
return (data, activeTab) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async function ({ data, outputs }) {
const copiedText = textarea.value;

if (data.assignVariable) {
this.referenceData.variables[data.variableName] = copiedText;
this.setVariable(data.variableName, copiedText);
}
if (data.saveData) {
this.addDataToColumn(data.dataColumn, copiedText);
Expand Down
71 changes: 52 additions & 19 deletions src/background/workflow-engine/blocks-handler/handler-conditions.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,74 @@
import { getBlockConnection } from '../helper';
import compareBlockValue from '@/utils/compare-block-value';
import mustacheReplacer from '@/utils/reference-data/mustache-replacer';
import testConditions from '@/utils/test-conditions';
import { getBlockConnection } from '../helper';

function conditions({ data, outputs }, { prevBlockData, refData }) {
return new Promise((resolve, reject) => {
if (data.conditions.length === 0) {
reject(new Error('conditions-empty'));
return;
}
async function conditions({ data, outputs }, { prevBlockData, refData }) {
if (data.conditions.length === 0) {
throw new Error('conditions-empty');
}

let resultData = '';
let isConditionMatch = false;
let outputIndex = data.conditions.length + 1;

const replacedValue = {};
const condition = data.conditions[0];
const prevData = Array.isArray(prevBlockData)
? prevBlockData[0]
: prevBlockData;

if (condition && condition.conditions) {
const conditionPayload = {
refData,
activeTab: this.activeTab.id,
sendMessage: (payload) =>
this._sendMessageToTab({ ...payload, isBlock: false }),
};

for (let index = 0; index < data.conditions.length; index += 1) {
const result = await testConditions(
data.conditions[index].conditions,
conditionPayload
);

let resultData = '';
let isConditionMatch = false;
let outputIndex = data.conditions.length + 1;
const prevData = Array.isArray(prevBlockData)
? prevBlockData[0]
: prevBlockData;
Object.assign(replacedValue, result?.replacedValue || {});

if (result.isMatch) {
isConditionMatch = true;
outputIndex = index + 1;

break;
}
}
} else {
data.conditions.forEach(({ type, value, compareValue }, index) => {
if (isConditionMatch) return;

const firstValue = mustacheReplacer(compareValue ?? prevData, refData);
const secondValue = mustacheReplacer(value, refData);

const isMatch = compareBlockValue(type, firstValue, secondValue);
Object.assign(replacedValue, firstValue.list, secondValue.list);

const isMatch = compareBlockValue(
type,
firstValue.value,
secondValue.value
);

if (isMatch) {
resultData = value;
outputIndex = index + 1;
isConditionMatch = true;
}
});
}

resolve({
data: resultData,
nextBlockId: getBlockConnection({ outputs }, outputIndex),
});
});
return {
replacedValue,
data: resultData,
nextBlockId: getBlockConnection({ outputs }, outputIndex),
};
}

export default conditions;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import browser from 'webextension-polyfill';
import WorkflowEngine from '../engine';
import { getBlockConnection } from '../helper';
import { isWhitespace, parseJSON } from '@/utils/helper';
import decryptFlow, { getWorkflowPass } from '@/utils/decrypt-flow';
import WorkflowEngine from '../engine';
import { getBlockConnection } from '../helper';

function workflowListener(workflow, options) {
return new Promise((resolve, reject) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill';
import { getBlockConnection } from '../helper';
import { default as dataExporter, files } from '@/utils/data-exporter';
import { getBlockConnection } from '../helper';

async function exportData({ data, outputs }) {
const nextBlockId = getBlockConnection({ outputs });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getBlockConnection } from '../helper';
import { googleSheets } from '@/utils/api';
import {
convert2DArrayToArrayObj,
convertArrObjTo2DArr,
isWhitespace,
parseJSON,
} from '@/utils/helper';
import { getBlockConnection } from '../helper';

async function getSpreadsheetValues({ spreadsheetId, range, firstRowAsKey }) {
const response = await googleSheets.getValues({ spreadsheetId, range });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function handleDownload({ data, outputs }) {
this.addDataToColumn(data.dataColumn, currentFilename);
}
if (data.assignVariable) {
this.referenceData.variables[data.variableName] = currentFilename;
this.setVariable(data.variableName, currentFilename);
}

clearTimeout(timeout);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { getBlockConnection } from '../helper';
import { parseJSON } from '@/utils/helper';
import mustacheReplacer from '@/utils/reference-data/mustache-replacer';
import { getBlockConnection } from '../helper';

function insertData({ outputs, data }, { refData }) {
return new Promise((resolve) => {
const replacedValueList = {};
data.dataList.forEach(({ name, value, type }) => {
const replacedValue = mustacheReplacer(value, refData);
const realValue = parseJSON(replacedValue, replacedValue);
const realValue = parseJSON(replacedValue.value, replacedValue.value);

Object.assign(replacedValueList, replacedValue.list);

if (type === 'table') {
this.addDataToColumn(name, realValue);
} else {
this.referenceData.variables[name] = realValue;
this.setVariable(name, realValue);
}
});

resolve({
data: '',
replacedValue: replacedValueList,
nextBlockId: getBlockConnection({ outputs }),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,13 @@ async function checkAccess(blockName) {
return true;
}

async function interactionHandler(block, { refData }) {
async function interactionHandler(block) {
await checkAccess(block.name);

const { executedBlockOnWeb, debugMode } = this.workflow.settings;

const nextBlockId = getBlockConnection(block);
const messagePayload = {
...block,
debugMode,
executedBlockOnWeb,
activeTabId: this.activeTab.id,
frameSelector: this.frameSelector,
};

if (block.name === 'javascript-code') messagePayload.refData = refData;

try {
const data = await this._sendMessageToTab(messagePayload, {
const data = await this._sendMessageToTab(block, {
frameId: this.activeTab.frameId || 0,
});

Expand Down Expand Up @@ -75,23 +64,7 @@ async function interactionHandler(block, { refData }) {
}

if (block.data.assignVariable) {
this.referenceData.variables[block.data.variableName] = data;
}

if (block.name === 'javascript-code') {
if (data?.variables) {
Object.keys(data.variables).forEach((varName) => {
this.referenceData.variables[varName] = data.variables[varName];
});
}

if (data?.columns.insert) {
const params = Array.isArray(data.columns.data)
? data.columns.data
: [data.columns.data];

this.addDataToColumn(params);
}
this.setVariable(block.data.variableName, data);
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { getBlockConnection } from '../helper';

export async function javascriptCode({ outputs, data, ...block }, { refData }) {
const nextBlockId = getBlockConnection({ outputs });

try {
if (data.everyNewTab) {
const isScriptExist = this.preloadScripts.find(
({ id }) => id === block.id
);

if (!isScriptExist) {
this.preloadScripts.push({ ...block, data });
}
}
if (!this.activeTab.id) {
if (!data.everyNewTab) {
throw new Error('no-tab');
} else {
return { data: '', nextBlockId };
}
}

const result = await this._sendMessageToTab({ ...block, data, refData });

if (result?.variables) {
Object.keys(result.variables).forEach((varName) => {
this.setVariable(varName, result.variables[varName]);
});
}
if (result?.columns.insert) {
const params = Array.isArray(result.columns.data)
? result.columns.data
: [result.columns.data];

this.addDataToColumn(params);
}

return {
nextBlockId,
data: result?.columns.data || {},
};
} catch (error) {
error.nextBlockId = nextBlockId;

throw error;
}
}

export default javascriptCode;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getBlockConnection } from '../helper';
import { parseJSON } from '@/utils/helper';
import { getBlockConnection } from '../helper';

async function loopData({ data, id, outputs }) {
const nextBlockId = getBlockConnection({ outputs });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import browser from 'webextension-polyfill';
import { isWhitespace, sleep } from '@/utils/helper';
import {
getBlockConnection,
attachDebugger,
sendDebugCommand,
} from '../helper';
import { isWhitespace, sleep } from '@/utils/helper';

async function newTab(block) {
if (this.windowId) {
Expand Down Expand Up @@ -84,6 +84,13 @@ async function newTab(block) {
chrome.debugger.detach({ tabId: tab.id });
}

if (this.preloadScripts.length > 0) {
const preloadScripts = this.preloadScripts.map((script) =>
this._sendMessageToTab(script)
);
await Promise.allSettled(preloadScripts);
}

return {
data: url,
nextBlockId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export default async function ({ data, outputs }) {
this.activeTab.url = tab.url;
this.windowId = tab.windowId;

if (this.preloadScripts.length > 0) {
const preloadScripts = this.preloadScripts.map((script) =>
this._sendMessageToTab(script)
);
await Promise.allSettled(preloadScripts);
}

return {
nextBlockId,
data: tab.url,
Expand Down
Loading

0 comments on commit 6557466

Please sign in to comment.