Skip to content

Commit

Permalink
v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholid060 authored Mar 19, 2022
2 parents 75becb8 + 21b80f6 commit 190ea40
Show file tree
Hide file tree
Showing 31 changed files with 621 additions and 82 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
'no-console': ['warn', { allow: ['warn', 'error'] }],
'no-underscore-dangle': 'off',
'func-names': 'off',
'import/no-named-default': 'off',
'import/extensions': [
'error',
'always',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "automa",
"version": "1.4.4",
"version": "1.5.0",
"description": "An extension for automating your browser by connecting blocks",
"license": "MIT",
"repository": {
Expand Down
45 changes: 45 additions & 0 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,51 @@ chrome.runtime.onStartup.addListener(async () => {
await browser.storage.local.set({ onStartupTriggers });
});

if (chrome.downloads) {
const getFileExtension = (str) => /(?:\.([^.]+))?$/.exec(str)[1];
chrome.downloads.onDeterminingFilename.addListener((item, suggest) => {
if (item.byExtensionId === chrome.runtime.id) {
const filesname =
JSON.parse(sessionStorage.getItem('export-filesname')) || {};
const blobId = item.url.replace('blob:chrome-extension://', '');
const suggestion = filesname[blobId];

if (suggestion) {
delete filesname[blobId];

suggest(suggestion);
sessionStorage.setItem('export-filesname', JSON.stringify(filesname));
}

return;
}

const filesname =
JSON.parse(sessionStorage.getItem('rename-downloaded-files')) || {};
const suggestion = filesname[item.id];

if (!suggestion) return;

const hasFileExt = getFileExtension(suggestion.filename);

if (!hasFileExt) {
const filExtension = getFileExtension(item.filename);
suggestion.filename += `.${filExtension}`;
}

if (!suggestion.waitForDownload) delete filesname[item.id];
sessionStorage.setItem(
'rename-downloaded-files',
JSON.stringify(filesname)
);

suggest({
filename: suggestion.filename,
conflictAction: suggestion.onConflict,
});
});
}

const message = new MessageListener('background');

message.on('fetch:text', (url) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
import browser from 'webextension-polyfill';
import { getBlockConnection } from '../helper';
import dataExporter from '@/utils/data-exporter';
import { default as dataExporter, files } from '@/utils/data-exporter';

function exportData({ data, outputs }) {
return new Promise((resolve) => {
async function exportData({ data, outputs }) {
const nextBlockId = getBlockConnection({ outputs });

try {
const dataToExport = data.dataToExport || 'data-columns';
let payload = this.referenceData.table;

if (dataToExport === 'google-sheets') {
payload = this.referenceData.googleSheets[data.refKey] || [];
}

dataExporter(payload, data);
const hasDownloadAccess = await browser.permissions.contains({
permissions: ['downloads'],
});
const blobUrl = dataExporter(payload, {
...data,
returnUrl: hasDownloadAccess,
});

if (hasDownloadAccess) {
const filename = `${data.name}${files[data.type].ext}`;
const blobId = blobUrl.replace('blob:chrome-extension://', '');
const filesname =
JSON.parse(sessionStorage.getItem('export-filesname')) || {};

const options = {
filename,
conflictAction: data.onConflict || 'uniquify',
};

resolve({
filesname[blobId] = options;
sessionStorage.setItem('export-filesname', JSON.stringify(filesname));

await browser.downloads.download({
...options,
url: blobUrl,
});
}

return {
data: '',
nextBlockId: getBlockConnection({ outputs }),
});
});
nextBlockId,
};
} catch (error) {
error.nextBlockId = nextBlockId;

throw error;
}
}

export default exportData;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getBlockConnection, sendDebugCommand } from '../helper';

function handleDialog({ data, outputs }) {
const nextBlockId = getBlockConnection({ outputs });

return new Promise((resolve, reject) => {
if (!this.workflow.settings.debugMode) {
const error = new Error('not-debug-mode');
error.nextBlockId = nextBlockId;

reject(error);
return;
}

this.dialogParams = {
accept: data.accept,
promptText: data.promptText,
};

const methodName = 'Page.javascriptDialogOpening';
if (!this.eventListeners[methodName]) {
this.on(methodName, () => {
sendDebugCommand(
this.activeTab.id,
'Page.handleJavaScriptDialog',
this.dialogParams
);
});
}

resolve({
data: '',
nextBlockId,
});
});
}

export default handleDialog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import browser from 'webextension-polyfill';
import { getBlockConnection } from '../helper';

function handleDownload({ data, outputs }) {
const nextBlockId = getBlockConnection({ outputs });
const getFilesname = () =>
JSON.parse(sessionStorage.getItem('rename-downloaded-files')) || {};

return new Promise((resolve) => {
if (!this.activeTab.id) throw new Error('no-tab');

let downloadId = null;
const handleCreated = ({ id }) => {
if (downloadId) return;

const names = getFilesname();

downloadId = id;
names[id] = data;
sessionStorage.setItem('rename-downloaded-files', JSON.stringify(names));

browser.downloads.onCreated.removeListener(handleCreated);
};
browser.downloads.onCreated.addListener(handleCreated);

if (!data.waitForDownload) {
resolve({
nextBlockId,
data: data.filename,
});

return;
}

let isResolved = false;
let currentFilename = data.filename;

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

isResolved = true;

resolve({
nextBlockId,
data: currentFilename,
});
}, data.timeout);

const resolvePromise = (id) => {
if (data.saveData) {
this.addDataToColumn(data.dataColumn, currentFilename);
}
if (data.assignVariable) {
this.referenceData.variables[data.variableName] = currentFilename;
}

clearTimeout(timeout);
isResolved = true;

const filesname = getFilesname();
delete filesname[id];
sessionStorage.setItem(
'rename-downloaded-files',
JSON.stringify(filesname)
);

resolve({
nextBlockId,
data: currentFilename,
});
};

const handleChanged = ({ state, id, filename }) => {
if (this.isDestroyed || isResolved) {
browser.downloads.onChanged.removeListener(handleChanged);
return;
}

if (downloadId !== id) return;

if (filename) currentFilename = filename.current;

if (state && state.current === 'complete') {
resolvePromise(id);
} else {
browser.downloads.search({ id }).then(([download]) => {
if (!download || !download.endTime) return;

resolvePromise(id);
});
}
};

browser.downloads.onChanged.addListener(handleChanged);
});
}

export default handleDownload;
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ async function interactionHandler(block, { refData }) {
}

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

this.addDataToColumn(params);
}
}

Expand Down
Loading

0 comments on commit 190ea40

Please sign in to comment.