2014-11-15 19:15:11 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-11-15 19:15:11 +01:00
|
|
|
Copyright (C) 2014 The µBlock authors
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
// For non background pages
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
self.vAPI = self.vAPI || {};
|
2014-12-01 16:23:19 +01:00
|
|
|
vAPI.safari = true;
|
2014-11-15 19:15:11 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var messagingConnector = function(response) {
|
2014-11-23 18:21:06 +01:00
|
|
|
if ( !response ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-01 16:23:19 +01:00
|
|
|
var channels = vAPI.messaging.channels;
|
2014-11-23 18:21:06 +01:00
|
|
|
var channel, listener;
|
2014-11-15 19:15:11 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( response.broadcast === true && !response.channelName ) {
|
2014-12-01 16:23:19 +01:00
|
|
|
for ( channel in channels ) {
|
|
|
|
if ( channels.hasOwnProperty(channel) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
listener = channels[channel].listener;
|
2014-11-23 18:21:06 +01:00
|
|
|
if ( typeof listener === 'function' ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
listener(response.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:21:06 +01:00
|
|
|
if ( response.requestId ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
listener = vAPI.messaging.listeners[response.requestId];
|
|
|
|
delete vAPI.messaging.listeners[response.requestId];
|
|
|
|
delete response.requestId;
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:21:06 +01:00
|
|
|
if ( !listener ) {
|
2014-12-28 21:26:06 +01:00
|
|
|
channel = channels[response.channelName];
|
2014-11-15 19:15:11 +01:00
|
|
|
listener = channel && channel.listener;
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:21:06 +01:00
|
|
|
if ( typeof listener === 'function' ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
listener(response.msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-23 18:21:06 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
var uniqueId = function() {
|
2014-12-16 13:44:34 +01:00
|
|
|
return Math.random().toString(36).slice(2);
|
2014-11-15 19:15:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
// Relevant?
|
2014-11-15 19:15:11 +01:00
|
|
|
// https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/MessagesandProxies/MessagesandProxies.html#//apple_ref/doc/uid/TP40009977-CH14-SW12
|
|
|
|
vAPI.messaging = {
|
|
|
|
channels: {},
|
|
|
|
listeners: {},
|
2014-11-23 18:21:06 +01:00
|
|
|
requestId: 1,
|
2014-11-15 19:15:11 +01:00
|
|
|
connectorId: uniqueId(),
|
2014-11-23 18:21:06 +01:00
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
setup: function() {
|
|
|
|
this.connector = function(msg) {
|
|
|
|
// messages from the background script are sent to every frame,
|
|
|
|
// so we need to check the connectorId to accept only
|
|
|
|
// what is meant for the current context
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( msg.name === vAPI.messaging.connectorId
|
|
|
|
|| msg.name === 'broadcast' ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
messagingConnector(msg.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
safari.self.addEventListener('message', this.connector, false);
|
|
|
|
|
|
|
|
this.channels['vAPI'] = {
|
|
|
|
listener: function(msg) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( msg.cmd === 'injectScript' && msg.details.code ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
Function(msg.details.code).call(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
close: function() {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( this.connector ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
safari.self.removeEventListener('message', this.connector, false);
|
2014-11-23 18:21:06 +01:00
|
|
|
this.connector = null;
|
|
|
|
this.channels = {};
|
|
|
|
this.listeners = {};
|
2014-11-15 19:15:11 +01:00
|
|
|
}
|
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
channel: function(channelName, callback) {
|
2014-11-23 18:21:06 +01:00
|
|
|
if ( !channelName ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.channels[channelName] = {
|
2014-12-28 21:26:06 +01:00
|
|
|
channelName: channelName,
|
2014-11-15 19:15:11 +01:00
|
|
|
listener: typeof callback === 'function' ? callback : null,
|
|
|
|
send: function(message, callback) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !vAPI.messaging.connector ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
vAPI.messaging.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
message = {
|
2014-12-28 21:26:06 +01:00
|
|
|
channelName: this.channelName,
|
2014-11-15 19:15:11 +01:00
|
|
|
msg: message
|
|
|
|
};
|
|
|
|
|
2014-11-23 18:21:06 +01:00
|
|
|
if ( callback ) {
|
|
|
|
message.requestId = vAPI.messaging.requestId++;
|
2014-11-15 19:15:11 +01:00
|
|
|
vAPI.messaging.listeners[message.requestId] = callback;
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:21:06 +01:00
|
|
|
// popover content doesn't know messaging...
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( safari.extension.globalPage ) {
|
|
|
|
if ( !safari.self.visible ) {
|
2014-11-23 18:21:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
safari.extension.globalPage.contentWindow
|
2014-11-23 18:21:06 +01:00
|
|
|
.vAPI.messaging.onMessage({
|
2014-11-15 19:15:11 +01:00
|
|
|
name: vAPI.messaging.connectorId,
|
|
|
|
message: message,
|
|
|
|
target: {
|
|
|
|
page: {
|
|
|
|
dispatchMessage: function(name, msg) {
|
|
|
|
messagingConnector(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-12-28 21:26:06 +01:00
|
|
|
} else {
|
2014-11-15 19:15:11 +01:00
|
|
|
safari.self.tab.dispatchMessage(
|
|
|
|
vAPI.messaging.connectorId,
|
|
|
|
message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
close: function() {
|
2014-12-28 21:26:06 +01:00
|
|
|
delete vAPI.messaging.channels[this.channelName];
|
2014-11-15 19:15:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.channels[channelName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-01 16:23:19 +01:00
|
|
|
vAPI.canExecuteContentScript = function() {
|
|
|
|
return /^https?:/.test(location.protocol);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// This file can be included into extensin pages,
|
|
|
|
// but the following code should run only in content pages.
|
2014-11-23 18:21:06 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( location.protocol === 'safari-extension:' ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var frameId = window === window.top ? 0 : Date.now() % 1E5;
|
2015-01-19 09:50:10 +01:00
|
|
|
var parentFrameId = frameId ? 0 : -1;
|
2015-01-20 08:11:26 +01:00
|
|
|
var beforeLoadEvent = new Event('beforeload'); // Helper event to message background
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-01-20 08:11:26 +01:00
|
|
|
// Inform that we've navigated
|
|
|
|
if(frameId === 0) {
|
|
|
|
safari.self.tab.canLoad(beforeLoadEvent, {
|
|
|
|
url: location.href,
|
|
|
|
type: 'main_frame',
|
|
|
|
navigatedToNew: true
|
|
|
|
});
|
|
|
|
}
|
2014-11-15 19:15:11 +01:00
|
|
|
|
2015-01-20 08:11:26 +01:00
|
|
|
var linkHelper = document.createElement('a');
|
|
|
|
var nodeTypes = {
|
|
|
|
'frame': 'sub_frame',
|
|
|
|
'iframe': 'sub_frame',
|
|
|
|
'script': 'script',
|
|
|
|
'img': 'image',
|
|
|
|
'input': 'image',
|
|
|
|
'object': 'object',
|
|
|
|
'embed': 'object',
|
|
|
|
'link': 'stylesheet'
|
|
|
|
};
|
|
|
|
var shouldBlockDetailedRequest = function(details) {
|
|
|
|
linkHelper.href = details.url;
|
|
|
|
details.url = linkHelper.href;
|
2014-11-15 19:15:11 +01:00
|
|
|
details.frameId = frameId;
|
2015-01-19 09:50:10 +01:00
|
|
|
details.parentFrameId = parentFrameId;
|
2014-11-15 19:15:11 +01:00
|
|
|
details.timeStamp = Date.now();
|
2015-01-20 08:11:26 +01:00
|
|
|
return !(safari.self.tab.canLoad(beforeLoadEvent, details));
|
|
|
|
}
|
|
|
|
var onBeforeLoad = function(e) {
|
|
|
|
if(e.url.lastIndexOf('data:', 0) === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
linkHelper.href = e.url;
|
|
|
|
var url = linkHelper.href;
|
|
|
|
var details = {
|
|
|
|
url: url,
|
|
|
|
type: nodeTypes[e.target.nodeName.toLowerCase()] || 'other',
|
|
|
|
// tabId is determined in the background script
|
|
|
|
frameId: frameId,
|
|
|
|
parentFrameId: parentFrameId,
|
|
|
|
timeStamp: Date.now()
|
|
|
|
};
|
2014-11-15 19:15:11 +01:00
|
|
|
var response = safari.self.tab.canLoad(e, details);
|
2015-01-20 08:11:26 +01:00
|
|
|
if(!response) {
|
|
|
|
e.preventDefault();
|
2014-11-15 19:15:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-12-28 21:26:06 +01:00
|
|
|
// Local mirroring, response should be a data: URL here
|
2015-01-20 08:11:26 +01:00
|
|
|
if(typeof response !== 'string') {
|
2014-12-28 21:26:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-01-20 08:11:26 +01:00
|
|
|
// Okay, we're mirroring...
|
2014-12-28 21:26:06 +01:00
|
|
|
e.preventDefault();
|
2014-12-29 18:04:39 +01:00
|
|
|
// Content Security Policy with disallowed inline scripts may break things
|
2014-12-28 21:26:06 +01:00
|
|
|
details = document.createElement('script');
|
|
|
|
details.textContent = atob(response.slice(response.indexOf(',', 20) + 1));
|
|
|
|
|
|
|
|
if ( e.target.hasAttribute('defer') && document.readyState === 'loading' ) {
|
|
|
|
var jsOnLoad = function(ev) {
|
|
|
|
this.removeEventListener(ev.type, jsOnLoad, true);
|
|
|
|
this.body.removeChild(this.body.appendChild(details));
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', jsOnLoad, true);
|
|
|
|
} else {
|
|
|
|
e.target.parentNode.insertBefore(details, e.target);
|
|
|
|
details.parentNode.removeChild(details);
|
2014-11-15 19:15:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('beforeload', onBeforeLoad, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// block pop-ups, intercept xhr requests, and apply site patches
|
|
|
|
var firstMutation = function() {
|
|
|
|
document.removeEventListener('DOMSubtreeModified', firstMutation, true);
|
2015-01-19 09:50:10 +01:00
|
|
|
firstMutation = false;
|
2014-12-28 21:26:06 +01:00
|
|
|
|
|
|
|
var randEventName = uniqueId();
|
2014-11-15 19:15:11 +01:00
|
|
|
|
|
|
|
window.addEventListener(randEventName, function(e) {
|
2015-01-20 08:11:26 +01:00
|
|
|
if(shouldBlockDetailedRequest(e.detail)) {
|
2014-11-15 19:15:11 +01:00
|
|
|
e.detail.url = false;
|
2015-01-20 08:11:26 +01:00
|
|
|
};
|
2014-11-15 19:15:11 +01:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
// the extension context is unable to reach the page context,
|
|
|
|
// also this only works when Content Security Policy allows inline scripts
|
|
|
|
var tmpJS = document.createElement('script');
|
2014-12-01 16:30:17 +01:00
|
|
|
var tmpScript = ['(function() {',
|
|
|
|
'var block = function(u, t) {',
|
2015-01-19 09:50:10 +01:00
|
|
|
'var e = new CustomEvent("' + randEventName +'",',
|
|
|
|
'{detail: {url: u, type: t}, bubbles: false});',
|
2014-12-01 16:30:17 +01:00
|
|
|
'dispatchEvent(e);',
|
2015-01-19 09:50:10 +01:00
|
|
|
'return e.detail.url === false;',
|
2014-12-01 16:30:17 +01:00
|
|
|
'}, wo = open, xo = XMLHttpRequest.prototype.open;',
|
|
|
|
'open = function(u) {',
|
|
|
|
'return block(u, "popup") ? null : wo.apply(this, arguments);',
|
|
|
|
'};',
|
|
|
|
'XMLHttpRequest.prototype.open = function(m, u, s) {',
|
2014-12-28 21:26:06 +01:00
|
|
|
'return xo.apply(',
|
|
|
|
'this,',
|
|
|
|
'block(u, "xmlhttprequest") ? ["HEAD", u, s] : arguments',
|
|
|
|
');',
|
2014-12-01 16:30:17 +01:00
|
|
|
'};'
|
2014-11-15 19:15:11 +01:00
|
|
|
];
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( frameId === 0 ) {
|
2014-12-01 16:30:17 +01:00
|
|
|
tmpScript.push(
|
|
|
|
'var pS = history.pushState, rS = history.replaceState,',
|
|
|
|
'onpopstate = function(e) {',
|
|
|
|
'if (!e || e.state !== null) block(location.href, "popstate");',
|
|
|
|
'};',
|
|
|
|
'window.addEventListener("popstate", onpopstate, true);',
|
|
|
|
'history.pushState = function() {',
|
|
|
|
'var r = pS.apply(this, arguments);',
|
|
|
|
'onpopstate();',
|
|
|
|
'return r;',
|
|
|
|
'};',
|
|
|
|
'history.replaceState = function() {',
|
2014-12-16 13:44:34 +01:00
|
|
|
'var r = rS.apply(this, arguments);',
|
2014-12-01 16:30:17 +01:00
|
|
|
'onpopstate();',
|
|
|
|
'return r;',
|
|
|
|
'};'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var block = safari.self.tab.canLoad(beforeLoadEvent, {
|
2014-12-28 21:26:06 +01:00
|
|
|
isURLWhiteListed: location.href
|
2014-12-01 16:30:17 +01:00
|
|
|
});
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( vAPI.sitePatch && !block ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
tmpScript.push('(' + vAPI.sitePatch + ')();');
|
|
|
|
}
|
|
|
|
|
2014-12-01 16:30:17 +01:00
|
|
|
tmpScript.push('})();');
|
2014-11-15 19:15:11 +01:00
|
|
|
tmpJS.textContent = tmpScript.join('');
|
2014-12-28 21:26:06 +01:00
|
|
|
document.documentElement.removeChild(
|
|
|
|
document.documentElement.appendChild(tmpJS)
|
|
|
|
);
|
2014-11-15 19:15:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('DOMSubtreeModified', firstMutation, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var onContextMenu = function(e) {
|
2014-12-28 21:26:06 +01:00
|
|
|
var target = e.target;
|
2014-12-29 18:04:39 +01:00
|
|
|
var tagName = target.tagName.toLowerCase();
|
2014-11-15 19:15:11 +01:00
|
|
|
var details = {
|
2014-12-29 18:04:39 +01:00
|
|
|
tagName: tagName,
|
2014-11-15 19:15:11 +01:00
|
|
|
pageUrl: location.href,
|
|
|
|
insideFrame: window !== window.top
|
|
|
|
};
|
|
|
|
|
2014-12-29 18:04:39 +01:00
|
|
|
details.editable = tagName === 'textarea' || tagName === 'input';
|
2014-11-15 19:15:11 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( target.hasOwnProperty('checked') ) {
|
|
|
|
details.checked = target.checked;
|
2014-11-15 19:15:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-29 18:04:39 +01:00
|
|
|
if ( tagName === 'a' ) {
|
2014-12-28 21:26:06 +01:00
|
|
|
details.linkUrl = target.href;
|
2014-11-15 19:15:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( target.hasOwnProperty('src') ) {
|
|
|
|
details.srcUrl = target.src;
|
2014-11-15 19:15:11 +01:00
|
|
|
|
2014-12-29 18:04:39 +01:00
|
|
|
if ( tagName === 'img' ) {
|
2014-11-15 19:15:11 +01:00
|
|
|
details.mediaType = 'image';
|
2014-12-29 18:04:39 +01:00
|
|
|
} else if ( tagName === 'video' || tagName === 'audio' ) {
|
|
|
|
details.mediaType = tagName;
|
2014-11-15 19:15:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
safari.self.tab.setContextMenuEventUserInfo(e, details);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.addEventListener('contextmenu', onContextMenu, true);
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|