From 064e7cfebe7cdfdb6518452a2700039e0872b1cc Mon Sep 17 00:00:00 2001 From: Maxim Tsoy Date: Fri, 6 Dec 2024 15:51:27 +0100 Subject: [PATCH] Add support for filterlist exceptions in CPM --- .../Autoconsent/AutoconsentUserScript.swift | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/DuckDuckGo/Autoconsent/AutoconsentUserScript.swift b/DuckDuckGo/Autoconsent/AutoconsentUserScript.swift index 97c1370bca..ea4cc34ce7 100644 --- a/DuckDuckGo/Autoconsent/AutoconsentUserScript.swift +++ b/DuckDuckGo/Autoconsent/AutoconsentUserScript.swift @@ -194,6 +194,24 @@ extension AutoconsentUserScript { replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection } } + + func matchDomainList(domain: String?, domainsList: [String]) -> Bool { + guard let domain = domain else { return false } + let trimmedDomains = domainsList.filter { !$0.trimmingWhitespace().isEmpty } + + // Break domain apart to handle www.* + var tempDomain = domain + while tempDomain.contains(".") { + if trimmedDomains.contains(tempDomain) { + return true + } + + let comps = tempDomain.split(separator: ".") + tempDomain = comps.dropFirst().joined(separator: ".") + } + + return false + } @MainActor func handleInit(message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) { @@ -236,9 +254,10 @@ extension AutoconsentUserScript { } let remoteConfig = self.config.settings(for: .autoconsent) let disabledCMPs = remoteConfig["disabledCMPs"] as? [String] ?? [] - let enableFilterList = config.isSubfeatureEnabled(AutoconsentSubfeature.filterlist) + let filterlistExceptions = remoteConfig["filterlistExceptions"] as? [String] ?? [] + let enableFilterList = config.isSubfeatureEnabled(AutoconsentSubfeature.filterlist) && !self.matchDomainList(domain: topURLDomain, domainsList: filterlistExceptions) - replyHandler([ + let autoconsentConfig = [ "type": "initResp", "rules": nil, // rules are bundled with the content script atm "config": [ @@ -251,7 +270,10 @@ extension AutoconsentUserScript { "isMainWorld": false, "enableFilterList": enableFilterList ] as [String: Any?] - ] as [String: Any?], nil) + ] as [String: Any?] + Logger.autoconsent.debug("autoconsent config: \(String(describing: autoconsentConfig))") + + replyHandler(autoconsentConfig, nil) } @MainActor