简体   繁体   English

如何在渲染完成之前阻止UIWebView中的Javascript alert | confirm | promt?

[英]How to block Javascript alert|confirm|promt in UIWebView before render completed?

There are multiple questions on SO already addressing similar issues but none answers my exact type of question. SO上有很多问题已经解决了类似的问题,但没有一个问题可以回答我的确切类型。 Most of the answers involves running a JS snippet and overloading the alert method as such: window.alert = function() {}; 大多数答案涉及运行JS片段并重载警报方法: 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. 我遇到的问题是加载的网页(我无法控制内容)在整个页面的呈现完成之前打开警报。

Because of this I cannot use the: - (void)webViewDidFinishLoad:(UIWebView *)webView delegate method to run the JS snippet. 因此,我不能使用: - (void)webViewDidFinishLoad:(UIWebView *)webView委托方法来运行JS片段。 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. 另外在- (void)webViewDidStartLoad:(UIWebView *)webView运行相同的片段- (void)webViewDidStartLoad:(UIWebView *)webView对我没有任何帮助,因为它在加载DOM之前执行。

Similar questions: 类似的问题:

Any pointers on this? 有关于此的任何指示?

After experimenting a bit with the suggested solutions regarding injecting a Javascript I fell back on the native platform. 在尝试了一些关于注入Javascript的建议解决方案后,我回到了原生平台上。

Since a UIWebView translates all Javascript alerts into native UIAlertViews it is fairly simple to block it on the native end. 由于UIWebView将所有Javascript警报转换为本机UIAlertViews ,因此在本机端阻止它是相当简单的。 Looking into UIAlertView.h there is only one public method for showing an alert which is conveniently called: - (void)show; 查看UIAlertView.h只有一种公共方法可以显示一个方便调用的警报: - (void)show; .

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 . 我最终使用了一个类别,因为文档提到了子类化UIAlertView的警告。

@interface UIAlertView (Blocker)
@end

#import "UIAlertView+Blocker.h"

@implementation UIAlertView (Blocker)

- (void)show {
   return;
}
@end

Importing this category in the view controller that holds the UIWebView will block all instances of UIAlertView and in turn also all JS alerts. 在包含UIWebView的视图控制器中导入此类别将阻止所有UIAlertView实例以及所有JS警报。

I find this solution more robust since it does not rely on injecting Javascripts, changing HTML or using a third party library. 我发现这个解决方案更加健壮,因为它不依赖于注入Javascripts,更改HTML或使用第三方库。 It is also unrelated to timing (the JS-alert code can be in the header, body or in some third party lib). 它与计时无关(JS警报代码可以在标题,正文或某些第三方lib中)。

It seems that the result may vary based on the rendering engine, there are a few ways, what seemed to work for me what this: 似乎结果可能会因渲染引擎而异,有几种方法,对我来说什么似乎对我有用:

alert("something");
alert = function(){};
alert("awesome");

http://jsfiddle.net/g5S5M/ I know that the V8 engine that chrome uses is not the same as the one that firefox or IE use, so when making complex web applications one has to really start thinking because not all browsers interpred javascript the sameway. http://jsfiddle.net/g5S5M/我知道chrome使用的V8引擎与firefox或IE使用的引擎不同,因此在制作复杂的Web应用程序时,必须真正开始思考,因为并非所有浏览器都在使用javascript以同样的方式。

One thing that you might want to look at is third party libraries that override this function, as in if you tell alert to be blank and right after another script tells it not to be blank, so ether remove that library, change the execution order or modify that library. 您可能想要查看的一件事是覆盖此功能的第三方库,如果您将警报告知为空白并且在另一个脚本告诉它不是空白之后,那么以太删除该库,更改执行顺序或修改该库。

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

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