diff --git a/Sources/TrackerRadarKit/ContentBlockerRule.swift b/Sources/TrackerRadarKit/ContentBlockerRule.swift index 271f09b..8b6882b 100644 --- a/Sources/TrackerRadarKit/ContentBlockerRule.swift +++ b/Sources/TrackerRadarKit/ContentBlockerRule.swift @@ -91,6 +91,11 @@ public struct ContentBlockerRule: Codable, Hashable { return Trigger(urlFilter: filter, unlessDomain: urls, ifDomain: nil, resourceType: nil, loadType: loadTypes, loadContext: nil) } + public static func trigger(urlFilter filter: String, resourceType types: [ResourceType]?, + loadTypes: [LoadType]?, loadContext: [LoadContext]?) -> Trigger { + return Trigger(urlFilter: filter, unlessDomain: nil, ifDomain: nil, resourceType: types, loadType: loadTypes, loadContext: loadContext) + } + public static func trigger(urlFilter filter: String, ifDomain domains: [String]?, resourceType types: [ResourceType]?) -> Trigger { return Trigger(urlFilter: filter, unlessDomain: nil, ifDomain: domains, resourceType: types, loadType: [ .thirdParty ], loadContext: nil) } diff --git a/Sources/TrackerRadarKit/ContentBlockerRulesBuilder.swift b/Sources/TrackerRadarKit/ContentBlockerRulesBuilder.swift index 4a3688c..2fa5645 100644 --- a/Sources/TrackerRadarKit/ContentBlockerRulesBuilder.swift +++ b/Sources/TrackerRadarKit/ContentBlockerRulesBuilder.swift @@ -145,7 +145,13 @@ public struct ContentBlockerRulesBuilder { return [ ContentBlockerRule(trigger: .trigger(urlFilter: urlFilter, unlessDomain: trackerData.relatedDomains(for: tracker.owner)?.wildcards(), loadTypes: loadTypes), - action: .block()) ] + action: .block()), + ContentBlockerRule(trigger: .trigger(urlFilter: urlFilter, + resourceType: [.popup], + loadTypes: loadTypes, + loadContext: [.topFrame]), + action: .ignorePreviousRules()) + ] } private func buildRules(fromRule r: KnownTracker.Rule, diff --git a/Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift b/Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift index 0e6ead8..20a0ad2 100644 --- a/Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift +++ b/Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift @@ -52,7 +52,7 @@ class ContentBlockerRulesBuilderTests: XCTestCase { andTemporaryUnprotectedDomains: []) // swiftlint:disable:next line_length - let domainFilter = "^(https?)?(wss?)?://([a-z0-9-]+\\.)*xvideos-cdn\\.com\\/v-c19d94e7937\\/v3\\/js\\/skins\\/min\\/default\\.header\\.static\\.js" + var domainFilter = "^(https?)?(wss?)?://([a-z0-9-]+\\.)*xvideos-cdn\\.com\\/v-c19d94e7937\\/v3\\/js\\/skins\\/min\\/default\\.header\\.static\\.js" if let idx = rules.firstIndexOfExactFilter(filter: domainFilter) { let nextRule = rules[idx + 1] XCTAssertNotNil(nextRule, "Missing ignore-previous popup type rule") @@ -62,6 +62,19 @@ class ContentBlockerRulesBuilderTests: XCTestCase { } else { XCTFail("Missing rule for testing") } + + // Test top level default block rule + domainFilter = "^(https?)?(wss?)?://([a-z0-9-]+\\.)*google-analytics\\.com(:?[0-9]+)?/.*" + if let idx = rules.firstIndexOfExactFilter(filter: domainFilter) { + let nextRule = rules[idx + 1] + XCTAssertNotNil(nextRule, "Missing ignore-previous popup type rule") + XCTAssert(nextRule.action == .ignorePreviousRules()) + XCTAssert(nextRule.trigger.loadContext?.first == .topFrame) + XCTAssert(nextRule.trigger.resourceType?.first == .popup) + } else { + XCTFail("Missing rule for testing") + } + } func testLoadingUnsupportedRules() throws {