Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse procedural_filters.ts for ios #25694

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
27 changes: 17 additions & 10 deletions components/cosmetic_filters/resources/data/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ import("//tools/grit/grit_rule.gni")
import("//tools/grit/repack.gni")

transpile_web_ui("cosmetic_filters_resources") {
entry_points = [
[
"cosmetic_filters",
rebase_path("content_cosmetic.ts"),
],
[
"element_picker",
rebase_path("element_picker.ts"),
],
]
if (!is_ios) {
entry_points = [
[
"cosmetic_filters",
rebase_path("content_cosmetic.ts"),
],
[
"element_picker",
rebase_path("element_picker.ts"),
],
]
} else {
entry_points = [ [
"content_cosmetic_ios",
rebase_path("content_cosmetic_ios.js"),
] ]
}

resource_name = "cosmetic_filters"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

window.__firefox__.execute(function($) {
const args = $<args>;
const proceduralFilters = $<procedural_filters>;
const messageHandler = '$<message_handler>';
const partinessMessageHandler = '$<partiness_message_handler>';
const {
applyCompiledSelector, compileProceduralSelector
} = (function() {
$<procedural_filters_script>
})();
import { applyCompiledSelector, compileProceduralSelector } from './procedural_filters'

/**
* Send ids and classes to iOS and await new hide selectors
Expand All @@ -34,7 +25,8 @@ window.__firefox__.execute(function($) {
/**
* Send new urls found on the page and return their partiness
* @param {Array} urls The urls found on this page
* @returns A Promise resolving to a dictionary base urls and their first party status
* @returns A Promise resolving to a dictionary base urls and their first
* party status
*/
const getPartiness = $((urls) => {
return $.postNativeMessage(partinessMessageHandler, {
Expand All @@ -50,9 +42,9 @@ window.__firefox__.execute(function($) {
// the backend script is up and connected (eg backgroundReady = true),
// or sooner if the thread is idle.
const maxTimeMSBeforeStart = 1000
// The cutoff for text ads. If something has only text in it, it needs to have
// this many, or more, characters. Similarly, require it to have a non-trivial
// number of words in it, to look like an actual text ad.
// The cutoff for text ads. If something has only text in it, it needs to
// have this many, or more, characters. Similarly, require it to have a
// non-trivial number of words in it, to look like an actual text ad.
const minAdTextChars = 30
const minAdTextWords = 5
const returnToMutationObserverIntervalMs = 10000
Expand All @@ -68,7 +60,8 @@ window.__firefox__.execute(function($) {
// Elements that are not yet queried for selectors
const notYetQueriedElements = []
// The query to perform when extracting classes and ids
const classIdWithoutHtmlOrBody = '[id]:not(html):not(body),[class]:not(html):not(body)'
const classIdWithoutHtmlOrBody =
'[id]:not(html):not(body),[class]:not(html):not(body)'

// Generate a random string between [a000000000, zzzzzzzzzz] (base 36)
const generateRandomAttr = () => {
Expand Down Expand Up @@ -108,7 +101,8 @@ window.__firefox__.execute(function($) {
}

/**
* Send any new urls to the iOS subrutine so we can determine its partyness (1st or 3rd party)
* Send any new urls to the iOS subrutine so we can determine its partyness
* (1st or 3rd party)
* @returns A Promise that returns if new party information was returned or no
*/
const sendPendingOriginsIfNeeded = async () => {
Expand Down Expand Up @@ -197,7 +191,7 @@ window.__firefox__.execute(function($) {

sendPendingSelectorsTimerId = window.setTimeout(() => {
sendPendingSelectorsIfNeeded()
delete sendPendingSelectorsTimerId
sendPendingSelectorsTimerId = undefined
}, args.fetchNewClassIdRulesThrottlingMs)
}

Expand Down Expand Up @@ -650,7 +644,7 @@ window.__firefox__.execute(function($) {
CC.unhiddenSelectors.add(selector)

// Remove these selectors from the run queues
for (let index = 0; index < CC.runQueues; index++) {
for (let index = 0; index < CC.runQueues.length; index++) {
CC.runQueues[index].delete(selector)
}
})
Expand Down Expand Up @@ -1066,7 +1060,7 @@ window.__firefox__.execute(function($) {

setRulesTimerId = window.setTimeout(() => {
setRulesOnStylesheet()
delete setRulesTimerId
setRulesTimerId = undefined
}, 200)
}

Expand Down Expand Up @@ -1222,4 +1216,3 @@ window.__firefox__.execute(function($) {
}

window.setTimeout(waitForBody, maxTimeMSBeforeStart)
});
4 changes: 2 additions & 2 deletions components/resources/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ repack("resources") {
deps = [
":static_resources",
"//brave/components/ai_rewriter/common/buildflags",
"//brave/components/cosmetic_filters/resources/data:generated_resources",
"//brave/components/skus/browser/resources:generated_resources",
]
sources = [
"$root_gen_dir/brave/components/cosmetic_filters/resources/cosmetic_filters_generated.pak",
"$root_gen_dir/brave/components/skus/browser/resources/skus_internals_generated.pak",
"$root_gen_dir/components/brave_components_static.pak",
]
Expand All @@ -66,13 +68,11 @@ repack("resources") {
deps += [
"//brave/components/brave_adblock_ui:generated_resources",
"//brave/components/brave_adblock_ui/adblock_internals:generated_resources",
"//brave/components/cosmetic_filters/resources/data:generated_resources",
]

sources += [
"$root_gen_dir/brave/components/brave_adblock/adblock_internals/resources/brave_adblock_internals_generated.pak",
"$root_gen_dir/brave/components/brave_adblock/resources/brave_adblock_generated.pak",
"$root_gen_dir/brave/components/cosmetic_filters/resources/cosmetic_filters_generated.pak",
]
}

Expand Down
6 changes: 0 additions & 6 deletions ios/brave-ios/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,6 @@ var braveTarget: PackageDescription.Target = .target(
.copy(
"Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/ResourceDownloaderScript.js"
),
.copy(
"Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js"
),
.copy(
"Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/ProceduralFilters.js"
),
.copy(
"Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SiteStateListenerScript.js"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import BraveCore
import Foundation
import WebKit

Expand Down Expand Up @@ -153,12 +154,18 @@ class ScriptFactory {
let args = String(data: data, encoding: .utf8)!
let proceduralActionFilters =
proceduralActions.isEmpty ? "undefined" : "[ \(proceduralActions.joined(separator: ","))]"
let proceduralFiltersScript = try makeScriptSource(of: .proceduralFilters)
let source = try ScriptFactory.shared.makeScriptSource(of: .selectorsPoller)
.replacingOccurrences(of: "$<args>", with: args)
.replacingOccurrences(of: "$<procedural_filters_script>", with: proceduralFiltersScript)
.replacingOccurrences(of: "$<procedural_filters>", with: proceduralActionFilters)

let cosmeticFiltersScript = AdblockService.cosmeticFiltersScript()
let source = """
window.__firefox__.execute(function($) {
const args = \(args);
const proceduralFilters = \(proceduralActionFilters);
const messageHandler = '$<message_handler>';
const partinessMessageHandler = '$<partiness_message_handler>';

\(cosmeticFiltersScript)

});
"""
let secureSource = CosmeticFiltersScriptHandler.secureScript(
handlerNamesMap: [
"$<message_handler>": CosmeticFiltersScriptHandler.messageHandlerName,
Expand Down
Loading
Loading