Parcourir la source

use browser.downloads API when available

Gildas il y a 7 ans
Parent
commit
cae0023a37

+ 13 - 0
extension/core/bg/bg.js

@@ -44,6 +44,19 @@ singlefile.core = (() => {
 		if (request.getConfig) {
 			return singlefile.config.get();
 		}
+		if (request.download) {
+			try {
+				return browser.downloads.download({ url: request.url, saveAs: request.saveAs, filename: request.filename.replace(/[/?<>\\:*|"]/g, "_") })
+					.then(downloadId => new Promise(resolve => browser.downloads.onChanged.addListener(event => {
+						if (event.id == downloadId && event.state && event.state.current == "complete") {
+							resolve({});
+						}
+					})))
+					.catch(() => ({ notSupported: true }));
+			} catch (error) {
+				return Promise.resolve({ notSupported: true });
+			}
+		}
 	});
 
 	return {

+ 15 - 12
extension/core/content/content.js

@@ -40,7 +40,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 			processing = true;
 			try {
 				const page = await processMessage(message);
-				downloadPage(page, message.options);
+				await downloadPage(page, message.options);
 				revokeDownloadURL(page);
 			} catch (error) {
 				console.error(error); // eslint-disable-line no-console
@@ -215,17 +215,20 @@ this.singlefile.top = this.singlefile.top || (() => {
 		return "";
 	}
 
-	function downloadPage(page, options) {
-		if (options.confirmFilename) {
-			page.filename = prompt("File name", page.filename);
-		}
-		if (page.filename && page.filename.length) {
-			const link = document.createElement("a");
-			document.body.appendChild(link);
-			link.download = page.filename;
-			link.href = page.url;
-			link.dispatchEvent(new MouseEvent("click"));
-			link.remove();
+	async function downloadPage(page, options) {
+		const response = await browser.runtime.sendMessage({ download: true, url: page.url, saveAs: options.confirmFilename, filename: page.filename });
+		if (response.notSupported) {
+			if (options.confirmFilename) {
+				page.filename = prompt("File name", page.filename);
+			}
+			if (page.filename && page.filename.length) {
+				const link = document.createElement("a");
+				document.body.appendChild(link);
+				link.download = page.filename;
+				link.href = page.url;
+				link.dispatchEvent(new MouseEvent("click"));
+				link.remove();
+			}
 		}
 	}
 

+ 14 - 0
lib/browser-polyfill/custom-browser-polyfill.js

@@ -107,6 +107,20 @@
 					}
 				})
 			},
+			downloads: {
+				download: options => new Promise((resolve, reject) => {
+					chrome.downloads.download(options, downloadId => {
+						if (chrome.runtime.lastError) {
+							reject(chrome.runtime.lastError);
+						} else {
+							resolve(downloadId);
+						}
+					});
+				}),
+				onChanged: {
+					addListener: listener => chrome.downloads.onChanged.addListener(listener)
+				}
+			},
 			menus: {
 				onClicked: {
 					addListener: listener => chrome.contextMenus.onClicked.addListener(listener)

+ 2 - 1
manifest.json

@@ -47,10 +47,11 @@
         "default_title": "Save page with SingleFile"
     },
     "permissions": [
-        "tabs",
         "contextMenus",
+        "downloads",
         "menus",
         "storage",
+        "tabs",
         "<all_urls>"
     ],
     "applications": {