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

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

Issue 4663448277352448: Disable history API on YouTube in order to block ads in subsequent videos (Closed)
Patch Set: Only disable history.pushState if actually blocking ads Created Feb. 22, 2014, 5:51 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-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 pushStateDisabled = false;
23
22 function rewriteFlashvars(flashvars) 24 function rewriteFlashvars(flashvars)
23 { 25 {
24 var pairs = flashvars.split("&"); 26 var pairs = flashvars.split("&");
25 for (var i = 0; i < pairs.length; i++) 27 for (var i = 0; i < pairs.length; i++)
26 if (/^((ad|afv|adsense|iv)(_.*)?|(ad3|iv3|st)_module|prerolls|interstitial |infringe|invideo)=/.test(pairs[i])) 28 if (/^((ad|afv|adsense|iv)(_.*)?|(ad3|iv3|st)_module|prerolls|interstitial |infringe|invideo)=/.test(pairs[i]))
27 pairs.splice(i--, 1); 29 pairs.splice(i--, 1);
28 return pairs.join("&"); 30 return pairs.join("&");
29 } 31 }
30 32
31 function patchPlayer(player) 33 function patchPlayer(player)
(...skipping 22 matching lines...) Expand all
54 if (value != newValue) 56 if (value != newValue)
55 { 57 {
56 param.setAttribute("value", newValue); 58 param.setAttribute("value", newValue);
57 flashvarsChanged = true; 59 flashvarsChanged = true;
58 } 60 }
59 } 61 }
60 } 62 }
61 63
62 if (flashvarsChanged) 64 if (flashvarsChanged)
63 player.parentNode.replaceChild(newPlayer, player); 65 player.parentNode.replaceChild(newPlayer, player);
66
67 // if history.pushState is available, YouTube uses the history API
68 // when navigation from one video to another, and tells the flash
69 // player with JavaScript which video and which ads to show next,
70 // bypassing our flashvars rewrite code. So we disable
71 // history.pushState on pages with YouTube's flash player.
72 if (!pushStateDisabled)
73 {
74 var script = document.createElement("script");
75 script.type = "application/javascript";
76 script.textContent = "history.pushState = undefined;";
77 document.documentElement.appendChild(script);
Wladimir Palant 2014/02/24 13:15:03 We shouldn't leave this script around, it needs to
Sebastian Noack 2014/02/24 14:32:09 Done.
78
79 pushStateDisabled = true;
Wladimir Palant 2014/02/24 13:15:03 Why is this flag necessary? Content scripts should
Sebastian Noack 2014/02/24 14:32:09 Yes, but the code runs per player loaded. However
80 }
64 } 81 }
65 82
66 var deferred = []; 83 var deferred = [];
67 function patchPlayerDeferred(player) 84 function patchPlayerDeferred(player)
68 { 85 {
69 deferred.push(player); 86 deferred.push(player);
70 } 87 }
71 88
72 var onBeforeLoadYoutubeVideo = patchPlayerDeferred; 89 var onBeforeLoadYoutubeVideo = patchPlayerDeferred;
73 function onBeforeLoad(event) 90 function onBeforeLoad(event)
74 { 91 {
75 if ((event.target.localName == "object" || event.target.localName == "embed" ) && /:\/\/[^\/]*\.ytimg\.com\//.test(event.url)) 92 if ((event.target.localName == "object" || event.target.localName == "embed" ) && /:\/\/[^\/]*\.ytimg\.com\//.test(event.url))
76 onBeforeLoadYoutubeVideo(event.target); 93 onBeforeLoadYoutubeVideo(event.target);
77 } 94 }
78 95
79 ext.backgroundPage.sendMessage({type: "get-domain-enabled-state"}, function(re sponse) 96 ext.backgroundPage.sendMessage({type: "get-domain-enabled-state"}, function(re sponse)
80 { 97 {
81 if (response.enabled) 98 if (response.enabled)
82 { 99 {
83 deferred.forEach(patchPlayer); 100 deferred.forEach(patchPlayer);
84 onBeforeLoadYoutubeVideo = patchPlayer; 101 onBeforeLoadYoutubeVideo = patchPlayer;
85 } 102 }
86 else 103 else
87 document.removeEventListener("beforeload", onBeforeLoad); 104 document.removeEventListener("beforeload", onBeforeLoad);
88 }); 105 });
89 106
90 document.addEventListener("beforeload", onBeforeLoad, true); 107 document.addEventListener("beforeload", onBeforeLoad, true);
91 })(); 108 })();
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