From 033de5816c5cd6a04c208e934586b755e097e38b Mon Sep 17 00:00:00 2001 From: PhilReact Date: Fri, 25 Oct 2024 23:52:37 +0300 Subject: [PATCH] fixed save-as qortal requests --- public/content-script.js | 28 +++++++++++++++++-- .../Apps/useQortalMessageListener.tsx | 10 +++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/public/content-script.js b/public/content-script.js index 7d0681e..bd5622f 100644 --- a/public/content-script.js +++ b/public/content-script.js @@ -551,10 +551,34 @@ const testAsync = async (sendResponse)=> { sendResponse({ result: null, error: "Testing" }); } +const saveFile = (blob, filename) => { + // Create a URL for the blob + const url = URL.createObjectURL(blob); + + // Create a link element + const a = document.createElement('a'); + a.href = url; + a.download = filename; + + // Append the link to the document and trigger a click + document.body.appendChild(a); + a.click(); + + // Clean up by removing the link and revoking the object URL + document.body.removeChild(a); + URL.revokeObjectURL(url); +}; + + + + const showSaveFilePicker = async (data) => { + let blob + let fileName try { const {filename, mimeType, fileHandleOptions, fileId} = data - const blob = await retrieveFileFromIndexedDB(fileId) + blob = await retrieveFileFromIndexedDB(fileId) + fileName = filename const fileHandle = await window.showSaveFilePicker({ suggestedName: filename, types: [ @@ -571,7 +595,7 @@ const showSaveFilePicker = async (data) => { } writeFile(fileHandle, blob).then(() => console.log("FILE SAVED")) } catch (error) { - FileSaver.saveAs(blob, filename) + saveFile(blob, fileName) } } diff --git a/src/components/Apps/useQortalMessageListener.tsx b/src/components/Apps/useQortalMessageListener.tsx index 67b4461..cc3dc2a 100644 --- a/src/components/Apps/useQortalMessageListener.tsx +++ b/src/components/Apps/useQortalMessageListener.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react'; - +import FileSaver from 'file-saver'; class Semaphore { constructor(count) { this.count = count @@ -213,9 +213,13 @@ const UIQortalRequests = [ } const showSaveFilePicker = async (data) => { + let blob + let fileName try { const {filename, mimeType, fileHandleOptions, fileId} = data - const blob = await retrieveFileFromIndexedDB(fileId) + blob = await retrieveFileFromIndexedDB(fileId) + fileName = filename + const fileHandle = await window.showSaveFilePicker({ suggestedName: filename, types: [ @@ -232,7 +236,7 @@ const UIQortalRequests = [ } writeFile(fileHandle, blob).then(() => console.log("FILE SAVED")) } catch (error) { - FileSaver.saveAs(blob, filename) + FileSaver.saveAs(blob, fileName) } }