As we know, we can use WKContentRuleList to block url requests/cookies or perform other actions in WKWebView. Is there any way that we can know what has been blocked by the WKWebView based on that WKContentRuleList?
How to know what's blocked by WKContentRuleList
1.3k views Asked by miscaper AtThere are 3 answers
Do you mean you want to get what all are the rules in the list or you want to lookup the rule list? Then you can use below api's of WKContentRuleListStore https://developer.apple.com/documentation/webkit/wkcontentruleliststore
// Gets the identifiers for all rules lists in the store.
func getAvailableContentRuleListIdentifiers((([String]?) -> Void)!)
// Searches for a specific rules list in the store.
func lookUpContentRuleList(forIdentifier: String!, completionHandler:
((WKContentRuleList?, Error?) -> Void)!)
You can also refer the answer for question below, if you require more details: block ads from url loaded in WKWebView
UPDATE: Check this thread if it will be of any help How to show the inspector within your WKWebView based desktop app?
You can do it with the WebKit SDK. Using WKScriptMessageHandler : https://developer.apple.com/documentation/webkit/wkscriptmessagehandler/1396222-usercontentcontroller
You can find a sample of this technic in the open source project iOS browser DuckDuckGo : https://github.com/duckduckgo/iOS
Have a look to this file : https://github.com/duckduckgo/iOS/blob/develop/Core/ContentBlockerRulesUserScript.swift
I have some level of certainty that there isn't a simple way to just retrieve this information using public APIs. As such, I've put together a javascript work-around that's "good enough" solution for my purposes. It attempts to extract the resources from the parsed html and then it compares them to the loaded resources, as obtained from the window.performance module. The main caveat is that some resource types aren't handled at all, while others are probably missed.
Obviously, it should be called after the page has fully loaded what it's going to load. Usually, this would be done from the 'webViewDidFinishNavigation' delegate method. The provided completion argument is closure that is called with an array of the blocked resources as the single parameter.
This first part is a function to build the javascript to extract the resources from the page. Stackoverflow seems to format things better with this split out.
This next part is the function that is called from the didFinishNavigation delegate.