Skip to content

Commit

Permalink
Add support for filterlist exceptions in CPM
Browse files Browse the repository at this point in the history
  • Loading branch information
muodov committed Dec 6, 2024
1 parent bdcbdbf commit 064e7cf
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions DuckDuckGo/Autoconsent/AutoconsentUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ extension AutoconsentUserScript {
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
}
}

Check failure on line 197 in DuckDuckGo/Autoconsent/AutoconsentUserScript.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
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) {
Expand Down Expand Up @@ -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": [
Expand All @@ -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
Expand Down

0 comments on commit 064e7cf

Please sign in to comment.