1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-02 08:40:08 +01:00

download file via content script

This commit is contained in:
Kyle Spearrin 2019-08-21 21:10:38 -04:00
parent 2e609331f3
commit 8dd574bf9a
3 changed files with 39 additions and 6 deletions

View File

@ -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,
command: 'downloaderPageData',
data: {
blobData: data,
blobOptions: blobOptions,
fileName: fileName,
},
}), true);
} else {
const blob = new Blob([blobData], blobOptions);

View File

@ -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);
});

View File

@ -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?
}