mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
process safari tab on app messages
This commit is contained in:
@@ -17,10 +17,12 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
|
||||
override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String: Any]?) {
|
||||
// This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message").
|
||||
if messageName == "bitwarden" {
|
||||
page.getPropertiesWithCompletionHandler { _ in
|
||||
page.getPropertiesWithCompletionHandler { properties in
|
||||
// NSLog("The extension received a message (\(messageName)) from a script injected into (\(String(describing: properties?.url))) with userInfo (\(userInfo ?? [:]))")
|
||||
DispatchQueue.main.async {
|
||||
SafariExtensionViewController.shared.sendMessage(msg: userInfo)
|
||||
makeSenderTabObject(page: page, props: properties, complete: { senderTab in
|
||||
self.sendMessage(msg: userInfo, sender: senderTab)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,7 +44,51 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
|
||||
|
||||
override func popoverWillShow(in _: SFSafariWindow) {
|
||||
DispatchQueue.main.async {
|
||||
SafariExtensionViewController.shared.sendMessage(msg: ["command": "reloadPopup"])
|
||||
self.sendMessage(msg: ["command": "reloadPopup"], sender: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func sendMessage(msg: [String: Any]?, sender: Tab? = nil) {
|
||||
if SafariExtensionViewController.shared.webView == nil {
|
||||
return
|
||||
}
|
||||
let newMsg = AppMessage()
|
||||
newMsg.command = "app_message"
|
||||
newMsg.senderTab = sender
|
||||
do {
|
||||
let jsonData = try JSONSerialization.data(withJSONObject: msg as Any, options: [])
|
||||
newMsg.data = String(data: jsonData, encoding: .utf8)
|
||||
} catch let error {
|
||||
print("error converting to json: \(error)")
|
||||
}
|
||||
SafariExtensionViewController.shared.replyMessage(message: newMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func makeSenderTabObject(page: SFSafariPage, props: SFSafariPageProperties?, complete: @escaping (Tab) -> Void) {
|
||||
let t = Tab()
|
||||
t.title = props?.title
|
||||
t.url = props?.url?.absoluteString
|
||||
page.getContainingTab { tab in
|
||||
tab.getContainingWindow(completionHandler: { win in
|
||||
win?.getActiveTab(completionHandler: { activeTab in
|
||||
t.active = activeTab != nil && tab == activeTab
|
||||
SFSafariApplication.getAllWindows(completionHandler: { allWins in
|
||||
t.windowId = allWins.firstIndex(of: win!) ?? -100
|
||||
let winGroup = DispatchGroup()
|
||||
for allWin in allWins {
|
||||
winGroup.enter()
|
||||
allWin.getAllTabs { allWinTabs in
|
||||
t.index = allWinTabs.firstIndex(of: tab) ?? -1
|
||||
winGroup.leave()
|
||||
}
|
||||
}
|
||||
winGroup.notify(queue: .main) {
|
||||
t.id = "\(t.windowId)_\(t.index)"
|
||||
complete(t)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,21 +169,6 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
||||
let json = (jsonSerialize(obj: message) ?? "null")
|
||||
webView.evaluateJavaScript("window.bitwardenSafariAppMessageReceiver(\(json));", completionHandler: nil)
|
||||
}
|
||||
|
||||
func sendMessage(msg: [String: Any]?) {
|
||||
if webView == nil {
|
||||
return
|
||||
}
|
||||
let newMsg = AppMessage()
|
||||
newMsg.command = "app_message"
|
||||
do {
|
||||
let jsonData = try JSONSerialization.data(withJSONObject: msg as Any, options: [])
|
||||
newMsg.data = String(data: jsonData, encoding: .utf8)
|
||||
} catch let error {
|
||||
print("error converting to json: \(error)")
|
||||
}
|
||||
replyMessage(message: newMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func processWindowsForTabs(wins: [SFSafariWindow], options: TabQueryOptions?, complete: @escaping ([Tab]) -> Void) {
|
||||
@@ -289,6 +274,7 @@ class AppMessage: Decodable, Encodable {
|
||||
var data: String?
|
||||
var responseData: String?
|
||||
var responseError: Bool?
|
||||
var senderTab: Tab?
|
||||
}
|
||||
|
||||
class StorageData: Decodable, Encodable {
|
||||
|
||||
Reference in New Issue
Block a user