mirror of
https://github.com/bitwarden/browser
synced 2025-01-01 20:57:53 +01:00
process safari tab on app messages
This commit is contained in:
parent
630d825ed5
commit
80314e766f
@ -9,7 +9,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
safari.self.addEventListener('message', (msgEvent: any) => {
|
||||
init2fa(msgEvent.message);
|
||||
init2fa(JSON.parse(msgEvent.message.msg));
|
||||
}, false);
|
||||
|
||||
function init2fa(msg: any) {
|
||||
|
@ -59,7 +59,10 @@ export class SafariApp {
|
||||
if ((message.id == null || message.id === '') && message.command === 'app_message') {
|
||||
try {
|
||||
const msg = JSON.parse(message.data);
|
||||
SafariApp.sendMessageToListeners(msg, 'app_message', null);
|
||||
SafariApp.sendMessageToListeners(msg, {
|
||||
id: 'app_message',
|
||||
tab: message.senderTab,
|
||||
}, null);
|
||||
} catch { }
|
||||
} else if (message.id != null && (window as any).bitwardenSafariAppRequests.has(message.id)) {
|
||||
const p = (window as any).bitwardenSafariAppRequests.get(message.id);
|
||||
|
@ -17,7 +17,7 @@ document.addEventListener('DOMContentLoaded', (event) => {
|
||||
bitwardenFrameId: (window as any).__bitwardenFrameId,
|
||||
});
|
||||
safari.self.addEventListener('message', (msgEvent: any) => {
|
||||
const msg = msgEvent.message;
|
||||
const msg = JSON.parse(msgEvent.message.msg);
|
||||
if (msg.bitwardenFrameId != null && (window as any).__bitwardenFrameId !== msg.bitwardenFrameId) {
|
||||
return;
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ document.addEventListener('DOMContentLoaded', (event) => {
|
||||
bitwardenFrameId: (window as any).__bitwardenFrameId,
|
||||
});
|
||||
safari.self.addEventListener('message', (msgEvent: any) => {
|
||||
const msg = msgEvent.message;
|
||||
const msg = JSON.parse(msgEvent.message.msg);
|
||||
if (msg.bitwardenFrameId != null && (window as any).__bitwardenFrameId !== msg.bitwardenFrameId) {
|
||||
return;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
|
||||
safari.self.addEventListener('message', (msgEvent: any) => {
|
||||
doDownload(msgEvent.message);
|
||||
doDownload(JSON.parse(msgEvent.message.msg));
|
||||
}, false);
|
||||
|
||||
function doDownload(msg: any) {
|
||||
|
@ -9,7 +9,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
responseCommand: responseCommand
|
||||
});
|
||||
safari.self.addEventListener('message', (msgEvent) => {
|
||||
const msg = msgEvent.message;
|
||||
const msg = JSON.parse(msgEvent.message.msg);
|
||||
if (msg.command === responseCommand && msg.data) {
|
||||
i18n = msg.data.i18n;
|
||||
load();
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user