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

fixes to safariapp messaging

This commit is contained in:
Kyle Spearrin 2019-08-15 14:48:02 -04:00
parent 87abab1eb8
commit fcc777cded
3 changed files with 10 additions and 13 deletions

View File

@ -6,8 +6,7 @@ export class SafariApp {
(window as any).bitwardenSafariAppMessageReceiver = (message: any) => {
// tslint:disable-next-line
console.log(message);
message.data = null;
SafariApp.receiveMessageFromApp(message == null ? null : JSON.parse(message));
SafariApp.receiveMessageFromApp(message);
};
}
}

View File

@ -25,12 +25,13 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
let webViewConfig = WKWebViewConfiguration()
let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent("app/popup/index.html")
webViewConfig.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled")
webViewConfig.userContentController.add(self, name: "bitwardenApp")
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: parentWidth, height: parentHeight), configuration: webViewConfig)
webView.navigationDelegate = self
webView.allowsLinkPreview = false
webView.loadFileURL(html, allowingReadAccessTo:bundleURL)
webView.loadFileURL(html, allowingReadAccessTo: bundleURL)
webView.alphaValue = 0.0;
self.view.addSubview(webView)
}
@ -57,11 +58,11 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
if(m == nil) {
print("m is nil")
} else {
let command = m?.command ?? "\"null\""
let command = m?.command ?? "null"
print(command)
if(command == "storage_get") {
let obj = UserDefaults.standard.string(forKey: m!.data!) ?? "null"
m!.data = obj
let obj = UserDefaults.standard.string(forKey: m!.data!)
m!.responseData = obj
replyMessage(message: m!)
} else if(command == "storage_save") {
let data : StorageData? = jsonDeserialize(json: m!.data)
@ -70,21 +71,17 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
} else {
UserDefaults.standard.set(data?.obj, forKey: data!.key)
}
m!.data = nil
replyMessage(message: m!)
} else if(command == "storage_remove") {
UserDefaults.standard.removeObject(forKey: m!.data!)
m!.data = nil
replyMessage(message: m!)
} else if(command == "getLocaleStrings") {
let language = m!.data
m!.data = nil
let bundleURL = Bundle.main.resourceURL!.absoluteURL
let messagesUrl = bundleURL.appendingPathComponent("app/_locales/en/messages.json")
do {
let json = try String(contentsOf: messagesUrl, encoding: .utf8)
webView.evaluateJavaScript("window.bitwardenLocaleStrings = \(json);", completionHandler: nil)
m!.data = nil
} catch { }
replyMessage(message: m!)
}
@ -93,8 +90,8 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
}
func replyMessage(message: AppMessage) {
let json = (jsonSerialize(obj: message) ?? "\"null\"").replacingOccurrences(of: "`", with: "\\`")
webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver(`\(json)`);", completionHandler: nil)
let json = (jsonSerialize(obj: message) ?? "null")
webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver(\(json));", completionHandler: nil)
}
}
@ -126,6 +123,7 @@ class AppMessage : Decodable, Encodable {
var id: String
var command: String
var data: String?
var responseData: String?
}
class StorageData : Decodable, Encodable {

View File

@ -19,7 +19,7 @@ export default class BrowserStorageService implements StorageService {
async get<T>(key: string): Promise<T> {
if (this.isSafari) {
const obj = await SafariApp.sendMessageToApp('storage_get', key);
return JSON.parse(obj) as T;
return obj == null ? null : JSON.parse(obj) as T;
} else {
return new Promise((resolve) => {
this.chromeStorageApi.get(key, (obj: any) => {