How to block Javascript alert|confirm|promt in UIWebView before render completed?(如何在渲染完成之前阻止 UIWebView 中的 Javascript 警报|确认|提示?)
问题描述
关于 SO 的多个问题已经解决了类似的问题,但没有一个回答我的确切问题类型.大多数答案涉及运行 JS 片段并重载警报方法,如下所示:window.alert = function() {};
There are multiple questions on SO already addressing similar issues but none answers my exact type of question. Most of the answers involves running a JS snippet and overloading the alert method as such: window.alert = function() {};
我遇到的问题是加载的网页(我无法控制内容)在整个页面的呈现完成之前打开一个警报.
The problem I am having is that the webpage loaded (which I have no control over the content) opens an alert before the rendering of the whole page completes.
因此,我无法使用:- (void)webViewDidFinishLoad:(UIWebView *)webView
委托方法来运行 JS 片段.此外,在 - (void)webViewDidStartLoad:(UIWebView *)webView
中运行相同的代码段对我没有任何好处,因为它在加载 DOM 之前执行.
Because of this I cannot use the: - (void)webViewDidFinishLoad:(UIWebView *)webView
delegate method to run the JS snippet. In addition running the same snippet in - (void)webViewDidStartLoad:(UIWebView *)webView
won't do me any good since it executes before the DOM is loaded.
类似问题:
- 在 UIWebView 中捕获(和阻止)alert() 模态
- 我可以在 UIWebViewDelegate 中处理警报吗?
- UIWebView:我可以禁用 javascriptalert() 在任何网页中?
这方面有什么建议吗?
推荐答案
在尝试了一些有关注入 Javascript 的建议解决方案后,我回到了原生平台.
After experimenting a bit with the suggested solutions regarding injecting a Javascript I fell back on the native platform.
由于 UIWebView
将所有 Javascript 警报转换为本机 UIAlertViews
,因此在本机端阻止它相当简单.查看 UIAlertView.h
只有一种公共方法可以方便地显示警报:- (void)show;
.
Since a UIWebView
translates all Javascript alerts into native UIAlertViews
it is fairly simple to block it on the native end. Looking into UIAlertView.h
there is only one public method for showing an alert which is conveniently called: - (void)show;
.
此方法可以很容易地在子类中或通过类别被覆盖.我最终使用了一个类别,因为 文档 提到子类化 UIAlertView
的警告.
This method can easily be either overridden in a subclass or through a category. I ended up using a category since the documentation mentions a warning for subclassing UIAlertView
.
@interface UIAlertView (Blocker)
@end
#import "UIAlertView+Blocker.h"
@implementation UIAlertView (Blocker)
- (void)show {
return;
}
@end
在包含 UIWebView
的视图控制器中导入此类别将阻止 UIAlertView
的所有实例,进而阻止所有 JS 警报.
Importing this category in the view controller that holds the UIWebView
will block all instances of UIAlertView
and in turn also all JS alerts.
我发现这个解决方案更强大,因为它不依赖于注入 Javascript、更改 HTML 或使用第三方库.它也与时间无关(JS-alert 代码可以在 header、body 或某些第三方库中).
I find this solution more robust since it does not rely on injecting Javascripts, changing HTML or using a third party library. It is also unrelated to timing (the JS-alert code can be in the header, body or in some third party lib).
这篇关于如何在渲染完成之前阻止 UIWebView 中的 Javascript 警报|确认|提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在渲染完成之前阻止 UIWebView 中的 Javascript 警报|确认|提示?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01