简体   繁体   English

如何在WebView中通知我的Cocoa应用程序onClick事件?

[英]How can my Cocoa application be notified of onClick events in a WebView?

I'm loading a WebView in my OSX application and want to receive a notification in my app when the user clicks on an element in the WebView . 我正在我的OSX应用程序中加载WebView ,并希望在用户单击WebView的元素时在我的应用程序中收到通知。 (The WebView is loading a web page that is part of my web application.) WebView正在加载一个属于我的Web应用程序的网页。)

Is this possible? 这可能吗?

I can think of a way to do it (poll a javascript value that gets set on the click event) but I'm hoping there's a more sensible way! 我可以想出一种方法(调查在click事件上设置的javascript值),但我希望有一种更明智的方法!

Since the web page is your web application, then I assume you have control over the html. 由于网页是您的Web应用程序,因此我假设您可以控制html。

Use WebScript . 使用WebScript

Here's a little demo: 这是一个小小的演示:

Objective-C: Objective-C的:

- (void)windowDidLoad{
    [[webView windowScriptObject] setValue:self forKey:@"objcConnector"];
    ....
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector{
    if (aSelector == @selector(elementClicked:)) return NO;
    return YES;
}
-(void)elementClicked:(id)object{
    //object is the id of the element
}

HTML: HTML:

<button id="example" onClick="window.objcConnector.elementClicked_(this.id)">Click me</button>

Subclass NSWindow and catch the mouse down events on the WebView which is actually a WebHTMLView: 子类NSWindow并在WebView上捕获鼠标按下事件,这实际上是一个WebHTMLView:

@interface MyWindow : NSWindow
@end

@implementation MyWindow

- (void)sendEvent:(NSEvent *)event
{

    if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown)
    {
        NSView *deepView = [[self contentView] hitTest:[event locationInWindow]];
        if([[deepView className] isEqualToString:@"WebHTMLView"]){
            return;
        }
    }
    [super sendEvent:event];

}

@end

Then by monitoring the mouse over element delegate method you can track which element was clicked. 然后通过监视鼠标在元素委托方法上,您可以跟踪单击了哪个元素。

I think this delegate method should be useful. 我认为这种委托方法应该是有用的。 Open a link from the WebView would call this method, so you can figure out what to do with it. 从WebView打开一个链接会调用此方法,因此您可以弄清楚如何处理它。

- (void)webView:(WebView *)sender
        decidePolicyForNavigationAction:(NSDictionary *)actionInformation
        request:(NSURLRequest *)request frame:(WebFrame *)frame
        decisionListener:(id<WebPolicyDecisionListener>)listener

Or you can listen to the WebView frame change URL : 或者您可以收听WebView框架更改URL:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame 

Malhal's solution did not work for me in Mac OS. Malhal的解决方案在Mac OS中对我不起作用。 WebView in Mac OS is weird and wkWebView is incomplete. Mac OS中的WebView很奇怪,而且wkWebView不完整。

Having set the first responder to the webView, this one did: 将第一个响应者设置为webView后,这个响应者做了:

- (void)sendEvent:(NSEvent *)event
{

if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown)
{

    if([[self.firstResponder className] isEqualToString:@"WebHTMLView"])
    {
        NSLog(@"MouseDown at: %f, %f",event.locationInWindow.x,event.locationInWindow.y);
        return;
    }
}
[super sendEvent:event];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM