Prechádzať zdrojové kódy

display "Save as" dialog in Firefox instead of prompt

Gildas 7 rokov pred
rodič
commit
3d807cb865
2 zmenil súbory, kde vykonal 20 pridanie a 11 odobranie
  1. 7 1
      extension/core/bg/bg.js
  2. 13 10
      extension/core/content/content.js

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

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global browser, singlefile, FrameTree */
+/* global browser, singlefile, FrameTree, Blob */
 
 singlefile.core = (() => {
 
@@ -47,8 +47,14 @@ singlefile.core = (() => {
 		}
 		if (request.download) {
 			try {
+				if (request.content) {
+					request.url = URL.createObjectURL(new Blob([request.content], { type: "text/html" }));
+				}
 				return browser.downloads.download({ url: request.url, saveAs: request.saveAs, filename: request.filename.replace(/[/?<>\\:*|"]/g, "_") })
 					.then(downloadId => new Promise(resolve => {
+						if (request.content) {
+							URL.revokeObjectURL(request.url);
+						}
 						browser.downloads.onChanged.addListener(onChanged);
 
 						function onChanged(event) {

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

@@ -136,16 +136,19 @@ this.singlefile.top = this.singlefile.top || (() => {
 	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();
+			const response = await browser.runtime.sendMessage({ download: true, content: page.content, 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();
+				}
 			}
 		}
 	}