在Mac上的WKWebView中,如何修改或覆盖上下文菜单? [英] How can the context menu in WKWebView on the Mac be modified or overridden?

查看:381
本文介绍了在Mac上的WKWebView中,如何修改或覆盖上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OS X应用程序中使用WKWebView.我想覆盖用户 Control +在WKWebView中单击或右键单击时出现的上下文菜单,但是我找不到实现此目的的方法.

I'm using a WKWebView in a Mac OS X application. I want to override the contextual menu that appears when the user Control + clicks or right clicks in the WKWebView, but I cannot find a way to accomplish this.

应注意,上下文菜单根据WKWebView的状态以及调用上下文菜单时鼠标下方的元素而变化.例如,当鼠标悬停在内容的空"部分上时,上下文菜单只有一个重新加载"项,而右键单击链接将显示选项打开链接",在新窗口中打开链接"和很快.如果可能,对这些不同的菜单进行精细控制将很有帮助.

It should be noted that the context menu changes depending on the state of the WKWebView and what element is under the mouse when the context menu is invoked. For example, the context menu only has a single "Reload" item when the mouse is over an "empty" part of the content, whereas right clicking a link presents the options "Open Link", "Open Link In New Window", and so on. It would be helpful to have granular control over these different menus if possible.

较早的WebUIDelegate提供了- webView:contextMenuItemsForElement:defaultMenuItems: 方法,该方法允许您自定义WebView实例的上下文菜单;我本质上是在寻找WKWebView的这种方法的类似物,或以任何方式复制该功能.

The older WebUIDelegate provides the - webView:contextMenuItemsForElement:defaultMenuItems: method that allows you to customize the context menu for WebView instances; I'm essentially looking for the analog to this method for WKWebView, or any way to duplicate the functionality.

推荐答案

您可以通过在javascript中拦截contextmenu事件,然后通过scriptMessageHandler将事件报告回OSX容器,然后从菜单中弹出菜单来做到这一点. OSX.您可以通过脚本消息的正文字段将上下文传递回以显示适当的菜单,或者为每个菜单使用不同的处理程序.

You can do this by intercepting the contextmenu event in your javascript, reporting the event back to your OSX container through a scriptMessageHandler, then popping up a menu from OSX. You can pass context back through the body field of the script message to show an appropriate menu, or use a different handler for each one.

在目标C中设置回调处理程序:

Setting up callback handler in Objective C:

WKUserContentController *contentController = [[WKUserContentController alloc]init];
[contentController addScriptMessageHandler:self name:@"callbackHandler"];
config.userContentController = contentController;
self.mainWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];

使用jquery的Javascript代码:

Javascript code using jquery:

$(nodeId).on("contextmenu", function (evt) {
   window.webkit.messageHandlers.callbackHandler.postMessage({body: "..."});
   evt.preventDefault();
});

响应目标C:

-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:@"callbackHandler"]) {
      [self popupMenu:message.body];
    }
}

-(void)popupMenu:(NSString *)context {
    NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Context Menu"];
    [theMenu insertItemWithTitle:@"Beep" action:@selector(beep:) keyEquivalent:@"" atIndex:0];
    [theMenu insertItemWithTitle:@"Honk" action:@selector(honk:) keyEquivalent:@"" atIndex:1];
    [theMenu popUpMenuPositioningItem:theMenu.itemArray[0] atLocation:NSPointFromCGPoint(CGPointMake(0,0)) inView:self.view];
}

-(void)beep:(id)val {
    NSLog(@"got beep %@", val);
}

-(void)honk:(id)val {
    NSLog(@"got honk %@", val);
}

这篇关于在Mac上的WKWebView中,如何修改或覆盖上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆