获得WkWebView鼠标中键的操作

时间:2018-08-19 14:23:39

标签: javascript swift macos wkwebview

相关

我试图让我的子类WkWebView作用于中间按钮URL,以在新窗口/ webView中显示链接。 WkWebView的上下文菜单中有一个项目可以执行此操作-我将其目标/操作更改为我的视图,但是我希望像在Web浏览器中那样简单,可以做到这一点;中间按钮会产生一个新窗口。

到目前为止,我会这样:

viewDidLoad() {
    //  Watch javascript selection messages
    let controller = webView.configuration.userContentController
    controller.add(self, name: "newWindowWithUrlDetected")

    let js = """
//  https://stackoverflow.com/questions/50846404/how-do-i-get-the-selected-text-from-a-wkwebview-from-objective-c
function getSelectionAndSendMessage()
{
    var txt = document.getSelection().toString() ;
    window.webkit.messageHandlers.newSelectionDetected.postMessage(txt) ;
}
document.onmouseup   = getSelectionAndSendMessage ;
document.onkeyup     = getSelectionAndSendMessage ;

//  https://stackoverflow.com/questions/21224327/how-to-detect-middle-mouse-button-click/21224428
document.body.onclick = function (e) {
  if (e && (e.which == 2 || e.button == 4 )) {
    middleLink;
  }
}
function middleLink()
{
   window.webkit.messageHandlers.newWindowWithUrlDetected.postMessage(this.href) ;
}

//  https://stackoverflow.com/questions/51894733/how-to-get-mouse-over-urls-into-wkwebview-with-swift/51899392#51899392
function sendLink()
{
    window.webkit.messageHandlers.newUrlDetected.postMessage(this.href) ;
}

var allLinks = document.links;
for(var i=0; i< allLinks.length; i++)
{
    allLinks[i].onmouseover = sendLink ;
}
"""
    let script = WKUserScript.init(source: js, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    controller.addUserScript(script)
}

这个想法是,在鼠标悬停时,将报告该链接,但是当操作发生时,我想知道向该视图发送了哪个按钮。

问题在于我没有看到动作,而不是在决策处理程序中;甚至是负载(URLRequest :)本身的替代

override func load(_ request: URLRequest) -> WKNavigation? {
    Swift.print("we got \(request)")
    return super.load(request)
}

因为我没有采取措施,所以我想问题是javascrpt没有“发送”这样的事件并且完全在网页内处理吗?

该事件的按钮如何显示?

导航代表显然无法正常工作吗?

func webView(_ webView: WKWebView,
             decidePolicyFor navigationAction: WKNavigationAction,
             decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

    guard let event = NSApp.currentEvent, event.buttonNumber <= 1 else {
        if let url = navigationAction.request.url {
            Swift.print("newWindow with url:\(String(describing: url))")
            DispatchQueue.main.async {
                self.appDelegate.openURLInNewWindow(url)
            }
        }
        decisionHandler(WKNavigationActionPolicy.cancel)
        return
    }
    decisionHandler(WKNavigationActionPolicy.allow)
    return
}

1 个答案:

答案 0 :(得分:0)

在@Willeke的帮助下,此问题得以解决:

func webView(_ webView: WKWebView,
             decidePolicyFor navigationAction: WKNavigationAction,
             decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    guard navigationAction.buttonNumber <= 1 else {
        if let url = navigationAction.request.url {
            Swift.print("newWindow with url:\(String(describing: url))")
            DispatchQueue.main.async {
                self.appDelegate.openURLInNewWindow(url: url)
            }
        }
        decisionHandler(WKNavigationActionPolicy.cancel)
        return
    }
    decisionHandler(WKNavigationActionPolicy.allow)
}