【问题标题】:Links in iframe are not triggering decidePolicyForNewWindowActioniframe 中的链接不会触发 decisionPolicyForNewWindowAction
【发布时间】:2020-11-11 03:40:39
【问题描述】:

我的 webview macos 应用程序中有一个 youtube 播放器的 iframe,并且 iframe 内的大多数链接(<a> 元素)都没有触发 decidePolicyForNewWindowAction 委托。

唯一有效的<a> 元素是频道链接,其他像视频标题、youtube 图标都是无声的,我无法分辨这些<a>s 之间的区别。

那么为什么有些链接无法触发decidePolicyForNewWindowAction

代表文件:https://developer.apple.com/documentation/webkit/webpolicydelegate/1536381-webview?language=objc

iframe youtube 播放器的文档:https://developers.google.com/youtube/iframe_api_reference

【问题讨论】:

    标签: macos iframe webview


    【解决方案1】:

    对于可能感兴趣的人:

    原来这不是iframe 的问题,这是因为一些<a> 没有直接更改url,它调用了一些javascript 来完成这项工作。这是从调用堆栈中检查的。

    在这种情况下,第一件事是需要再连接一个代表,createWebViewWithRequest。默认情况下,此委托返回 nil。所以改成这样:

    - (WebView*)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
    {
        return sender;
    }
    

    不多做,让下面的代码继续。

    第二件事javascript url 请求转到decidePolicyForNavigationAction。所以仍然没有解雇decidePolicyForNewWindowAction,但至少有机会知道。

    在我的例子中,我在 decidePolicyForNavigationAction 中添加了一些条件来区分我的请求和 iframe 的请求,如下所示:

    - (void) webView: (WebView*) sender decidePolicyForNavigationAction: (NSDictionary*) actionInformation
                                                                request: (NSURLRequest*) request
                                                                  frame: (WebFrame*) frame
                                                       decisionListener: (id <WebPolicyDecisionListener>) listener
    {
        NSString* mainDocumentURL = [[request mainDocumentURL] absoluteString];
        NSString* fromYT = @"https://www.youtube.com";
        if ([mainDocumentURL hasPrefix:fromYT]) {
            [[NSWorkspace sharedWorkspace] openURL:url]; // fires Safari
            [listener ignore];
            return;
        }
    
        // normal flow here, you could open it inside the webview or something
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多