From 1ba788cae0a692f019ab9bc14e7ab3d9ccdf0c1b Mon Sep 17 00:00:00 2001 From: Jacob Sikorski Date: Thu, 12 Oct 2023 13:34:22 -0600 Subject: [PATCH] Fix brave/brave-ios#8239: Remove hard coded YouTube script (brave/brave-ios#8238) --- Package.swift | 1 - .../Frontend/Browser/DomainUserScript.swift | 5 - .../Browser/User Scripts/ScriptFactory.swift | 4 - .../User Scripts/ScriptSourceType.swift | 3 - .../DomainSpecific/Paged/YoutubeAdblock.js | 163 ------------------ 5 files changed, 176 deletions(-) delete mode 100644 Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/YoutubeAdblock.js diff --git a/Package.swift b/Package.swift index 5c6109c728ea..eb7cdc408a7b 100644 --- a/Package.swift +++ b/Package.swift @@ -433,7 +433,6 @@ var braveTarget: PackageDescription.Target = .target( .copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/nacl.min.js"), .copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/PlaylistFolderSharingScript.js"), .copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/FrameCheckWrapper.js"), - .copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/YoutubeAdblock.js"), .copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/CookieControlScript.js"), .copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/FarblingProtectionScript.js"), .copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/MediaBackgroundingScript.js"), diff --git a/Sources/Brave/Frontend/Browser/DomainUserScript.swift b/Sources/Brave/Frontend/Browser/DomainUserScript.swift index 78804b0c990d..1756249bfcd6 100644 --- a/Sources/Brave/Frontend/Browser/DomainUserScript.swift +++ b/Sources/Brave/Frontend/Browser/DomainUserScript.swift @@ -15,7 +15,6 @@ enum DomainUserScript: CaseIterable { #endif case braveSkus case bravePlaylistFolderSharingHelper - case youtubeAdblock /// Initialize this script with a URL init?(for url: URL) { @@ -55,8 +54,6 @@ enum DomainUserScript: CaseIterable { return Set(["account.brave.com", "account.bravesoftware.com", "account.brave.software"]) - case .youtubeAdblock: - return Set(["youtube.com"]) } } @@ -70,8 +67,6 @@ enum DomainUserScript: CaseIterable { case .braveTalkHelper: return nil #endif - case .youtubeAdblock: - return .AdblockAndTp } } } diff --git a/Sources/Brave/Frontend/Browser/User Scripts/ScriptFactory.swift b/Sources/Brave/Frontend/Browser/User Scripts/ScriptFactory.swift index 3eca3b91b48b..4a0858314efe 100644 --- a/Sources/Brave/Frontend/Browser/User Scripts/ScriptFactory.swift +++ b/Sources/Brave/Frontend/Browser/User Scripts/ScriptFactory.swift @@ -88,10 +88,6 @@ class ScriptFactory { } return script - - case .youtubeAdblock: - let source = try makeScriptSource(of: .youtubeAdblock) - return WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page) } } diff --git a/Sources/Brave/Frontend/Browser/User Scripts/ScriptSourceType.swift b/Sources/Brave/Frontend/Browser/User Scripts/ScriptSourceType.swift index efbb5d4c3cc6..23b45e4c693f 100644 --- a/Sources/Brave/Frontend/Browser/User Scripts/ScriptSourceType.swift +++ b/Sources/Brave/Frontend/Browser/User Scripts/ScriptSourceType.swift @@ -22,8 +22,6 @@ enum ScriptSourceType { /// This script is a modification of the android and desktop script found here: /// https://github.com/brave/brave-core/blob/master/components/cosmetic_filters/resources/data/content_cosmetic.ts case selectorsPoller - /// A script for blocking youtube ads - case youtubeAdblock var fileName: String { switch self { @@ -31,7 +29,6 @@ enum ScriptSourceType { case .farblingProtection: return "FarblingProtectionScript" case .frameCheckWrapper: return "FrameCheckWrapper" case .selectorsPoller: return "SelectorsPollerScript" - case .youtubeAdblock: return "YoutubeAdblock" } } diff --git a/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/YoutubeAdblock.js b/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/YoutubeAdblock.js deleted file mode 100644 index dccd2a41fc0f..000000000000 --- a/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/YoutubeAdblock.js +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright 2022 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/. - -// PruneJS -(function() { - const prunePaths = ['playerResponse.adPlacements', 'playerResponse.playerAds', 'adPlacements', 'playerAds']; - const findOwner = function(root, path) { - let owner = root; - let chain = path; - while(true) { - if (owner instanceof Object === false) { return; } - const pos = chain.indexOf('.'); - if (pos === -1) { - return owner.hasOwnProperty(chain)? [owner, chain]: undefined; - } - const prop = chain.slice(0, pos); - if (owner.hasOwnProperty(prop) === false) { return; } - owner = owner[prop]; - chain = chain.slice(pos + 1); - } - }; - - JSON.parse = new Proxy(JSON.parse, { - apply: function() { - const r = Reflect.apply(...arguments); - for (const path of prunePaths) { - const details = findOwner(r, path); - if (details !== undefined) { - delete details[0][details[1]]; - } - } - return r; - }, - }); -})(); - -/// SetJS -(function() { - const setJS = function(chain, cValue) { - const thisScript = document.currentScript; - if (cValue === 'undefined') { - cValue = undefined; - } else if (cValue === 'false') { - cValue = false; - } else if (cValue === 'true') { - cValue = true; - } else if (cValue === 'null') { - cValue = null; - } else if (cValue === 'noopFunc') { - cValue = function(){}; - } else if (cValue === 'trueFunc') { - cValue = function(){ return true; }; - } else if (cValue === 'falseFunc') { - cValue = function(){ return false; }; - } else if (/^\d+$/.test(cValue)) { - cValue = parseFloat(cValue); - if (isNaN(cValue)) { return; } - if (Math.abs(cValue) > 0x7FFF) { return; } - } else if (cValue === "''") { - cValue = ''; - } else { - return; - } - - let aborted = false; - const mustAbort = function(v) { - if (aborted) { return true; } - aborted = - (v !== undefined && v !== null) && - (cValue !== undefined && cValue !== null) && - (typeof v !== typeof cValue); - return aborted; - }; - - // https://github.com/uBlockOrigin/uBlock-issues/issues/156 - // Support multiple trappers for the same property. - const trapProp = function(owner, prop, handler) { - if (handler.init(owner[prop]) === false) { return; } - const odesc = Object.getOwnPropertyDescriptor(owner, prop); - let prevGetter, prevSetter; - if (odesc instanceof Object) { - if (odesc.get instanceof Function) { - prevGetter = odesc.get; - } - if (odesc.set instanceof Function) { - prevSetter = odesc.set; - } - } - - Object.defineProperty(owner, prop, { - configurable: true, - get() { - if (prevGetter !== undefined) { - prevGetter(); - } - return handler.getter(); - }, - set(a) { - if (prevSetter !== undefined) { - prevSetter(a); - } - handler.setter(a); - } - }); - }; - - const trapChain = function(owner, chain) { - const pos = chain.indexOf('.'); - if (pos === -1) { - trapProp(owner, chain, { - v: undefined, - init: function(v) { - if (mustAbort(v)) { return false; } - this.v = v; - return true; - }, - getter: function() { - return document.currentScript === thisScript - ? this.v - : cValue; - }, - setter: function(a) { - if (mustAbort(a) === false) { return; } - cValue = a; - } - }); - return; - } - - const prop = chain.slice(0, pos); - const v = owner[prop]; - chain = chain.slice(pos + 1); - if (v instanceof Object || typeof v === 'object' && v !== null) { - trapChain(v, chain); - return; - } - - trapProp(owner, prop, { - v: undefined, - init: function(v) { - this.v = v; - return true; - }, - getter: function() { - return this.v; - }, - setter: function(a) { - this.v = a; - if (a instanceof Object) { - trapChain(a, chain); - } - } - }); - }; - - trapChain(window, chain); - }; - - setJS('ytInitialPlayerResponse.adPlacements', 'undefined'); - setJS('playerResponse.adPlacements', 'undefined'); -})();