I can send message when received message in override func messageReceived
with from page
prop and dispatchMessageToScript
method, but I don't understand how can I do it in injectOpenWelcomeTab
method. Thanks
import SafariServices
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").
page.getPropertiesWithCompletionHandler { properties in
NSLog("The extension received a message (\(messageName)) from a script injected into (\(String(describing: properties?.url))) with userInfo (\(userInfo ?? [:]))")
//Вот таким образом можем дёргать методы из контроллера
SafariExtensionViewController.shared.popupChangeBackgorundColor()
}
page.dispatchMessageToScript(withName: "openWelcomePage")
}
public func injectOpenWelcomeTab(){
//Here I want send message to injected script
}
}
SFSafariApplication.getActiveWindow is your best friend here.
Just make sure you import SafariServices
func injectOpenWelcomeTab() {
getActivePage {
$0?.dispatchMessageToScript(withName: "openWelcomePage", userInfo: ["foo": bar as Any])
}
}
func getActivePage(completionHandler: @escaping (SFSafariPage?) -> Void) {
SFSafariApplication.getActiveWindow {$0?.getActiveTab {$0?.getActivePage(completionHandler: completionHandler)}}
}