-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
621 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 41 additions & 8 deletions
49
src/background/workflow-engine/blocks-handler/handler-export-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
38 changes: 38 additions & 0 deletions
38
src/background/workflow-engine/blocks-handler/handler-handle-dialog.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
98 changes: 98 additions & 0 deletions
98
src/background/workflow-engine/blocks-handler/handler-handle-download.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.