This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add non-aggressive cosmetic filters Undo * Make default ad-block level standard * Fix issue with extracting origin on selectors poller script
- Loading branch information
Showing
9 changed files
with
971 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...rContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/URLPartinessScriptHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2023 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import WebKit | ||
import Shared | ||
import Data | ||
import os.log | ||
|
||
/// This handler receives a list of urls for a given frame that need to determine its partiness (i.e. 1st part vs 3rd party) | ||
/// | ||
/// The urls are collected in the `SelectorsPollerScript.js` file. | ||
class URLPartinessScriptHandler: TabContentScript { | ||
private struct PartinessDTO: Decodable { | ||
struct PartinessDTOData: Decodable, Hashable { | ||
let sourceURL: String | ||
let urls: [String] | ||
} | ||
|
||
let securityToken: String | ||
let data: PartinessDTOData | ||
} | ||
|
||
static let scriptName = "URLPartinessScript" | ||
static let scriptId = CosmeticFiltersScriptHandler.scriptId | ||
static let messageHandlerName = "\(scriptName)_\(messageUUID)" | ||
static let scriptSandbox: WKContentWorld = .defaultClient | ||
static let userScript: WKUserScript? = nil | ||
|
||
private weak var tab: Tab? | ||
|
||
init(tab: Tab) { | ||
self.tab = tab | ||
} | ||
|
||
func userContentController(_ userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) { | ||
if !verifyMessage(message: message) { | ||
assertionFailure("Invalid security token. Fix the `SelectorsPollerScript.js` script") | ||
replyHandler(nil, nil) | ||
return | ||
} | ||
|
||
do { | ||
let data = try JSONSerialization.data(withJSONObject: message.body) | ||
let dto = try JSONDecoder().decode(PartinessDTO.self, from: data) | ||
var results: [String: Bool] = [:] | ||
|
||
guard let frameURL = NSURL(idnString: dto.data.sourceURL) as URL? else { | ||
// Since we can't create a url from the source, | ||
// we will assume they are all 3rd party | ||
for urlString in dto.data.urls { | ||
results[urlString] = false | ||
} | ||
|
||
replyHandler(results, nil) | ||
return | ||
} | ||
|
||
let frameETLD1 = frameURL.baseDomain | ||
|
||
for urlString in dto.data.urls { | ||
guard let etld1 = (NSURL(idnString: urlString) as? URL)?.baseDomain else { | ||
// We can't determine a url. | ||
// Let's assume it's 3rd party | ||
results[urlString] = false | ||
continue | ||
} | ||
|
||
results[urlString] = frameETLD1 == etld1 | ||
} | ||
|
||
replyHandler(results, nil) | ||
} catch { | ||
assertionFailure("Invalid type of message. Fix the `RequestBlocking.js` script") | ||
replyHandler(nil, nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.