webView:decisionListener不适用于iframe

时间:2018-11-12 16:09:09

标签: macos cocoa iframe webkit

我使用可可调用来设置webView:decidePolicyForNavigationAction:request:frame:decisionListener:

通过此回调,我可以解码一些用户单击的链接的处理方式。但是可悲的是,如果内部有一些iframe,则无法获得iframe内部链接的回调。

我尝试使用findFrameNamed:函数获取iframe的webFrame(似乎可以正常工作),然后获取底层的webView在那里设置DecisionListener,但不会触发。

有任何提示吗?

PS。我使用PureBasic,手动调用Cocoa函数。因此,如果有帮助,我只能添加我的PB代码:

Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")

ProcedureC PolicyDecisionMaker(obj, sel, webView, actionInformation, request, frameName, listener)
  ; handle all links like it is _blank target. We do not want something else in regify client.
  Protected URL = CocoaMessage(0, request, "URL")
  Protected URLText.s = PeekS(CocoaMessage(0, CocoaMessage(0, URL, "absoluteString"), "UTF8String"), -1, #PB_UTF8)
  Debug "URL: " + URLText.s
EndProcedure

class_addMethod(delegateClass, 
                sel_registerName("webView:decidePolicyForNavigationAction:request:frame:decisionListener:"), 
                @PolicyDecisionMaker(), 
                "v@:@@@@@")

Procedure.i SetMacWebgadgetTargetSupport(WebgadgetID.i, iFrameName.s = "")
  If iFrameName.s = ""
    CocoaMessage(0, GadgetID(WebgadgetID.i), "setPolicyDelegate:", appDelegate)
    ProcedureReturn #True
  EndIf

  Protected macMainFrame = CocoaMessage(0, GadgetID(WebgadgetID), "mainFrame")
  Protected maciFrameView = CocoaMessage(0, macMainFrame, "findFrameNamed:$", @iFrameName.s); get iframe
  If maciFrameView <> 0
    ; Set appDelegate on the given iFrame
    Protected macWebView = CocoaMessage(0, maciFrameView, "webView") ; get webView object
    CocoaMessage(0, macWebView, "setPolicyDelegate:", appDelegate)
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False

EndProcedure

以上内容在普通HTML(例如file://somefile.html)上可以正常工作。但是,对于包含链接的iFrame来说,它失败。

0 个答案:

没有答案