Skip to content

Commit

Permalink
WIP rule splitting for custom actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ladamski committed Mar 5, 2024
1 parent 446bed7 commit 5a5712a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import BrowserServicesKit

struct ClickToLoadRulesSplitter {

public enum Constant {
public enum Constants {

public static let clickToLoadRuleListPrefix = "CTL_"

Expand All @@ -43,7 +43,7 @@ struct ClickToLoadRulesSplitter {
ContentBlockerRulesList(name: rulesList.name,
trackerData: splitTDS?.withoutBlockCTL ?? rulesList.trackerData,
fallbackTrackerData: splitFallbackTDS?.withoutBlockCTL ?? rulesList.fallbackTrackerData),
ContentBlockerRulesList(name: "XD",
ContentBlockerRulesList(name: ContentBlockerRulesLists.Constants.clickToLoadRulesListName,
trackerData: splitTDS?.withBlockCTL ?? rulesList.trackerData,
fallbackTrackerData: splitFallbackTDS?.withBlockCTL ?? rulesList.fallbackTrackerData)
)
Expand All @@ -52,16 +52,16 @@ struct ClickToLoadRulesSplitter {
}

private func split(trackerData: TrackerDataManager.DataSet) -> (withoutBlockCTL: TrackerDataManager.DataSet, withBlockCTL: TrackerDataManager.DataSet)? {
let trackersWithBlockCTL = filterTrackersByBlockCTLAction(trackerData.tds.trackers, hasBlockCTL: true)
let trackersWithBlockCTL = filterTrackersWithCTLAction(trackerData.tds.trackers)

if !trackersWithBlockCTL.isEmpty {
let trackersWithoutBlockCTL = filterTrackersByBlockCTLAction(trackerData.tds.trackers, hasBlockCTL: false)
let trackersWithoutBlockCTL = filterTrackersWithoutCTLAction(trackerData.tds.trackers)
let trackerDataWithoutBlockCTL = makeTrackerData(using: trackersWithoutBlockCTL, originalTDS: trackerData.tds)
let trackerDataWithBlockCTL = makeTrackerData(using: trackersWithBlockCTL, originalTDS: trackerData.tds)

return (
(tds: trackerDataWithoutBlockCTL, etag: Constant.clickToLoadRuleListPrefix + trackerData.etag),
(tds: trackerDataWithBlockCTL, etag: Constant.clickToLoadRuleListPrefix + trackerData.etag)
(tds: trackerDataWithoutBlockCTL, etag: Constants.clickToLoadRuleListPrefix + trackerData.etag),
(tds: trackerDataWithBlockCTL, etag: Constants.clickToLoadRuleListPrefix + trackerData.etag)
)
}
return nil
Expand All @@ -76,8 +76,33 @@ struct ClickToLoadRulesSplitter {
cnames: originalTDS.cnames)
}

private func filterTrackersByBlockCTLAction(_ trackers: [String: KnownTracker], hasBlockCTL: Bool) -> [String: KnownTracker] {
trackers.filter { (_, tracker) in tracker.containsCTLActions == hasBlockCTL }
private func filterTrackersWithoutCTLAction(_ trackers: [String: KnownTracker]) -> [String: KnownTracker] {
trackers.filter { (_, tracker) in tracker.containsCTLActions == false }
}

private func filterTrackersWithCTLAction(_ trackers: [String: KnownTracker]) -> [String: KnownTracker] {
return Dictionary(uniqueKeysWithValues: trackers.filter { (_, tracker) in
return tracker.containsCTLActions == true
}.map { (key, value) in
var modifiedTracker = value
// Modify the tracker here
if modifiedTracker.defaultAction == .blockCtlFB {
modifiedTracker.defaultAction = .block
}
print("RULES BEFORE \(modifiedTracker.rules)")

if let rules = modifiedTracker.rules as [KnownTracker.Rule]? {
for ruleIndex in rules.indices {
if let action = rules[ruleIndex].action, action == .blockCtlFB {
// modifiedTracker.rules?[ruleIndex].action = .block
modifiedTracker.rules?[ruleIndex].action = nil
}
}
}
print("RULES AFTER \(modifiedTracker.rules)")

return (key, modifiedTracker)
})
}

private func extractDomains(from entities: [String: Entity]) -> [String: String] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ final class ClickToLoadUserScript: NSObject, WKNavigationDelegate, Subfeature {
'use strict';
console.warn('fb-sdk document.currentScript.src', document.currentScript, document.currentScript?.src);
console.warn('in fbSurrogate, location is', window.location.href);
debugger;
return;
window.fbTest = "inline surrogate";
const facebookEntity = 'Facebook, Inc.';
const originalFBURL = 'https://connect.facebook.net/en_US/sdk.js?XFBML=false' //FIXME: document.currentScript.src;
Expand Down

0 comments on commit 5a5712a

Please sign in to comment.