最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - How to block Javascript alert|confirm|promt in UIWebView before render completed? - Stack Overflow

programmeradmin1浏览0评论

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 pletes.

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.

Similar questions:

  • Capture (and prevent) alert() modal in UIWebView
  • Can I handle alert inside UIWebViewDelegate?
  • UIWebView: Can I disable the javascript alert() inside any web page?

Any pointers on this?

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 pletes.

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.

Similar questions:

  • Capture (and prevent) alert() modal in UIWebView
  • Can I handle alert inside UIWebViewDelegate?
  • UIWebView: Can I disable the javascript alert() inside any web page?

Any pointers on this?

Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Feb 5, 2014 at 12:46 ABeanSitsABeanSits 1,7251 gold badge18 silver badges35 bronze badges 3
  • can you move the script that create the alert to one line above </body>? – DrogoNevets Commented Feb 5, 2014 at 14:58
  • Are you suggesting that I preload the HTML and parse-out the alerts code and move it? I was looking for a bit more robust solution. – ABeanSits Commented Feb 5, 2014 at 16:09
  • if you have no control over anything else thn yes i guess i am, cant see how else you might do it if you dont override alert – DrogoNevets Commented Feb 6, 2014 at 8:53
Add a ment  | 

2 Answers 2

Reset to default 9

After experimenting a bit with the suggested solutions regarding injecting a Javascript I fell back on the native platform.

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;.

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

Importing this category in the view controller that holds the UIWebView will block all instances of UIAlertView and in turn also all JS alerts.

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).

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/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 plex web applications one has to really start thinking because not all browsers interpred javascript the sameway.

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.

发布评论

评论列表(0)

  1. 暂无评论