From ceb18d4629355e5ef82f37fa4dd04475422e4dda Mon Sep 17 00:00:00 2001 From: Deathamns Date: Sun, 2 Nov 2014 17:20:06 +0100 Subject: [PATCH] Site-patching possibility for Safari Safari's extension API doesn't provide a way to intercept requests initiated by plugins, so those cases need special care (or at least the popular sites). This commit adds a new JS file (sitepatch-safari.js), which will store the patches (if it's possible to create one) for specific sites. As an example, this commit includes a technique for removing in-video ads from YouTube videos. --- src/js/vapi-background.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/js/vapi-background.js b/src/js/vapi-background.js index 4344530..c56d806 100644 --- a/src/js/vapi-background.js +++ b/src/js/vapi-background.js @@ -4,10 +4,10 @@ (function() { 'use strict'; -window.vAPI = window.vAPI || {}; +self.vAPI = self.vAPI || {}; -if (window.chrome) { - var chrome = window.chrome; +if (self.chrome) { + var chrome = self.chrome; vAPI.chrome = true; @@ -272,9 +272,22 @@ if (window.chrome) { chrome.contextMenus.remove(this.menuId); } }; -} else if (window.safari) { +} else if (self.safari) { vAPI.safari = true; + // addContentScriptFromURL allows whitelisting, + // so load sitepaching this way, instead of adding it to the Info.plist + safari.extension.addContentScriptFromURL( + safari.extension.baseURI + 'js/sitepatch-safari.js', + [ + 'http://www.youtube.com/*', + 'https://www.youtube.com/*', + 'http://www.youtube-nocookie.com/*', + 'https://www.youtube-nocookie.com/*' + ] + ); + + vAPI.storage = { _storage: safari.extension.settings, QUOTA_BYTES: 52428800, // copied from Info.plist @@ -709,6 +722,8 @@ if (window.chrome) { onBeforeRequest = onBeforeRequest.callback; this.onBeforeRequest.callback = function(e) { + var block; + if (e.name !== 'canLoad') { return; } @@ -718,6 +733,13 @@ if (window.chrome) { e.stopPropagation(); } + if (e.message.isWhiteListed) { + block = µBlock.URI.hostnameFromURI(e.message.isWhiteListed); + block = µBlock.URI.domainFromHostname(block) || block; + e.message = !!µBlock.netWhitelist[block]; + return e.message; + } + if (e.message.middleClickURL) { vAPI.lastMiddleClick = e.message; return; @@ -739,7 +761,7 @@ if (window.chrome) { return; } - var block = vAPI.net.onBeforeRequest; + block = vAPI.net.onBeforeRequest; if (block.types.indexOf(e.message.type) < 0) { return true; @@ -897,7 +919,7 @@ if (window.chrome) { }; } -if (!window.chrome) { - window.chrome = { runtime: { lastError: null } }; +if (!self.chrome) { + self.chrome = { runtime: { lastError: null } }; } })();