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: Use Object.create() Created Aug. 30, 2014, 11:02 a.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 HTML5 player is configured via ytplayer.config.args. We have
85 document.documentElement.removeChild(script); 97 // to make sure that ad-related arguments are ignored as they are set.
98 var ytplayer = undefined;
99 Object.defineProperty(window, "ytplayer",
100 {
101 configurable: true,
102 get: function()
103 {
104 return ytplayer;
105 },
106 set: function(rawYtplayer)
107 {
108 if (!rawYtplayer || typeof rawYtplayer != "object")
109 {
110 ytplayer = rawYtplayer;
111 return;
112 }
113
114 var config = undefined;
115 ytplayer = Object.create(rawYtplayer, {
116 config: {
117 enumerable: true,
118 get: function()
119 {
120 return config;
121 },
122 set: function(rawConfig)
123 {
124 if (!rawConfig || typeof rawConfig != "object")
125 {
126 config = rawConfig;
127 return;
128 }
129
130 var args = undefined;
131 config = Object.create(rawConfig, {
132 args: {
133 enumerable: true,
134 get: function()
135 {
136 return args;
137 },
138 set: function(rawArgs)
139 {
140 if (!rawArgs || typeof rawArgs != "object")
141 {
142 args = rawArgs;
143 return;
144 }
145
146 args = {};
147 for (var arg in rawArgs)
148 {
149 if (!badArgumentsRegex.test(arg))
150 args[arg] = rawArgs[arg];
151 }
152 }
153 }
154 });
155
156 config.args = rawConfig.args;
157 }
158 }
159 });
160
161 ytplayer.config = rawYtplayer.config;
162 }
163 });
164 }, badArgumentsRegex);
86 })(); 165 })();
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