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.
pull/2/head
Deathamns 10 years ago committed by gorhill
parent c0b1565131
commit ceb18d4629

@ -4,10 +4,10 @@
(function() { (function() {
'use strict'; 'use strict';
window.vAPI = window.vAPI || {}; self.vAPI = self.vAPI || {};
if (window.chrome) { if (self.chrome) {
var chrome = window.chrome; var chrome = self.chrome;
vAPI.chrome = true; vAPI.chrome = true;
@ -272,9 +272,22 @@ if (window.chrome) {
chrome.contextMenus.remove(this.menuId); chrome.contextMenus.remove(this.menuId);
} }
}; };
} else if (window.safari) { } else if (self.safari) {
vAPI.safari = true; 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 = { vAPI.storage = {
_storage: safari.extension.settings, _storage: safari.extension.settings,
QUOTA_BYTES: 52428800, // copied from Info.plist QUOTA_BYTES: 52428800, // copied from Info.plist
@ -709,6 +722,8 @@ if (window.chrome) {
onBeforeRequest = onBeforeRequest.callback; onBeforeRequest = onBeforeRequest.callback;
this.onBeforeRequest.callback = function(e) { this.onBeforeRequest.callback = function(e) {
var block;
if (e.name !== 'canLoad') { if (e.name !== 'canLoad') {
return; return;
} }
@ -718,6 +733,13 @@ if (window.chrome) {
e.stopPropagation(); 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) { if (e.message.middleClickURL) {
vAPI.lastMiddleClick = e.message; vAPI.lastMiddleClick = e.message;
return; return;
@ -739,7 +761,7 @@ if (window.chrome) {
return; return;
} }
var block = vAPI.net.onBeforeRequest; block = vAPI.net.onBeforeRequest;
if (block.types.indexOf(e.message.type) < 0) { if (block.types.indexOf(e.message.type) < 0) {
return true; return true;
@ -897,7 +919,7 @@ if (window.chrome) {
}; };
} }
if (!window.chrome) { if (!self.chrome) {
window.chrome = { runtime: { lastError: null } }; self.chrome = { runtime: { lastError: null } };
} }
})(); })();

Loading…
Cancel
Save