|
@@ -1,6 +1,33 @@
|
|
import browser from 'webextension-polyfill';
|
|
import browser from 'webextension-polyfill';
|
|
import { getBlockConnection } from '../helper';
|
|
import { getBlockConnection } from '../helper';
|
|
|
|
|
|
|
|
+const getFileExtension = (str) => /(?:\.([^.]+))?$/.exec(str)[1];
|
|
|
|
+function determineFilenameListener(item, suggest) {
|
|
|
|
+ const filesname =
|
|
|
|
+ JSON.parse(sessionStorage.getItem('rename-downloaded-files')) || {};
|
|
|
|
+ const suggestion = filesname[item.id];
|
|
|
|
+
|
|
|
|
+ if (!suggestion) return true;
|
|
|
|
+
|
|
|
|
+ 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,
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
|
function handleDownload({ data, outputs }) {
|
|
function handleDownload({ data, outputs }) {
|
|
const nextBlockId = getBlockConnection({ outputs });
|
|
const nextBlockId = getBlockConnection({ outputs });
|
|
const getFilesname = () =>
|
|
const getFilesname = () =>
|
|
@@ -9,6 +36,15 @@ function handleDownload({ data, outputs }) {
|
|
return new Promise((resolve) => {
|
|
return new Promise((resolve) => {
|
|
if (!this.activeTab.id) throw new Error('no-tab');
|
|
if (!this.activeTab.id) throw new Error('no-tab');
|
|
|
|
|
|
|
|
+ const hasListener = chrome.downloads.onDeterminingFilename.hasListeners(
|
|
|
|
+ () => {}
|
|
|
|
+ );
|
|
|
|
+ if (!hasListener) {
|
|
|
|
+ chrome.downloads.onDeterminingFilename.addListener(
|
|
|
|
+ determineFilenameListener
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
let downloadId = null;
|
|
let downloadId = null;
|
|
const handleCreated = ({ id }) => {
|
|
const handleCreated = ({ id }) => {
|
|
if (downloadId) return;
|
|
if (downloadId) return;
|
|
@@ -64,6 +100,10 @@ function handleDownload({ data, outputs }) {
|
|
JSON.stringify(filesname)
|
|
JSON.stringify(filesname)
|
|
);
|
|
);
|
|
|
|
|
|
|
|
+ chrome.downloads.onDeterminingFilename.removeListener(
|
|
|
|
+ determineFilenameListener
|
|
|
|
+ );
|
|
|
|
+
|
|
resolve({
|
|
resolve({
|
|
nextBlockId,
|
|
nextBlockId,
|
|
data: currentFilename,
|
|
data: currentFilename,
|