JojoBiz
JojoBiz

Reputation: 21

Add site with content blocker extension

Hi I am writing a content blocker app.

  1. In this app I want to allow the user add a website that he wants to block.

  2. How can i do that? I used SFContentBlockerManager.reloadContentBlocker(withIdentifier: blockerIdentifier) but it's just activate filter of block list

  3. Thats my protocol on which i write domain(website) how can implement it here to my blocklist in content blocker extension

extension WhiteBlackViewController: AddDomainViewControllerDelegate {
    
    func addDomain(text: String?) {
        if listSwitch.isOn {
            viewModel.items.append(text ?? "")
            viewModel.filtered.append(text ?? "")
            tableView.reloadData()
        }
    }
 
}
  1. Thats my content blocker:
class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling {
    
        func beginRequest(with context: NSExtensionContext) {
            let documentFolder = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.cyberGuard")
            guard let jsonURL = documentFolder?.appendingPathComponent("whiteList.json") else { return }
            let attachment = NSItemProvider(contentsOf: jsonURL)
            
            let item = NSExtensionItem()
            item.attachments = [attachment!]
            
            context.completeRequest(returningItems: [item], completionHandler: nil)
        }
        
    }

Upvotes: 0

Views: 365

Answers (1)

JojoBiz
JojoBiz

Reputation: 21

So i already has a solution for this. You just need create appGroup between your extension and your app

And after that write in another file json and activate it

    func activateFilterBlock(fileName: String, website: String, realPath: String) {
    viewModel.dictionary.append(["action": ["type": "block"], "trigger": ["url-filter": "\(website)"]])
    let jsonData = try! JSONSerialization.data(withJSONObject: viewModel.dictionary, options: .prettyPrinted)
    if let json = String(data: jsonData, encoding: String.Encoding.utf8) {
        let file = "\(fileName).json"
        if let dir = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.cyberGuard") {
            let path = dir.appendingPathComponent(file)
            do {
                try json.write(to: path, atomically: false, encoding: String.Encoding.utf8)
                let id = "\(realPath).json"
                SFContentBlockerManager.reloadContentBlocker(withIdentifier: id) {error in
                    guard error == nil else {
                        print(error ?? "Error")
                        return
                    }
                    print("Reloaded")
                }
            } catch {
                print(error.localizedDescription)
            }
        }
    }
}

If you want receive this json you just can get it from new created json file

 func getJSON(fileName: String, success: @escaping(Success), onError: @escaping(OnError)) {
    let groupUrl: URL = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "group.cyberGuard")!
    if let path = URL(string: "\(fileName).json", relativeTo: groupUrl) {
        do {
            let data = try Data(contentsOf: path)
            let value = try JSONDecoder().decode(WhiteBlackList.self, from: data)
            items = value.map({ $0.trigger.urlFilter })
            filtered = value.map({ $0.trigger.urlFilter })
            if let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: [String: Any]]] {
                dictionary = json
            }
            success()
        } catch {
            onError(error.localizedDescription)
        }
    }
}

If it helps, let me know :)

Upvotes: 1

Related Questions