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

adds script for YT on Android to block ads (uplift to 1.11.x) #5868

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/strings/utf_string_conversions.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "content/browser/web_contents/web_contents_impl.h"
Expand All @@ -33,16 +34,72 @@ const char k_youtube_background_playback_script[] =
" }"
"}());";

const char k_youtube_ads_block_script[] =
"(function() {"
" const prunePaths = ['playerResponse.adPlacements',"
" 'playerResponse.playerAds', 'adPlacements', 'playerAds'];"
" const findOwner = function(root, path) {"
" let owner = root;"
" let chain = path;"
" for (;;) {"
" 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;"
" },"
" });"
"})();";

bool IsYouTubeDomain(content::WebContents* contents) {
if (net::registry_controlled_domains::SameDomainOrHost(
contents->GetLastCommittedURL(), GURL("https://www.youtube.com"),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
return true;
}

return false;
}

bool IsBackgroundVideoPlaybackEnabled(content::WebContents* contents) {
PrefService* prefs =
static_cast<Profile*>(contents->GetBrowserContext())->GetPrefs();
if (!prefs->GetBoolean(kBackgroundVideoPlaybackEnabled))
return false;

if (net::registry_controlled_domains::SameDomainOrHost(
contents->GetLastCommittedURL(), GURL("https://www.youtube.com"),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
content::RenderFrameHost::AllowInjectingJavaScript();

return true;
}

bool IsAdBlockEnabled(content::WebContents* contents) {
Profile* profile = static_cast<Profile*>(contents->GetBrowserContext());

brave_shields::ControlType control_type =
brave_shields::GetAdControlType(profile,
contents->GetLastCommittedURL());
if (brave_shields::GetBraveShieldsEnabled(profile,
contents->GetLastCommittedURL()) &&
control_type != brave_shields::ALLOW) {
content::RenderFrameHost::AllowInjectingJavaScript();

return true;
}

Expand All @@ -61,11 +118,20 @@ BackgroundVideoPlaybackTabHelper::~BackgroundVideoPlaybackTabHelper() {

void BackgroundVideoPlaybackTabHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// Filter only YT domains here
if (!IsYouTubeDomain(web_contents())) {
return;
}
if (IsBackgroundVideoPlaybackEnabled(web_contents())) {
web_contents()->GetMainFrame()->ExecuteJavaScript(
base::UTF8ToUTF16(k_youtube_background_playback_script),
base::NullCallback());
}
if (IsAdBlockEnabled(web_contents())) {
web_contents()->GetMainFrame()->ExecuteJavaScript(
base::UTF8ToUTF16(k_youtube_ads_block_script),
base::NullCallback());
}
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(BackgroundVideoPlaybackTabHelper)