I'm having a hard time finding samples that clearly explain how to use WKContentRuleList objects. I read that WKContentRuleList use the same format as Safari content blocker extensions however when I try to compile a content blocker from the sample app AdoptingDeclarativeContentBlockingInSafariWebExtensions it complains about missing a "trigger". I get
Error Domain=WKErrorDomain Code=6 "(null)" UserInfo={NSHelpAnchor=Rule list compilation failed: Invalid trigger object.}
When I try to use the rules from the AdoptingDeclarativeContentBlockingInSafariWebExtensions sample project:
[
{
"id": 1,
"priority": 1,
"action": { "type": "block" },
"condition": {"regexFilter": ".*", "resourceTypes": [ "image" ] }
},
{
"id": 2,
"priority": 1,
"action": { "type": "allow" },
"condition": {"regexFilter": "wikipedia", "resourceTypes": [ "image" ] }
}
]
So if WKContentRuleList requires different keys/value pairs than Safari content blockers are those differences documented anywhere? I can't really find any good info on this.
Thanks in advance.
This is a very good question! The AdoptingDeclarativeContentBlockingInSafariWebExtensions sample code is for the declarativeNetRequest API used by Web Extensions.
This format is documented at: https://developer.apple.com/documentation/safariservices/safari_web_extensions/blocking_content_with_your_safari_web_extension?language=objc and https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/
In Safari, these rules are translated into the WKContentRuleList format, which is documented at: https://webkit.org/blog/3476/content-blockers-first-look/ https://webkit.org/blog/4062/targeting-domains-with-content-blockers/ https://developer.apple.com/documentation/safariservices/creating_a_content_blocker?language=objc
Hope this helps!