简体   繁体   English

WKWebView在加载任何URL时阻止某些脚本(块跟踪或任何网站中的广告)

[英]WKWebView block certain scripts while loading any URL (Block tracking or Ads in any website)

I'm working on a iOS (swift) Secure Browser Project. 我正在开发一个iOS (swift) Secure Browser项目。 I need to implement one functionality in which we can show users how many tracking scripts are running in current WKWebView and ability to stop those tracking script. 我需要实现一个功能,我们可以向用户显示当前WKWebView中运行的跟踪脚本WKWebView以及停止跟踪脚本的能力。 (Just like Ghostery Privacy Browser ) (就像Ghostery Privacy Browser

I'm able to track and list the scripts embedded in web page (I do this after scanning the content which has already loaded in the delegate didfinishload is called). 我能够跟踪和列出网页中嵌入的脚本(我在扫描已经加载到委托中的内容后执行此操作didfinishload被调用)。 But I'm not sure how do I stop them. 但我不知道如何阻止它们。

For fetching all scripts in webpage I'm reading all script tags in innerHTML . 为了获取网页中的所有脚本,我正在读取innerHTML所有脚本标记。

Please help me stop executing certain scripts in WKWebView . 请帮我停止在WKWebView执行某些脚本。

Thank you in advance 先感谢您

I don't think that reading all script tags is the way to detect scripts loaded by web page as it is very error prone. 我不认为读取所有脚本标签是检测网页加载的脚本的方法,因为它非常容易出错。

For your task you would be better served by using an old UIWebView and implementing custom NSURLProtocol to do blocking/filtering. 对于您的任务,使用旧的UIWebView并实现自定义NSURLProtocol来执行阻止/过滤可以更好地满足您的需求。 It is not possible to intercept all the request made by WKWebView (as it is loaded out of process and you get as much callbacks as Apple has implemented in WKNavigationDelegate). 无法拦截WKWebView发出的所有请求(因为它是在进程外加载的,并且您获得了与Apple在WKNavigationDelegate中实现的回调一样多的回调)。 Contrary you can control everything loaded by UIWebView with simple custom NSURLProtocol implementation. 相反,您可以使用简单的自定义NSURLProtocol实现来控制UIWebView加载的所有内容。

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

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