diff --git a/.eslintrc b/.eslintrc index ef91911..35455f5 100644 --- a/.eslintrc +++ b/.eslintrc @@ -9,9 +9,9 @@ "extends": ["eslint:recommended", "plugin:prettier/recommended"], "parserOptions": { "sourceType": "module", - "ecmaVersion": 2018 + "ecmaVersion": 2020 }, "rules": { "linebreak-style": ["error", "unix"] } -} \ No newline at end of file +} diff --git a/.prettierrc b/.prettierrc index 92cde39..76e396e 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,3 +1,4 @@ { - "singleQuote": true + "singleQuote": true, + "semi": true } \ No newline at end of file diff --git a/app/scripts/background.js b/app/scripts/background.js index eb3cfb7..bb7cf55 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,4 +1,5 @@ import Aria2 from 'aria2'; +import { filter, Observable, take } from 'rxjs'; function validateUrl(value) { return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( @@ -7,7 +8,14 @@ function validateUrl(value) { } function downloadAgent() { - browser.downloads.onCreated.addListener(function (downloadItem) { + const subscribers = []; + const observable = new Observable((s) => subscribers.push(s)); + + chrome.downloads.onChanged.addListener((delta) => { + subscribers.forEach((s) => s.next(delta)); + }); + + chrome.downloads.onCreated.addListener(function (downloadItem) { if (downloadItem.state !== 'in_progress') { return; } @@ -49,7 +57,7 @@ function downloadAgent() { if (downloadItem.filename) { let directory, filename; filename = downloadItem.filename.replace(/^.*[\\/]/, ''); - directory = downloadItem.filename.match(/(.*)[/\\]/)[1] || ''; + directory = downloadItem.filename.match(/(.*)[/\\]/)?.[1] ?? ''; // Appends path to the options params = { dir: directory, @@ -66,18 +74,18 @@ function downloadAgent() { .call('addUri', [downloadUrl], params) .then(async () => { // Added successfully: Cancels and removes the download from browser download manager - function onFinished() {} + function pass() {} function onError(error) { console.error(`Error: ${error}`); } const removing = browser.downloads.removeFile(downloadItem.id); - removing.then(onFinished).catch(onError); + removing.then(pass).catch(pass); const canceling = browser.downloads.cancel(downloadItem.id); - canceling.then(onFinished).catch(onError); + canceling.then(pass).catch(onError); const erasing = browser.downloads.erase({ id: downloadItem.id }); - erasing.then(onFinished).catch(onError); + erasing.then(pass).catch(onError); // Shows notification if (result.enablenotifications) { @@ -97,7 +105,6 @@ function downloadAgent() { } }); } - setTimeout(() => browser.runtime.reload(), 1000); }) .catch((err) => { console.error(err); @@ -120,7 +127,21 @@ function downloadAgent() { 'extensionstatus', 'enablenotifications', ]); - getResult.then(onGot, onError); + + getResult.then((result) => { + // wait for filename to be set + if (downloadItem.filename == null || downloadItem.filename === '') { + observable + .pipe( + filter((delta) => delta.id === downloadItem.id && delta.filename), + take(1) + ) + .subscribe((delta) => { + downloadItem.filename = delta.filename.current; + onGot(result); + }); + } + }, onError); }); } diff --git a/package.json b/package.json index 5b81eec..a5698e1 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "webextension-toolbox": "4.0.3" }, "dependencies": { - "aria2": "^4.1.2" + "aria2": "^4.1.2", + "rxjs": "^7.3.0" } } diff --git a/yarn.lock b/yarn.lock index 06b3fc1..bb264d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2680,6 +2680,13 @@ run-parallel@^1.1.9: dependencies: queue-microtask "^1.2.2" +rxjs@^7.3.0: + version "7.3.0" + resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.3.0.tgz#39fe4f3461dc1e50be1475b2b85a0a88c1e938c6" + integrity sha512-p2yuGIg9S1epc3vrjKf6iVb3RCaAYjYskkO+jHIaV0IjOPlJop4UnodOoFb2xeNwlguqLYvGw1b1McillYb5Gw== + dependencies: + tslib "~2.1.0" + safe-buffer@^5.1.0: version "5.2.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" @@ -2919,6 +2926,11 @@ to-regex-range@^5.0.1: dependencies: is-number "^7.0.0" +tslib@~2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a" + integrity sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A== + type-check@^0.4.0, type-check@~0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"