1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-18 02:41:15 +02:00

show popup, reload extension, and formatting

This commit is contained in:
Kyle Spearrin 2019-08-19 20:45:06 -04:00
parent 2a7e361300
commit 41be76a107

View File

@ -50,111 +50,110 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
} }
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) { func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "bitwardenApp" { if message.name != "bitwardenApp" {
let messageBody = message.body as! String return
print(messageBody) }
let m: AppMessage? = jsonDeserialize(json: messageBody) let messageBody = message.body as! String
if m == nil { // print(messageBody)
print("m is nil") let m: AppMessage? = jsonDeserialize(json: messageBody)
if m == nil {
// print("m is nil")
return
}
let command = m!.command
// print(command)
if command == "storage_get" {
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)
if data?.obj == nil {
UserDefaults.standard.removeObject(forKey: data!.key)
} else { } else {
let command = m?.command ?? "null" UserDefaults.standard.set(data?.obj, forKey: data!.key)
print(command) }
if command == "storage_get" { replyMessage(message: m!)
let obj = UserDefaults.standard.string(forKey: m!.data!) } else if command == "storage_remove" {
m!.responseData = obj UserDefaults.standard.removeObject(forKey: m!.data!)
replyMessage(message: m!) replyMessage(message: m!)
} else if command == "storage_save" { } else if command == "getLocaleStrings" {
let data: StorageData? = jsonDeserialize(json: m!.data) let language = m!.data ?? "en"
if data?.obj == nil { let bundleURL = Bundle.main.resourceURL!.absoluteURL
UserDefaults.standard.removeObject(forKey: data!.key) let messagesUrl = bundleURL.appendingPathComponent("app/_locales/\(language)/messages.json")
} else { do {
UserDefaults.standard.set(data?.obj, forKey: data!.key) let json = try String(contentsOf: messagesUrl, encoding: .utf8)
} webView.evaluateJavaScript("window.bitwardenLocaleStrings = \(json);", completionHandler: nil)
replyMessage(message: m!) } catch {}
} else if command == "storage_remove" { replyMessage(message: m!)
UserDefaults.standard.removeObject(forKey: m!.data!) } else if command == "tabs_query" {
replyMessage(message: m!) let options: TabQueryOptions? = jsonDeserialize(json: m!.data)
} else if command == "getLocaleStrings" { if options?.currentWindow ?? false {
let language = m!.data ?? "en" SFSafariApplication.getActiveWindow { win in
let bundleURL = Bundle.main.resourceURL!.absoluteURL processWindowsForTabs(wins: [win!], options: options
let messagesUrl = bundleURL.appendingPathComponent("app/_locales/\(language)/messages.json") , complete: { tabs in
do { m!.responseData = jsonSerialize(obj: tabs)
let json = try String(contentsOf: messagesUrl, encoding: .utf8) self.replyMessage(message: m!)
webView.evaluateJavaScript("window.bitwardenLocaleStrings = \(json);", completionHandler: nil) })
} catch {} }
replyMessage(message: m!) } else {
} else if command == "tabs_query" { SFSafariApplication.getAllWindows { wins in
let options: TabQueryOptions? = jsonDeserialize(json: m!.data) processWindowsForTabs(wins: wins, options: options
if options?.currentWindow ?? false { , complete: { tabs in
SFSafariApplication.getActiveWindow { win in m!.responseData = jsonSerialize(obj: tabs)
processWindowsForTabs(wins: [win!], options: options self.replyMessage(message: m!)
, complete: { tabs in })
m!.responseData = jsonSerialize(obj: tabs)
self.replyMessage(message: m!)
})
}
} else {
SFSafariApplication.getAllWindows { wins in
processWindowsForTabs(wins: wins, options: options
, complete: { tabs in
m!.responseData = jsonSerialize(obj: tabs)
self.replyMessage(message: m!)
})
}
}
} else if command == "tabs_message" {
let tabMsg: TabMessage? = jsonDeserialize(json: m!.data)
SFSafariApplication.getAllWindows { wins in
var theWin: SFSafariWindow?
var winIndex = 0
for win in wins {
if tabMsg?.tab.windowId == winIndex {
theWin = win
break
}
winIndex = winIndex + 1
}
if theWin != nil {
var theTab: SFSafariTab?
theWin!.getAllTabs { tabs in
var tabIndex = 0
for tab in tabs {
if tabMsg?.tab.index == tabIndex {
theTab = tab
break
}
tabIndex = tabIndex + 1
}
if theTab != nil {
theTab!.getActivePage { activePage in
if activePage != nil {
activePage?.dispatchMessageToScript(withName: "bitwarden", userInfo: ["msg": tabMsg!.obj])
}
}
}
}
}
}
} else if command == "hidePopover" {
dismissPopover()
replyMessage(message: m!)
} else if command == "showPopover" {
// TODO
replyMessage(message: m!)
} else if command == "reloadExtension" {
// TODO
replyMessage(message: m!)
} else if command == "copyToClipboard" {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString(m!.data ?? "", forType: NSPasteboard.PasteboardType.string)
replyMessage(message: m!)
} else if command == "readFromClipboard" {
let pasteboard = NSPasteboard.general
m?.responseData = pasteboard.pasteboardItems?.first?.string(forType: .string)
replyMessage(message: m!)
} }
} }
} else if command == "tabs_message" {
let tabMsg: TabMessage? = jsonDeserialize(json: m!.data)
SFSafariApplication.getAllWindows { wins in
var theWin: SFSafariWindow?
var winIndex = 0
for win in wins {
if tabMsg?.tab.windowId == winIndex {
theWin = win
break
}
winIndex = winIndex + 1
}
var theTab: SFSafariTab?
theWin?.getAllTabs { tabs in
var tabIndex = 0
for tab in tabs {
if tabMsg?.tab.index == tabIndex {
theTab = tab
break
}
tabIndex = tabIndex + 1
}
theTab?.getActivePage { activePage in
activePage?.dispatchMessageToScript(withName: "bitwarden", userInfo: ["msg": tabMsg!.obj])
}
}
}
} else if command == "hidePopover" {
dismissPopover()
replyMessage(message: m!)
} else if command == "showPopover" {
SFSafariApplication.getActiveWindow { win in
win?.getToolbarItem(completionHandler: { item in
item?.showPopover()
self.replyMessage(message: m!)
})
}
} else if command == "reloadExtension" {
webView?.reload()
replyMessage(message: m!)
} else if command == "copyToClipboard" {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString(m!.data ?? "", forType: NSPasteboard.PasteboardType.string)
replyMessage(message: m!)
} else if command == "readFromClipboard" {
let pasteboard = NSPasteboard.general
m?.responseData = pasteboard.pasteboardItems?.first?.string(forType: .string)
replyMessage(message: m!)
} }
} }