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

Unified Diff: safari/include.youtube.js

Issue 4922142982406144: Implemented blocking of YouTube video ads, by rewriting flashvars, for Safari (Closed)
Patch Set: Forgot to remove include.youtube.js from metadata.common Created Dec. 20, 2013, 10:28 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 | « safari/ext/content.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: safari/include.youtube.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/safari/include.youtube.js
@@ -0,0 +1,91 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2013 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+(function() {
+ if (document.domain != "www.youtube.com")
+ return;
+
+ function rewriteFlashvars(flashvars)
+ {
+ var pairs = flashvars.split("&");
+ for (var i = 0; i < pairs.length; i++)
+ if (/^((ad|afv|adsense|iv)(_.*)?|(ad3|iv3|st)_module|prerolls|interstitial|infringe|invideo)=/.test(pairs[i]))
+ pairs.splice(i--, 1);
+ return pairs.join("&");
+ }
+
+ function patchPlayer(player)
+ {
+ var newPlayer = player.cloneNode(true);
+ var flashvarsChanged = false;
+
+ var flashvars = newPlayer.getAttribute("flashvars");
+ if (flashvars)
+ {
+ var newFlashvars = rewriteFlashvars(flashvars);
+ if (flashvars != newFlashvars)
+ {
+ newPlayer.setAttribute("flashvars", newFlashvars);
+ flashvarsChanged = true;
+ }
+ }
+
+ var param = newPlayer.querySelector("param[name=flashvars]");
+ if (param)
+ {
+ var value = param.getAttribute("value");
+ if (value)
+ {
+ var newValue = rewriteFlashvars(value);
+ if (value != newValue)
+ {
+ param.setAttribute("value", newValue);
+ flashvarsChanged = true;
+ }
+ }
+ }
+
+ if (flashvarsChanged)
+ player.parentNode.replaceChild(newPlayer, player);
+ }
+
+ var deferred = [];
+ function patchPlayerDeferred(player)
+ {
+ deferred.push(player);
+ }
+
+ var onBeforeLoadYoutubeVideo = patchPlayerDeferred;
+ function onBeforeLoad(event)
+ {
+ if ((event.target.localName == "object" || event.target.localName == "embed") && /:\/\/[^\/]*\.ytimg\.com\//.test(event.url))
+ onBeforeLoadYoutubeVideo(event.target);
+ }
+
+ ext.backgroundPage.sendMessage({type: "get-domain-enabled-state"}, function(response)
+ {
+ if (response.enabled)
+ {
+ deferred.forEach(patchPlayer);
+ onBeforeLoadYoutubeVideo = patchPlayer;
+ }
+ else
+ document.removeEventListener("beforeload", onBeforeLoad);
+ });
+
+ document.addEventListener("beforeload", onBeforeLoad, true);
+})();
« no previous file with comments | « safari/ext/content.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld