Gildas 7 rokov pred
rodič
commit
e945567375
2 zmenil súbory, kde vykonal 54 pridanie a 52 odobranie
  1. 41 30
      extension/core/bg/download.js
  2. 13 22
      extension/core/content/content.js

+ 41 - 30
extension/core/bg/download.js

@@ -30,38 +30,34 @@ singlefile.download = (() => {
 	};
 
 	function onMessage(message, sender) {
-		try {
-			if (message.truncated) {
-				let partialContent = partialContents.get(sender.tab.id);
-				if (!partialContent) {
-					partialContent = [];
-					partialContents.set(sender.tab.id, partialContent);
-				}
-				partialContent.push(message.content);
-				if (message.finished) {
-					partialContents.delete(sender.tab.id);
-					message.url = URL.createObjectURL(new Blob(partialContent, { type: "text/html" }));
-				} else {
-					return Promise.resolve({});
-				}
-			} else if (message.content) {
-				message.url = URL.createObjectURL(new Blob([message.content], { type: "text/html" }));
+		if (message.truncated) {
+			let partialContent = partialContents.get(sender.tab.id);
+			if (!partialContent) {
+				partialContent = [];
+				partialContents.set(sender.tab.id, partialContent);
+			}
+			partialContent.push(message.content);
+			if (message.finished) {
+				partialContents.delete(sender.tab.id);
+				message.url = URL.createObjectURL(new Blob(partialContent, { type: "text/html" }));
+			} else {
+				return Promise.resolve({});
 			}
-			return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction })
-				.catch(error => {
-					if (error.message) {
-						if (!error.message.toLowerCase().includes("canceled")) {
-							if (error.message.includes("'incognito'")) {
-								return downloadPage(message, { confirmFilename: message.confirmFilename, filenameConflictAction: message.filenameConflictAction });
-							} else {
-								return { notSupported: true };
-							}
+		} else if (message.content) {
+			message.url = URL.createObjectURL(new Blob([message.content], { type: "text/html" }));
+		}
+		return downloadPage(message, { confirmFilename: message.confirmFilename, incognito: sender.tab.incognito, filenameConflictAction: message.filenameConflictAction })
+			.catch(error => {
+				if (error.message) {
+					if (!error.message.toLowerCase().includes("canceled")) {
+						if (error.message.includes("'incognito'")) {
+							return downloadPage(message, { confirmFilename: message.confirmFilename, filenameConflictAction: message.filenameConflictAction });
+						} else {
+							throw error;
 						}
 					}
-				});
-		} catch (error) {
-			return Promise.resolve({ notSupported: true });
-		}
+				}
+			});
 	}
 
 	async function downloadPage(page, options) {
@@ -74,7 +70,22 @@ singlefile.download = (() => {
 		if (options.incognito) {
 			downloadInfo.incognito = true;
 		}
-		const downloadId = await browser.downloads.download(downloadInfo);
+		let downloadId;
+		try {
+			downloadId = await browser.downloads.download(downloadInfo);
+		} catch (error) {
+			if (error.message) {
+				if (!error.message.toLowerCase().includes("canceled")) {
+					if (error.message.includes("'incognito'")) {
+						return downloadPage(page, { confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction });
+					} else {
+						throw error;
+					}
+				}
+			} else {
+				throw error;
+			}
+		}
 		return new Promise((resolve, reject) => {
 			browser.downloads.onChanged.addListener(onChanged);
 

+ 13 - 22
extension/core/content/content.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global browser, SingleFileBrowser, singlefile, frameTree, document, Blob, MouseEvent, addEventListener, window, lazyLoader, URL, setTimeout, docHelper */
+/* global browser, SingleFileBrowser, singlefile, frameTree, document, MouseEvent, addEventListener, window, lazyLoader, URL, setTimeout, docHelper */
 
 this.singlefile.top = this.singlefile.top || (() => {
 
@@ -141,7 +141,6 @@ this.singlefile.top = this.singlefile.top || (() => {
 		if (options.selected) {
 			singlefile.ui.unmarkSelection();
 		}
-		page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
 		if (options.shadowEnabled) {
 			singlefile.ui.onEndPage();
 		}
@@ -154,32 +153,24 @@ this.singlefile.top = this.singlefile.top || (() => {
 
 	async function downloadPage(page, options) {
 		if (options.backgroundSave) {
-			const response = await browser.runtime.sendMessage({ download: true, url: page.url, confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction, filename: page.filename });
-			if (response.notSupported) {
-				let response;
-				for (let blockIndex = 0; (!response || !response.notSupported) && (blockIndex * MAX_CONTENT_SIZE < page.content.length); blockIndex++) {
-					const message = { download: true, confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction, filename: page.filename };
-					message.truncated = page.content.length > MAX_CONTENT_SIZE;
-					if (message.truncated) {
-						message.finished = (blockIndex + 1) * MAX_CONTENT_SIZE > page.content.length;
-						message.content = page.content.substring(blockIndex * MAX_CONTENT_SIZE, (blockIndex + 1) * MAX_CONTENT_SIZE);
-					} else {
-						message.content = page.content;
-					}
-					response = await browser.runtime.sendMessage(message);
-				}
-				if (response.notSupported) {
-					downloadPageFallback(page, options);
+			let response;
+			for (let blockIndex = 0; !response && (blockIndex * MAX_CONTENT_SIZE < page.content.length); blockIndex++) {
+				const message = { download: true, confirmFilename: options.confirmFilename, filenameConflictAction: options.filenameConflictAction, filename: page.filename };
+				message.truncated = page.content.length > MAX_CONTENT_SIZE;
+				if (message.truncated) {
+					message.finished = (blockIndex + 1) * MAX_CONTENT_SIZE > page.content.length;
+					message.content = page.content.substring(blockIndex * MAX_CONTENT_SIZE, (blockIndex + 1) * MAX_CONTENT_SIZE);
+				} else {
+					message.content = page.content;
 				}
-			} else {
-				URL.revokeObjectURL(page.url);
+				response = await browser.runtime.sendMessage(message);
 			}
 		} else {
-			downloadPageFallback(page, options);
+			downloadPageForeground(page, options);
 		}
 	}
 
-	function downloadPageFallback(page, options) {
+	function downloadPageForeground(page, options) {
 		if (options.confirmFilename) {
 			page.filename = singlefile.ui.prompt("File name", page.filename);
 		}