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: Addressed comments Created Aug. 29, 2014, 2:47 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 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 get: function()
102 {
103 return ytplayer;
104 },
105 set: function(rawYtplayer)
106 {
107 if (!rawYtplayer || typeof rawYtplayer != "object")
108 {
109 ytplayer = rawYtplayer;
110 return;
111 }
112
113 var config = undefined;
114 ytplayer = {
115 __proto__: rawYtplayer,
116 get config()
Wladimir Palant 2014/08/29 20:37:07 Given that __proto__ is deprecated, I wonder wheth
Sebastian Noack 2014/08/29 20:45:05 We don't use Object.create() so far in this codeba
Sebastian Noack 2014/08/30 11:03:14 Done.
117 {
118 return config;
119 },
120 set config(rawConfig)
121 {
122 if (!rawConfig || typeof rawConfig != "object")
123 {
124 config = rawConfig;
125 return;
126 }
127
128 var args = undefined;
129 config = {
130 __proto__: rawConfig,
131 get args()
132 {
133 return args;
134 },
135 set args(rawArgs)
136 {
137 if (!rawArgs || typeof rawArgs != "object")
138 {
139 args = rawArgs;
140 return;
141 }
142
143 args = {};
144 for (var arg in rawArgs)
145 {
146 if (!badArgumentsRegex.test(arg))
147 args[arg] = rawArgs[arg];
148 }
149 }
150 };
151
152 config.args = rawConfig.args;
153 }
154 };
155
156 ytplayer.config = rawYtplayer.config;
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