Cocoa WebView中如何识别用户发起的请求?

发布于 2025-01-04 07:58:18 字数 554 浏览 5 评论 0原文

我有一个基于 WebKit 的应用程序。当用户启动打开新页面的操作(例如单击链接)时,我想拦截该 URL 并选择在我的应用程序中打开它,或者将其踢出到 Safari。

理想情况下,我想识别加载序列中的第一个请求,或者直接由用户操作产生的请求。或者,如果我能够识别框架中顶级 URL 的更改,则可能就足够了,但需要注意的是,如果我决定将其退回到 Safari,则需要能够取消该请求。

我已经实现了 webView:decidePolicyForNavigationAction:request:frame:decisionListener,并且我可以看到页面加载期间发出的所有请求。由用户触发的请求,以及由 Javascript、REST 调用等启动的请求。

对于简单的链接,我可以从操作信息中获取WebActionNavigationType。但是,如果用户使用 Javascript onclick() 处理程序单击某个元素,该元素又导航到不同的页面,则导航类型将为“其他”,并且与所有其他 Javascript 请求(即由原始请求产生的辅助 REST 调用)无法区分。要求)。

还有其他人解决过这个问题吗?有什么指点吗?谢谢!

I have a WebKit-based application. When the user initiates an action that opens a new page (such as clicking on a link), I would like to intercept that URL and choose to to open it in my application, or kick it out to Safari.

Ideally, I'd like to identify the first request in a loading sequence, or the one that directly resulted from a user action. Alternatively, it might be enough if I could identify changes to the top-level URL in a frame, with the caveat that I need to be able to cancel the request if I decide to bounce it to Safari instead.

I have implemented webView:decidePolicyForNavigationAction:request:frame:decisionListener, and I can see all of the requests made during the loading of the page. Both the requests triggered by the user, as well as those kicked off by Javascript, REST calls, that kind of thing.

For simple links, I can get the WebActionNavigationType from the action information. But if the user clicks on an element with a Javascript onclick() handler, which in turn navigates to a different page, the navigation type will be "Other", and indistinguishable from all other Javascript requests (i.e. secondary REST calls resulting from the original request).

Any one else tackled this? Any pointers? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-01-11 07:58:18

我发现了一些似乎对我有用的东西。在decidePolicyForNavigationAction中,此测试将确定它是否是用户启动的操作:

BOOL isUserAction = (
    [frame isEqual:[sender mainFrame]] &&
    [[request URL] isEqualTo:[actionInformation objectForKey:WebActionOriginalURLKey]]
); 

I figured out something that seems to work for me. In decidePolicyForNavigationAction, this test will determine if it was a user-initiated action:

BOOL isUserAction = (
    [frame isEqual:[sender mainFrame]] &&
    [[request URL] isEqualTo:[actionInformation objectForKey:WebActionOriginalURLKey]]
); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文