Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: safari/include.youtube.js

Issue 5607308285444096: Issue 1258 - Block ads in YouTube's HTML5 player on Safari (Closed)
Patch Set: Use Object.create() Created Aug. 30, 2014, 11:02 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: safari/include.youtube.js
===================================================================
--- a/safari/include.youtube.js
+++ b/safari/include.youtube.js
@@ -22,11 +22,13 @@
if (!ext.backgroundPage.sendMessageSync({type: "get-domain-enabled-state"}).enabled)
return;
+ var badArgumentsRegex = /^((.*_)?(ad|ads|afv|adsense)(_.*)?|(ad3|st)_module|prerolls|interstitial|infringe|iv_cta_url)$/;
+
function rewriteFlashvars(flashvars)
{
var pairs = flashvars.split("&");
for (var i = 0; i < pairs.length; i++)
- if (/^((ad|afv|adsense)(_.*)?|(ad3|st)_module|prerolls|interstitial|infringe|iv_cta_url)=/.test(pairs[i]))
+ if (badArgumentsRegex.test(pairs[i].split("=")[0]))
pairs.splice(i--, 1);
return pairs.join("&");
}
@@ -66,21 +68,98 @@
player.parentNode.replaceChild(newPlayer, player);
}
+ function runInPage(fn, arg)
+ {
+ var script = document.createElement("script");
+ script.type = "application/javascript";
+ script.async = false;
+ script.textContent = "(" + fn + ")(" + arg + ");";
+ document.documentElement.appendChild(script);
+ document.documentElement.removeChild(script);
+ }
+
document.addEventListener("beforeload", function(event)
{
if ((event.target.localName == "object" || event.target.localName == "embed") && /:\/\/[^\/]*\.ytimg\.com\//.test(event.url))
patchPlayer(event.target);
}, true);
- // if history.pushState is available, YouTube uses the history API
- // when navigation from one video to another, and tells the flash
- // player with JavaScript which video and which ads to show next,
- // bypassing our flashvars rewrite code. So we disable
- // history.pushState before YouTube's JavaScript runs.
- var script = document.createElement("script");
- script.type = "application/javascript";
- script.async = false;
- script.textContent = "History.prototype.pushState = undefined;";
- document.documentElement.appendChild(script);
- document.documentElement.removeChild(script);
+ runInPage(function(badArgumentsRegex)
+ {
+ // If history.pushState is available, YouTube uses the history API
+ // when navigation from one video to another, and tells the flash
+ // player with JavaScript which video and which ads to show next,
+ // bypassing our flashvars rewrite code. So we disable
+ // history.pushState before YouTube's JavaScript runs.
+ History.prototype.pushState = undefined;
+
+ // The HTML5 player is configured via ytplayer.config.args. We have
+ // to make sure that ad-related arguments are ignored as they are set.
+ var ytplayer = undefined;
+ Object.defineProperty(window, "ytplayer",
+ {
+ configurable: true,
+ get: function()
+ {
+ return ytplayer;
+ },
+ set: function(rawYtplayer)
+ {
+ if (!rawYtplayer || typeof rawYtplayer != "object")
+ {
+ ytplayer = rawYtplayer;
+ return;
+ }
+
+ var config = undefined;
+ ytplayer = Object.create(rawYtplayer, {
+ config: {
+ enumerable: true,
+ get: function()
+ {
+ return config;
+ },
+ set: function(rawConfig)
+ {
+ if (!rawConfig || typeof rawConfig != "object")
+ {
+ config = rawConfig;
+ return;
+ }
+
+ var args = undefined;
+ config = Object.create(rawConfig, {
+ args: {
+ enumerable: true,
+ get: function()
+ {
+ return args;
+ },
+ set: function(rawArgs)
+ {
+ if (!rawArgs || typeof rawArgs != "object")
+ {
+ args = rawArgs;
+ return;
+ }
+
+ args = {};
+ for (var arg in rawArgs)
+ {
+ if (!badArgumentsRegex.test(arg))
+ args[arg] = rawArgs[arg];
+ }
+ }
+ }
+ });
+
+ config.args = rawConfig.args;
+ }
+ }
+ });
+
+ ytplayer.config = rawYtplayer.config;
+ }
+ });
+ }, badArgumentsRegex);
})();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld