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

Side by Side Diff: safari/include.youtube.js

Issue 5607308285444096: Issue 1258 - Block ads in YouTube's HTML5 player on Safari (Closed)
Patch Set: Created Aug. 22, 2014, 2 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 (function() { 18 (function() {
19 if (document.domain != "www.youtube.com") 19 if (document.domain != "www.youtube.com")
20 return; 20 return;
21 21
22 if (!ext.backgroundPage.sendMessageSync({type: "get-domain-enabled-state"}).en abled) 22 if (!ext.backgroundPage.sendMessageSync({type: "get-domain-enabled-state"}).en abled)
23 return; 23 return;
24 24
25 var badArgumentsRegex = /^((.*_)?(ad|ads|afv|adsense)(_.*)?|(ad3|st)_module|pr erolls|interstitial|infringe|iv_cta_url)$/;
26
25 function rewriteFlashvars(flashvars) 27 function rewriteFlashvars(flashvars)
26 { 28 {
27 var pairs = flashvars.split("&"); 29 var pairs = flashvars.split("&");
28 for (var i = 0; i < pairs.length; i++) 30 for (var i = 0; i < pairs.length; i++)
29 if (/^((ad|afv|adsense)(_.*)?|(ad3|st)_module|prerolls|interstitial|infrin ge|iv_cta_url)=/.test(pairs[i])) 31 if (badArgumentsRegex.test(pairs[i].split("=")[0]))
30 pairs.splice(i--, 1); 32 pairs.splice(i--, 1);
31 return pairs.join("&"); 33 return pairs.join("&");
32 } 34 }
33 35
34 function patchPlayer(player) 36 function patchPlayer(player)
35 { 37 {
36 var newPlayer = player.cloneNode(true); 38 var newPlayer = player.cloneNode(true);
37 var flashvarsChanged = false; 39 var flashvarsChanged = false;
38 40
39 var flashvars = newPlayer.getAttribute("flashvars"); 41 var flashvars = newPlayer.getAttribute("flashvars");
(...skipping 19 matching lines...) Expand all
59 param.setAttribute("value", newValue); 61 param.setAttribute("value", newValue);
60 flashvarsChanged = true; 62 flashvarsChanged = true;
61 } 63 }
62 } 64 }
63 } 65 }
64 66
65 if (flashvarsChanged) 67 if (flashvarsChanged)
66 player.parentNode.replaceChild(newPlayer, player); 68 player.parentNode.replaceChild(newPlayer, player);
67 } 69 }
68 70
71 function runInPage(fn, arg)
72 {
73 var script = document.createElement("script");
74 script.type = "application/javascript";
75 script.async = false;
76 script.textContent = "(" + fn + ")(" + arg + ");";
77 document.documentElement.appendChild(script);
78 document.documentElement.removeChild(script);
79 }
80
69 document.addEventListener("beforeload", function(event) 81 document.addEventListener("beforeload", function(event)
70 { 82 {
71 if ((event.target.localName == "object" || event.target.localName == "embed" ) && /:\/\/[^\/]*\.ytimg\.com\//.test(event.url)) 83 if ((event.target.localName == "object" || event.target.localName == "embed" ) && /:\/\/[^\/]*\.ytimg\.com\//.test(event.url))
72 patchPlayer(event.target); 84 patchPlayer(event.target);
73 }, true); 85 }, true);
74 86
75 // if history.pushState is available, YouTube uses the history API 87 runInPage(function(badArgumentsRegex)
76 // when navigation from one video to another, and tells the flash 88 {
77 // player with JavaScript which video and which ads to show next, 89 // if history.pushState is available, YouTube uses the history API
78 // bypassing our flashvars rewrite code. So we disable 90 // when navigation from one video to another, and tells the flash
79 // history.pushState before YouTube's JavaScript runs. 91 // player with JavaScript which video and which ads to show next,
80 var script = document.createElement("script"); 92 // bypassing our flashvars rewrite code. So we disable
81 script.type = "application/javascript"; 93 // history.pushState before YouTube's JavaScript runs.
82 script.async = false; 94 History.prototype.pushState = undefined;
83 script.textContent = "History.prototype.pushState = undefined;"; 95
84 document.documentElement.appendChild(script); 96 // the HMLT5 player is configured via ytplayer.config.args. We have
Wladimir Palant 2014/08/28 16:43:38 Nit: "The HTML5 player" (properly capitalized and
Sebastian Noack 2014/08/29 14:51:24 Done.
85 document.documentElement.removeChild(script); 97 // to make sure that ad-related arguments are ignored as they are set.
98 var ytplayer = undefined;
Wladimir Palant 2014/08/28 16:43:38 What if ytplayer variable was set already? I'd rec
Sebastian Noack 2014/08/29 14:51:24 This hack only works anyway, if our JavaScript run
99 Object.defineProperty(window, "ytplayer",
100 {
101 get: function()
102 {
103 return ytplayer;
104 },
105 set: function(rawYtplayer)
106 {
107 if (typeof rawYtplayer != "object")
Wladimir Palant 2014/08/28 16:43:38 console.log(typeof null); This should be: if (
Sebastian Noack 2014/08/29 14:51:24 Thanks for reminding me that null is an object. I'
108 {
109 ytplayer = rawYtplayer;
110 return;
111 }
112
113 var config = undefined;
Wladimir Palant 2014/08/28 16:43:38 Here as well: what if the config property is alrea
Sebastian Noack 2014/08/29 14:51:24 This was handled by the loop below: for (var pr
114 ytplayer = {
115 get config()
116 {
117 return config;
118 },
119 set config(rawConfig)
120 {
121 if (typeof rawConfig != "object")
Wladimir Palant 2014/08/28 16:43:38 Here as well: please consider the possibility that
Sebastian Noack 2014/08/29 14:51:24 Done.
122 {
123 config = rawConfig;
124 return;
125 }
126
127 var args = undefined;
128 config = {
129 get args()
130 {
131 return args;
132 },
133 set args(rawArgs)
134 {
135 if (typeof rawArgs != "object")
136 {
137 args = rawArgs;
138 return;
139 }
140
141 args = {};
142 for (var arg in rawArgs)
143 {
144 if (!badArgumentsRegex.test(arg))
145 args[arg] = rawArgs[arg];
146 }
147 }
148 };
Wladimir Palant 2014/08/28 16:43:38 There is a recurring pattern here: you wrap object
Sebastian Noack 2014/08/29 14:51:24 Of course, I already considered that. However, the
149
150 for (var prop in rawConfig)
151 config[prop] = rawConfig[prop];
152 }
153 };
154
155 for (var prop in rawYtplayer)
156 ytplayer[prop] = rawYtplayer[prop];
Wladimir Palant 2014/08/28 16:43:38 I think that inheriting from rawYtplayer would be
Sebastian Noack 2014/08/29 14:51:24 Done. However, I'm not convinced that it is a bett
157 },
158 configurable: true
159 });
160 }, badArgumentsRegex);
86 })(); 161 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld