Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #6261: Add back bundled youtube adblock script
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba committed Nov 17, 2022
1 parent 673002c commit acf6a0e
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Client/Frontend/Browser/DomainUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum DomainUserScript: CaseIterable {
case braveTalkHelper
case braveSkus
case bravePlaylistFolderSharingHelper
case youtubeAdblock

/// Initialize this script with a URL
init?(for url: URL) {
Expand Down Expand Up @@ -47,6 +48,19 @@ enum DomainUserScript: CaseIterable {
return Set(["account.brave.com",
"account.bravesoftware.com",
"account.brave.software"])
case .youtubeAdblock:
return Set(["youtube.com"])
}
}

/// Returns a shield type for a given user script domain.
/// Returns nil if the domain's user script can't be turned off via a shield toggle. (i.e. it's always enabled)
var requiredShield: BraveShield? {
switch self {
case .braveSearchHelper, .braveTalkHelper, .bravePlaylistFolderSharingHelper, .braveSkus:
return nil
case .youtubeAdblock:
return .AdblockAndTp
}
}
}
10 changes: 9 additions & 1 deletion Client/Frontend/Browser/PageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@ struct PageData {
// Handle dynamic domain level scripts on the request that don't use shields
// This shield is always on and doesn't need sheild settings
if let domainUserScript = DomainUserScript(for: mainFrameURL) {
userScriptTypes.insert(.domainUserScript(domainUserScript))
if let shield = domainUserScript.requiredShield {
// If a shield is required check that shield
if domainForShields.isShieldExpected(shield, considerAllShieldsOption: true) {
userScriptTypes.insert(.domainUserScript(domainUserScript))
}
} else {
// Otherwise just add it right away
userScriptTypes.insert(.domainUserScript(domainUserScript))
}
}

// Add engine scripts for the main frame
Expand Down
4 changes: 4 additions & 0 deletions Client/Frontend/Browser/User Scripts/ScriptFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class ScriptFactory {
}

return script

case .youtubeAdblock:
let source = try makeScriptSource(of: .youtubeAdblock)
return WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
}
}

Expand Down
3 changes: 3 additions & 0 deletions Client/Frontend/Browser/User Scripts/ScriptSourceType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ 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 {
case .nacl: return "nacl.min"
case .farblingProtection: return "FarblingProtectionScript"
case .frameCheckWrapper: return "FrameCheckWrapper"
case .selectorsPoller: return "SelectorsPollerScript"
case .youtubeAdblock: return "YoutubeAdblock"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// 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');
})();
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ let package = Package(
.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"),
Expand Down

0 comments on commit acf6a0e

Please # to comment.