Content Blocker - Swift | iOS

2.3k views Asked by At

I'm trying to retrieve an URL when the user is visiting a website on Safari.

I had the idea to use Content Blocker but I don't find any method to get the URL of a web site in my blockList.json file.

I tried to print something in ContentBlockerRequestHandler class but nothing happened.

Here is the class :

class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling {

    func beginRequest(with context: NSExtensionContext) {

        let attachment = NSItemProvider(contentsOf: Bundle.main.url(forResource: "blockerList", withExtension: "json"))!

        print("test")

        let item = NSExtensionItem()
        item.attachments = [attachment]

        context.completeRequest(returningItems: [item], completionHandler: nil)

    }

}

Does an alternative solution exists, or what is the solution ?

Thanks

1

There are 1 answers

6
Mihir Thanekar On

UPDATE: You can make an action and trigger and just block everything on the page by selecting the html tag. You cannot get the URL that the user is on because that is a privacy violation. Instead you have predefined rules on how and when safari should block something. So, if you wanted to hide the whole page you could use the json rule:

    "action":{
          "type": "css-display-none"
          "selector": "#html"
     }

The above blocks everything in the html tags by not displaying it. Then you use a trigger dictionary:

     "trigger": {
           "if-domain": ["domaintoblock.com", "blocktodomain.org"]
           "url-filter": ".*"
     }

if-domainis your list of sites to block and url-filter specifies "which resources to apply to when loading"- source 1. The above only hides the element from view. If you want to block the loading altogether use "type": "block" in your action instead. See 6: 25 in the the WWDC video. Content blockers are just JSON array rules. To make one add a target to your project> Application Extension> Content Blocker Extension. Each time your Safari loads, your app extension will load it's byte code and you'll have to give Safari your blockerList.json in beginRequestWithExtensionContext(context: NSExtensionContext). See 9: 29 in the WWDC video. From there you are done, it's automatic. That should be plenty to get you started. Let me know if I helped. Thanks a bunch :)

Refer to https://developer.apple.com/videos/play/wwdc2015/511/ for more info on content blocking. For a more detailed, more difficult example see https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/AppExtensionKeys.html

PREVIOUS POST

You may consider looking into Safari Extensions. They have a handy event that may benefit you. SafariBeforeNavigateEvent is called right before page loading. You can use another method called preventDefault() to stop the page from loading. So using this in conjunction with your blockList.json file, you could check whether the event called had a url that's in blockList.json. You'd need to parse the JSON as an object first in order to use it. From there it's just a for loop in the event handler. You need another property of the SafariBeforeNavigateEvent which is the url. Let me know if this helped, thank you :)

https://developer.apple.com/documentation/safariextensions/safaribeforenavigateevent https://developer.apple.com/documentation/safariextensions/safarievent/1635502-preventdefault https://developer.apple.com/documentation/safariextensions/safarievent https://developer.apple.com/documentation/safariextensions