From 8dd574bf9ae57380ca4b9c66c4c8164f7cc3f5ae Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 21 Aug 2019 21:10:38 -0400 Subject: [PATCH] download file via content script --- src/browser/browserApi.ts | 12 +++++++---- src/content/downloader.ts | 12 +++++++++-- .../SafariExtensionViewController.swift | 21 +++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 120d1da699..342e2c5cfb 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -141,7 +141,8 @@ export class BrowserApi { if (BrowserApi.isChromeApi) { return chrome.extension.getURL(path); } else if (BrowserApi.isSafariApi) { - // TODO + // TODO: promisify + SafariApp.sendMessageToApp('getAppPath'); return null; } else { return null; @@ -188,9 +189,12 @@ export class BrowserApi { data = Utils.fromBufferToB64(blobData); } SafariApp.sendMessageToApp('downloadFile', JSON.stringify({ - data: data, - type: type, - fileName: fileName, + command: 'downloaderPageData', + data: { + blobData: data, + blobOptions: blobOptions, + fileName: fileName, + }, }), true); } else { const blob = new Blob([blobData], blobOptions); diff --git a/src/content/downloader.ts b/src/content/downloader.ts index eb1517f2fd..450ce1baec 100644 --- a/src/content/downloader.ts +++ b/src/content/downloader.ts @@ -9,7 +9,16 @@ document.addEventListener('DOMContentLoaded', (event) => { safari.self.addEventListener('message', (msgEvent: any) => { const msg = JSON.parse(msgEvent.message.msg); if (msg.command === 'downloaderPageData') { - const blob = new Blob([msg.data.blobData], msg.data.blobOptions); + let data: any = msg.data.blobData; + if (msg.data.blobOptions == null || msg.data.blobOptions.type !== 'text/plain') { + const binaryString = window.atob(msg.data.blobData); + const bytes = new Uint8Array(binaryString.length); + for (let i = 0; i < binaryString.length; i++) { + bytes[i] = binaryString.charCodeAt(i); + } + data = bytes.buffer; + } + const blob = new Blob([data], msg.data.blobOptions); if (navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, msg.data.fileName); } else { @@ -20,7 +29,6 @@ document.addEventListener('DOMContentLoaded', (event) => { a.click(); document.body.removeChild(a); } - window.setTimeout(() => window.close(), 1500); } }, false); }); diff --git a/src/safari/app/desktop/safari/SafariExtensionViewController.swift b/src/safari/app/desktop/safari/SafariExtensionViewController.swift index 245c061144..cef2a076a8 100644 --- a/src/safari/app/desktop/safari/SafariExtensionViewController.swift +++ b/src/safari/app/desktop/safari/SafariExtensionViewController.swift @@ -173,6 +173,21 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe let pasteboard = NSPasteboard.general m?.responseData = pasteboard.pasteboardItems?.first?.string(forType: .string) replyMessage(message: m!) + } else if command == "downloadFile" { + SFSafariApplication.getActiveWindow { win in + win?.getActiveTab(completionHandler: { tab in + tab?.getActivePage { activePage in + activePage?.dispatchMessageToScript(withName: "bitwarden", userInfo: ["msg": m!.data]) + } + }) + } + } else if command == "getAppPath" { + SFSafariExtension.getBaseURI(completionHandler: { uri in + if uri != nil { + m!.responseData = uri!.absoluteString + self.replyMessage(message: m!) + } + }) } } @@ -326,3 +341,9 @@ class TabMessage: Decodable, Encodable { class TabMessageOptions: Decodable, Encodable { var frameId: Int? } + +class DownloadFileMessage: Decodable, Encodable { + var fileName: String + var data: String? + var type: String? +}