From 028ff074f9334c126d90c2401f9355ad5d8c5651 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 9 Sep 2023 04:49:43 +0800 Subject: [PATCH 1/2] fix: download button in code editor --- src/page/Components/Editor/Editor.svelte | 12 +----- src/shared/utils.js | 47 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/page/Components/Editor/Editor.svelte b/src/page/Components/Editor/Editor.svelte index 81bb18b0..e8f1ed49 100644 --- a/src/page/Components/Editor/Editor.svelte +++ b/src/page/Components/Editor/Editor.svelte @@ -3,7 +3,7 @@ import Loader from "../../../shared/Components/Loader.svelte"; import IconButton from "../../../shared/Components/IconButton.svelte"; import Tag from "../../../shared/Components/Tag.svelte"; - import {formatDate} from "../../../shared/utils.js"; + import {formatDate, downloadToFile} from "../../../shared/utils.js"; import CodeMirror, {cmChanged, cmGetInstance, cmSetSavedCode} from "./CodeMirror.svelte"; import iconDownload from "../../../shared/img/icon-download.svg?raw"; import iconTrash from "../../../shared/img/icon-trash.svg?raw"; @@ -85,15 +85,7 @@ const discard = () => codemirror.discardChanges(); function download() { - const link = document.createElement("a"); - const content = codemirror.getValue(); - const filename = activeItem.filename; - link.setAttribute("href", `data:text/plain;charset=utf-8,${encodeURIComponent(content)}`); - link.setAttribute("download", filename); - link.style.display = "none"; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); + downloadToFile(activeItem.filename, codemirror.getValue()); } async function update() { diff --git a/src/shared/utils.js b/src/shared/utils.js index dfc689a2..78fba6d8 100644 --- a/src/shared/utils.js +++ b/src/shared/utils.js @@ -190,3 +190,50 @@ export async function openExtensionPage() { browser.tabs.update(tab.id, {active: true}); browser.windows.update(tab.windowId, {focused: true}); } + +// Safari currently does not honor the download attribute of elements in extension contexts +// Also not support https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/download +export async function downloadToFile(filename, content, type = "text/plain") { + const url = "https://quoid.github.io/userscripts/serve/download.html"; + const tab = await browser.tabs.create({url}); + const exchange = {filename, content, type}; + const exscript = o => { + // make sure executed only once + if (window.US_DOWNLOAD === 1) return; window.US_DOWNLOAD = 1; + window.stop(); + document.body.textContent = "Download is starting..."; + const a = document.createElement("a"); + a.download = o.filename; + a.href = URL.createObjectURL(new Blob([o.content], {type: o.type})); + a.click(); + document.body.innerHTML += "
The download should have started.
"; + a.textContent = o.filename; + document.body.append(a); + }; + // Safari currently unable to stably executeScript on tab loading status + try { + await browser.tabs.executeScript(tab.id, { + code: `(${exscript})(${JSON.stringify(exchange)});` + }); + } catch { + const handleUpdated = async tabId => { + if (tabId !== tab.id) return; + try { + await browser.tabs.executeScript(tabId, { + code: `(${exscript})(${JSON.stringify(exchange)});` + }); + console.info(`[${filename}] Download is starting...`); + } catch { + console.info(`[${filename}] Start download failed, retrying...`); + } + }; + browser.tabs.onUpdated.addListener(handleUpdated); + // Remove the listener when tab closing + const handleRemoved = tabId => { + if (tabId !== tab.id) return; + browser.tabs.onUpdated.removeListener(handleUpdated); + browser.tabs.onRemoved.removeListener(handleRemoved); + }; + browser.tabs.onRemoved.addListener(handleRemoved); + } +} From 6d1e8ac37330b28bf00f6838ee3d8ea889b698dd Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 9 Sep 2023 05:24:17 +0800 Subject: [PATCH 2/2] refactor: open link from new tab --- src/page/Components/Settings.svelte | 11 ++++++++++- src/shared/utils.js | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/page/Components/Settings.svelte b/src/page/Components/Settings.svelte index 5b89ea28..40f7f7b3 100644 --- a/src/page/Components/Settings.svelte +++ b/src/page/Components/Settings.svelte @@ -1,6 +1,7 @@