Explorar el Código

extracted download.js from core.js

Gildas hace 7 años
padre
commit
18470c605d
Se han modificado 3 ficheros con 83 adiciones y 48 borrados
  1. 1 48
      extension/core/bg/core.js
  2. 81 0
      extension/core/bg/download.js
  3. 1 0
      manifest.json

+ 1 - 48
extension/core/bg/core.js

@@ -71,23 +71,6 @@ singlefile.core = (() => {
 		if (request.getConfig) {
 			return singlefile.config.get();
 		}
-		if (request.download) {
-			try {
-				if (request.content) {
-					request.url = URL.createObjectURL(new Blob([request.content], { type: "text/html" }));
-				}
-				return downloadPage(request, { confirmFilename: request.confirmFilename, incognito: sender.tab.incognito })
-					.catch(error => {
-						if (error.message && error.message.includes("'incognito'")) {
-							return downloadPage(request, { confirmFilename: request.confirmFilename });
-						} else {
-							return { notSupported: true };
-						}
-					});
-			} catch (error) {
-				return Promise.resolve({ notSupported: true });
-			}
-		}
 		if (request.processContent) {
 			processBackgroundTab(sender.tab, request);
 		}
@@ -163,37 +146,7 @@ singlefile.core = (() => {
 		const date = new Date();
 		page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
 		page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
-		return downloadPage(page, options);
-	}
-
-	async function downloadPage(page, options) {
-		const downloadInfo = {
-			url: page.url,
-			saveAs: options.confirmFilename,
-			filename: page.filename.replace(/[/\\?%*:|"<>\x7F]+/g, "_")
-		};
-		if (options.incognito) {
-			downloadInfo.incognito = true;
-		}
-		const downloadId = await browser.downloads.download(downloadInfo);
-		return new Promise((resolve, reject) => {
-			browser.downloads.onChanged.addListener(onChanged);
-
-			function onChanged(event) {
-				if (event.id == downloadId && event.state) {
-					if (event.state.current == "complete") {
-						URL.revokeObjectURL(page.url);
-						resolve({});
-						browser.downloads.onChanged.removeListener(onChanged);
-					}
-					if (event.state.current == "interrupted") {
-						URL.revokeObjectURL(page.url);
-						reject(new Error(event.state.current));
-						browser.downloads.onChanged.removeListener(onChanged);
-					}
-				}
-			}
-		});
+		return singlefile.download.downloadPage(page, options);
 	}
 
 	async function saveStart(tab, options) {

+ 81 - 0
extension/core/bg/download.js

@@ -0,0 +1,81 @@
+/*
+ * Copyright 2018 Gildas Lormeau
+ * contact : gildas.lormeau <at> gmail.com
+ * 
+ * This file is part of SingleFile.
+ *
+ *   SingleFile is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Lesser General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   SingleFile is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Lesser General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Lesser General Public License
+ *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* global browser, singlefile, Blob */
+
+singlefile.download = (() => {
+
+	browser.runtime.onMessage.addListener((request, sender) => {
+		if (request.download) {
+			try {
+				if (request.content) {
+					request.url = URL.createObjectURL(new Blob([request.content], { type: "text/html" }));
+				}
+				return downloadPage(request, { confirmFilename: request.confirmFilename, incognito: sender.tab.incognito })
+					.catch(error => {
+						if (error.message && error.message.includes("'incognito'")) {
+							return downloadPage(request, { confirmFilename: request.confirmFilename });
+						} else {
+							return { notSupported: true };
+						}
+					});
+			} catch (error) {
+				return Promise.resolve({ notSupported: true });
+			}
+		}
+	});
+
+	return { downloadPage };
+
+	async function downloadPage(page, options) {
+		let filename = page.filename.replace(/[/\\?%*:|"<>\x7F]+/g, "_");
+		if (filename.length > 128) {
+			filename = filename.replace(/\.html?$/, "").substring(0, 122) + "….html";
+		}
+		const downloadInfo = {
+			url: page.url,
+			saveAs: options.confirmFilename,
+			filename
+		};
+		if (options.incognito) {
+			downloadInfo.incognito = true;
+		}
+		const downloadId = await browser.downloads.download(downloadInfo);
+		return new Promise((resolve, reject) => {
+			browser.downloads.onChanged.addListener(onChanged);
+
+			function onChanged(event) {
+				if (event.id == downloadId && event.state) {
+					if (event.state.current == "complete") {
+						URL.revokeObjectURL(page.url);
+						resolve({});
+						browser.downloads.onChanged.removeListener(onChanged);
+					}
+					if (event.state.current == "interrupted") {
+						URL.revokeObjectURL(page.url);
+						reject(new Error(event.state.current));
+						browser.downloads.onChanged.removeListener(onChanged);
+					}
+				}
+			}
+		});
+	}
+
+})();

+ 1 - 0
manifest.json

@@ -45,6 +45,7 @@
 			"lib/fetch/bg/fetch.js",
 			"extension/index.js",
 			"extension/core/bg/storage.js",
+			"extension/core/bg/download.js",
 			"extension/core/bg/config.js",
 			"extension/core/bg/core.js",
 			"extension/ui/bg/bg-ui.js",