2014-11-15 11:15:11 -07:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-12-17 13:33:53 -07:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-11-15 11:15:11 -07: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
|
|
|
|
*/
|
|
|
|
|
2015-01-21 06:59:23 -07:00
|
|
|
/* global self, safari, SafariBrowserTab, µBlock */
|
2014-12-17 13:33:53 -07:00
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
// For background page
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2015-01-14 15:45:55 -07:00
|
|
|
var vAPI = self.vAPI = self.vAPI || {};
|
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
vAPI.safari = true;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-21 10:48:43 -07:00
|
|
|
vAPI.app = safari.extension.toolbarItems[0].toolTip.split(' ');
|
2014-12-01 12:45:00 -07:00
|
|
|
vAPI.app = {
|
2014-12-21 10:48:43 -07:00
|
|
|
name: vAPI.app[0],
|
2014-12-18 06:43:34 -07:00
|
|
|
version: vAPI.app[1]
|
2014-12-01 12:45:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-02 09:02:17 -07:00
|
|
|
vAPI.app.restart = function() {
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
// 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/*'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
safari.extension.settings.addEventListener('change', function(e) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( e.key === 'open_prefs' ) {
|
|
|
|
vAPI.tabs.open({
|
|
|
|
url: 'dashboard.html',
|
|
|
|
active: true
|
|
|
|
});
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.storage = {
|
|
|
|
_storage: safari.extension.settings,
|
|
|
|
QUOTA_BYTES: 52428800, // copied from Info.plist
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
get: function(keys, callback) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof callback !== 'function' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i, value, result = {};
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( keys === null ) {
|
|
|
|
for ( i in this._storage ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
value = this._storage[i];
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof value === 'string' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
result[i] = JSON.parse(value);
|
|
|
|
}
|
|
|
|
}
|
2014-12-28 13:26:06 -07:00
|
|
|
} else if ( typeof keys === 'string' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
value = this._storage[keys];
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof value === 'string' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
result[keys] = JSON.parse(value);
|
|
|
|
}
|
2014-12-28 13:26:06 -07:00
|
|
|
} else if ( Array.isArray(keys) ) {
|
|
|
|
for ( i = 0; i < keys.length; i++ ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
value = this._storage[i];
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof value === 'string' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
result[keys[i]] = JSON.parse(value);
|
|
|
|
}
|
|
|
|
}
|
2014-12-28 13:26:06 -07:00
|
|
|
} else if ( typeof keys === 'object' ) {
|
|
|
|
for ( i in keys ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
value = this._storage[i];
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof value === 'string' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
result[i] = JSON.parse(value);
|
2014-12-28 13:26:06 -07:00
|
|
|
} else {
|
2014-11-15 11:15:11 -07:00
|
|
|
result[i] = keys[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(result);
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
set: function(details, callback) {
|
2014-12-28 13:26:06 -07:00
|
|
|
for ( var key in details ) {
|
2015-01-18 11:50:20 -07:00
|
|
|
if ( !details.hasOwnProperty(key) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
this._storage.setItem(key, JSON.stringify(details[key]));
|
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof callback === 'function' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
remove: function(keys) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof keys === 'string' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
keys = [keys];
|
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
for ( var i = 0; i < keys.length; i++ ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
this._storage.removeItem(keys[i]);
|
|
|
|
}
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
clear: function(callback) {
|
|
|
|
this._storage.clear();
|
|
|
|
callback();
|
|
|
|
},
|
2014-12-28 13:26:06 -07:00
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
getBytesInUse: function(keys, callback) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof callback !== 'function' ) {
|
2014-12-01 08:23:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-29 10:04:39 -07:00
|
|
|
var i;
|
|
|
|
var size = 0;
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( keys === null ) {
|
2014-12-29 10:04:39 -07:00
|
|
|
for ( i in this._storage ) {
|
|
|
|
size += (this._storage[i] || '').length;
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
2014-12-28 13:26:06 -07:00
|
|
|
} else {
|
|
|
|
if ( typeof keys === 'string' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
keys = [keys];
|
|
|
|
}
|
|
|
|
|
2014-12-29 10:04:39 -07:00
|
|
|
for ( i = 0; i < keys.length; i++ ) {
|
|
|
|
size += (this._storage[keys[i]] || '').length;
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.tabs = {
|
|
|
|
stack: {},
|
2014-12-09 13:56:17 -07:00
|
|
|
stackId: 1
|
2014-11-23 10:21:06 -07:00
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2015-01-19 16:42:58 -07:00
|
|
|
vAPI.isNoTabId = function(tabId) {
|
|
|
|
return tabId.toString() === '-1';
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.noTabId = '-1';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.tabs.registerListeners = function() {
|
2015-01-19 01:50:10 -07:00
|
|
|
safari.application.addEventListener('beforeNavigate', function(e) {
|
2015-01-20 00:44:04 -07:00
|
|
|
if ( !vAPI.tabs.popupCandidate || !e.target || e.url === 'about:blank' ) {
|
2014-12-28 13:26:06 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-01-20 00:11:26 -07:00
|
|
|
var url = e.url, tabId = vAPI.tabs.getTabId(e.target);
|
2015-01-20 00:44:04 -07:00
|
|
|
var details = {
|
|
|
|
url: url,
|
|
|
|
tabId: tabId,
|
|
|
|
sourceTabId: vAPI.tabs.popupCandidate
|
|
|
|
};
|
|
|
|
vAPI.tabs.popupCandidate = false;
|
|
|
|
if ( vAPI.tabs.onPopup(details) ) {
|
|
|
|
e.preventDefault();
|
|
|
|
if ( vAPI.tabs.stack[details.sourceTabId] ) {
|
|
|
|
vAPI.tabs.stack[details.sourceTabId].activate();
|
2015-01-19 01:50:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, true);
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
// onClosed handled in the main tab-close event
|
2014-12-01 08:30:17 -07:00
|
|
|
// onUpdated handled via monitoring the history.pushState on web-pages
|
|
|
|
// onPopup is handled in window.open on web-pages
|
2014-11-23 10:21:06 -07:00
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.tabs.getTabId = function(tab) {
|
2014-12-28 13:26:06 -07:00
|
|
|
for ( var i in vAPI.tabs.stack ) {
|
|
|
|
if ( vAPI.tabs.stack[i] === tab ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
return +i;
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
return -1;
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.tabs.get = function(tabId, callback) {
|
|
|
|
var tab;
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( tabId === null ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
tab = safari.application.activeBrowserWindow.activeTab;
|
|
|
|
tabId = this.getTabId(tab);
|
2014-12-28 13:26:06 -07:00
|
|
|
} else {
|
2014-11-23 10:21:06 -07:00
|
|
|
tab = this.stack[tabId];
|
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( !tab ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
callback({
|
|
|
|
id: tabId,
|
|
|
|
index: tab.browserWindow.tabs.indexOf(tab),
|
|
|
|
windowId: safari.application.browserWindows.indexOf(tab.browserWindow),
|
|
|
|
active: tab === tab.browserWindow.activeTab,
|
|
|
|
url: tab.url,
|
|
|
|
title: tab.title
|
|
|
|
});
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-01 08:23:19 -07:00
|
|
|
// properties of the details object:
|
|
|
|
// url: 'URL', // the address that will be opened
|
|
|
|
// tabId: 1, // the tab is used if set, instead of creating a new one
|
|
|
|
// index: -1, // undefined: end of the list, -1: following tab, or after index
|
|
|
|
// active: false, // opens the tab in background - true and undefined: foreground
|
|
|
|
// select: true // if a tab is already opened with that url, then select it instead of opening a new one
|
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.tabs.open = function(details) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( !details.url ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// extension pages
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( /^[\w-]{2,}:/.test(details.url) === false ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
details.url = vAPI.getURL(details.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
var curWin, tab;
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( details.select ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
tab = safari.application.browserWindows.some(function(win) {
|
|
|
|
var rgxHash = /#.*/;
|
|
|
|
// this is questionable
|
|
|
|
var url = details.url.replace(rgxHash, '');
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
for ( var i = 0; i < win.tabs.length; i++ ) {
|
|
|
|
if ( win.tabs[i].url.replace(rgxHash, '') === url ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
win.tabs[i].activate();
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( tab ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( details.active === undefined ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
details.active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
curWin = safari.application.activeBrowserWindow;
|
|
|
|
|
|
|
|
// it must be calculated before opening a new tab,
|
|
|
|
// otherwise the new tab will be the active tab here
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( details.index === -1 ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
details.index = curWin.tabs.indexOf(curWin.activeTab) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tab = details.tabId && this.stack[details.tabId]
|
|
|
|
|| curWin.openTab(details.active ? 'foreground' : 'background');
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( details.index !== undefined ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
curWin.insertTab(tab, details.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
tab.url = details.url;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-01 12:16:18 -07:00
|
|
|
vAPI.tabs.remove = function(tabIds) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( tabIds instanceof SafariBrowserTab ) {
|
2014-12-01 08:23:19 -07:00
|
|
|
tabIds = this.getTabId(tabIds);
|
2014-11-23 10:21:06 -07:00
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( !Array.isArray(tabIds) ) {
|
2014-12-01 08:23:19 -07:00
|
|
|
tabIds = [tabIds];
|
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
for ( var i = 0; i < tabIds.length; i++ ) {
|
|
|
|
if ( this.stack[tabIds[i]] ) {
|
2014-12-01 08:23:19 -07:00
|
|
|
this.stack[tabIds[i]].close();
|
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 13:11:43 -07:00
|
|
|
vAPI.tabs.reload = function(tabId) {
|
|
|
|
var tab = this.stack[tabId];
|
|
|
|
|
|
|
|
if ( tab ) {
|
|
|
|
tab.url = tab.url;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.tabs.injectScript = function(tabId, details, callback) {
|
|
|
|
var tab;
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( tabId ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
tab = this.stack[tabId];
|
2014-12-28 13:26:06 -07:00
|
|
|
} else {
|
2014-11-23 10:21:06 -07:00
|
|
|
tab = safari.application.activeBrowserWindow.activeTab;
|
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( details.file ) {
|
2015-01-21 06:59:23 -07:00
|
|
|
var xhr = new XMLHttpRequest();
|
2014-11-23 10:21:06 -07:00
|
|
|
xhr.overrideMimeType('application/x-javascript;charset=utf-8');
|
|
|
|
xhr.open('GET', details.file, false);
|
|
|
|
xhr.send();
|
|
|
|
details.code = xhr.responseText;
|
|
|
|
}
|
|
|
|
|
|
|
|
tab.page.dispatchMessage('broadcast', {
|
2014-12-28 13:26:06 -07:00
|
|
|
channelName: 'vAPI',
|
2014-11-23 10:21:06 -07:00
|
|
|
msg: {
|
2014-12-16 14:31:03 -07:00
|
|
|
cmd: 'injectScript',
|
2014-11-23 10:21:06 -07:00
|
|
|
details: details
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
});
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof callback === 'function' ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
setTimeout(callback, 13);
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// bind tabs to unique IDs
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var wins = safari.application.browserWindows, i = wins.length, j;
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
while ( i-- ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
j = wins[i].tabs.length;
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
while ( j-- ) {
|
2014-12-09 13:56:17 -07:00
|
|
|
vAPI.tabs.stack[vAPI.tabs.stackId++] = wins[i].tabs[j];
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 13:56:17 -07:00
|
|
|
})();
|
2014-11-15 11:15:11 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
safari.application.addEventListener('open', function(e) {
|
|
|
|
// ignore windows
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( e.target instanceof SafariBrowserTab ) {
|
2014-12-09 13:56:17 -07:00
|
|
|
vAPI.tabs.stack[vAPI.tabs.stackId++] = e.target;
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
safari.application.addEventListener('close', function(e) {
|
|
|
|
// ignore windows
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( !(e.target instanceof SafariBrowserTab) ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tabId = vAPI.tabs.getTabId(e.target);
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( tabId !== -1 ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
// to not add another listener, put this here
|
|
|
|
// instead of vAPI.tabs.registerListeners
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( typeof vAPI.tabs.onClosed === 'function' ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
vAPI.tabs.onClosed(tabId);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete vAPI.tabIcons[tabId];
|
|
|
|
delete vAPI.tabs.stack[tabId];
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// update badge when tab is activated
|
|
|
|
safari.application.addEventListener('activate', function(e) {
|
|
|
|
// ignore windows
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( !(e.target instanceof SafariBrowserTab) ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the badge, when tab is selected
|
|
|
|
vAPI.setIcon();
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// reload the popup when that is opened
|
|
|
|
safari.application.addEventListener('popover', function(e) {
|
|
|
|
e.target.contentWindow.document.body.textContent = '';
|
|
|
|
e.target.contentWindow.location.reload();
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-28 02:56:09 -07:00
|
|
|
vAPI.tabIcons = { /*tabId: {badge: 0, img: suffix}*/ };
|
2014-12-26 13:41:44 -07:00
|
|
|
vAPI.setIcon = function(tabId, iconStatus, badge) {
|
2014-11-23 10:21:06 -07:00
|
|
|
var curTabId = vAPI.tabs.getTabId(
|
|
|
|
safari.application.activeBrowserWindow.activeTab
|
|
|
|
);
|
2014-11-15 11:15:11 -07:00
|
|
|
|
|
|
|
// from 'activate' event
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( tabId === undefined ) {
|
2014-11-15 11:15:11 -07:00
|
|
|
tabId = curTabId;
|
2014-12-28 13:26:06 -07:00
|
|
|
} else {
|
|
|
|
if ( badge && /\D/.test(badge) ) {
|
2014-12-26 13:41:44 -07:00
|
|
|
badge = 999;
|
|
|
|
}
|
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
vAPI.tabIcons[tabId] = {
|
2014-12-26 13:41:44 -07:00
|
|
|
badge: badge || 0,
|
|
|
|
img: iconStatus === 'on' ? '' : '-off'
|
2014-11-15 11:15:11 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( tabId !== curTabId ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
// if the selected tab has the same ID, then update the badge too,
|
|
|
|
// or always update it when changing tabs ('activate' event)
|
2014-12-28 13:26:06 -07:00
|
|
|
var items = safari.extension.toolbarItems;
|
2014-12-26 13:41:44 -07:00
|
|
|
var i = items.length;
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
while ( i-- ) {
|
|
|
|
if ( items[i].browserWindow === safari.application.activeBrowserWindow ) {
|
2014-12-26 13:41:44 -07:00
|
|
|
var icon = vAPI.tabIcons[tabId];
|
|
|
|
items[i].badge = icon && icon.badge || 0;
|
|
|
|
// TODO: a disabled icon for Safari
|
|
|
|
// items[i].img = vAPI.getURL(icon.img);
|
2014-11-23 10:21:06 -07:00
|
|
|
return;
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.messaging = {
|
|
|
|
listeners: {},
|
2014-11-23 10:21:06 -07:00
|
|
|
defaultHandler: null,
|
|
|
|
NOOPFUNC: function(){},
|
|
|
|
UNHANDLED: 'vAPI.messaging.notHandled'
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.messaging.listen = function(listenerName, callback) {
|
|
|
|
this.listeners[listenerName] = callback;
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.messaging.onMessage = function(request) {
|
|
|
|
var callback = vAPI.messaging.NOOPFUNC;
|
|
|
|
if ( request.message.requestId !== undefined ) {
|
|
|
|
callback = function(response) {
|
|
|
|
request.target.page.dispatchMessage(
|
|
|
|
request.name,
|
|
|
|
{
|
|
|
|
requestId: request.message.requestId,
|
2014-12-28 13:26:06 -07:00
|
|
|
channelName: request.message.channelName,
|
2014-11-23 10:21:06 -07:00
|
|
|
msg: response !== undefined ? response : null
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
);
|
2014-11-15 11:15:11 -07:00
|
|
|
};
|
2014-11-23 10:21:06 -07:00
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
var sender = {
|
|
|
|
tab: {
|
|
|
|
id: vAPI.tabs.getTabId(request.target)
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Specific handler
|
|
|
|
var r = vAPI.messaging.UNHANDLED;
|
2014-12-28 13:26:06 -07:00
|
|
|
var listener = vAPI.messaging.listeners[request.message.channelName];
|
2014-11-23 10:21:06 -07:00
|
|
|
if ( typeof listener === 'function' ) {
|
|
|
|
r = listener(request.message.msg, sender, callback);
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
if ( r !== vAPI.messaging.UNHANDLED ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default handler
|
|
|
|
r = vAPI.messaging.defaultHandler(request.message.msg, sender, callback);
|
|
|
|
if ( r !== vAPI.messaging.UNHANDLED ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error('µBlock> messaging > unknown request: %o', request.message);
|
|
|
|
|
|
|
|
// Unhandled:
|
|
|
|
// Need to callback anyways in case caller expected an answer, or
|
|
|
|
// else there is a memory leak on caller's side
|
|
|
|
callback();
|
2014-11-15 11:15:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.messaging.setup = function(defaultHandler) {
|
|
|
|
// Already setup?
|
|
|
|
if ( this.defaultHandler !== null ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
if ( typeof defaultHandler !== 'function' ) {
|
|
|
|
defaultHandler = function(){ return vAPI.messaging.UNHANDLED; };
|
|
|
|
}
|
|
|
|
this.defaultHandler = defaultHandler;
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
// the third parameter must stay false (bubbling), so later
|
|
|
|
// onBeforeRequest will use true (capturing), where we can invoke
|
|
|
|
// stopPropagation() (this way this.onMessage won't be fired)
|
|
|
|
safari.application.addEventListener('message', this.onMessage, false);
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.messaging.broadcast = function(message) {
|
|
|
|
message = {
|
|
|
|
broadcast: true,
|
|
|
|
msg: message
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
for ( var tabId in vAPI.tabs.stack ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.tabs.stack[tabId].page.dispatchMessage('broadcast', message);
|
|
|
|
}
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-07 12:51:49 -07:00
|
|
|
vAPI.net = {};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-12-01 12:45:00 -07:00
|
|
|
|
2015-01-25 00:43:39 -07:00
|
|
|
// Fast `contains`
|
2015-01-11 10:41:52 -07:00
|
|
|
|
2015-01-25 00:43:39 -07:00
|
|
|
Array.prototype.contains = function(a) {
|
|
|
|
var b = this.length;
|
|
|
|
while(b --) {
|
|
|
|
if(this[b] === a) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-28 13:26:06 -07:00
|
|
|
}
|
2015-01-25 00:43:39 -07:00
|
|
|
return false;
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2015-01-25 00:43:39 -07:00
|
|
|
/******************************************************************************/
|
2014-11-23 10:21:06 -07:00
|
|
|
|
2015-01-25 00:43:39 -07:00
|
|
|
vAPI.net.registerListeners = function() {
|
|
|
|
var µb = µBlock;
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2015-01-25 00:43:39 -07:00
|
|
|
// Until Safari has more specific events, those are instead handled
|
|
|
|
// in the onBeforeRequestAdapter; clean them up so they're garbage-collected
|
|
|
|
vAPI.net.onBeforeSendHeaders = null;
|
|
|
|
vAPI.net.onHeadersReceived = null;
|
|
|
|
|
|
|
|
var onBeforeRequest = vAPI.net.onBeforeRequest,
|
|
|
|
onBeforeRequestClient = onBeforeRequest.callback,
|
|
|
|
blockableTypes = onBeforeRequest.types;
|
|
|
|
|
|
|
|
var onBeforeRequestAdapter = function(e) {
|
|
|
|
if(e.name !== "canLoad") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.stopPropagation && e.stopPropagation();
|
|
|
|
switch(e.message.type) {
|
|
|
|
case "isWhiteListed":
|
|
|
|
e.message = !µb.getNetFilteringSwitch(e.message.url);
|
|
|
|
break;
|
|
|
|
case "navigatedToNew":
|
|
|
|
vAPI.tabs.onNavigation({
|
|
|
|
url: e.message.url,
|
|
|
|
frameId: 0,
|
|
|
|
tabId: vAPI.tabs.getTabId(e.target)
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "popup":
|
|
|
|
if(e.message.url === 'about:blank') {
|
|
|
|
vAPI.tabs.popupCandidate = vAPI.tabs.getTabId(e.target);
|
|
|
|
e.message = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
e.message = !vAPI.tabs.onPopup({
|
|
|
|
url: e.message.url,
|
|
|
|
tabId: 0,
|
|
|
|
sourceTabId: vAPI.tabs.getTabId(e.target)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "popstate":
|
|
|
|
vAPI.tabs.onUpdated(vAPI.tabs.getTabId(e.target),
|
|
|
|
{url: e.message.url},
|
|
|
|
{url: e.message.url});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(!blockableTypes.contains(e.message.type)) {
|
|
|
|
e.message = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.message.hostname = µb.URI.hostnameFromURI(e.message.url);
|
|
|
|
e.message.tabId = vAPI.tabs.getTabId(e.target);
|
|
|
|
var blockVerdict = onBeforeRequestClient(e.message);
|
|
|
|
if(blockVerdict && blockVerdict.cancel) {
|
|
|
|
e.message = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
e.message = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2014-12-28 13:26:06 -07:00
|
|
|
};
|
2015-01-25 00:43:39 -07:00
|
|
|
safari.application.addEventListener("message", onBeforeRequestAdapter, true);
|
2014-11-15 11:15:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-19 13:24:30 -07:00
|
|
|
vAPI.contextMenu = {
|
|
|
|
contextMap: {
|
|
|
|
frame: 'insideFrame',
|
|
|
|
link: 'linkHref',
|
|
|
|
image: 'srcUrl',
|
|
|
|
editable: 'editable'
|
|
|
|
}
|
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
/******************************************************************************/
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.contextMenu.create = function(details, callback) {
|
|
|
|
var contexts = details.contexts;
|
|
|
|
var menuItemId = details.id;
|
|
|
|
var menuTitle = details.title;
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( Array.isArray(contexts) && contexts.length ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
contexts = contexts.indexOf('all') === -1 ? contexts : null;
|
2014-12-28 13:26:06 -07:00
|
|
|
} else {
|
2014-11-23 10:21:06 -07:00
|
|
|
// default in Chrome
|
|
|
|
contexts = ['page'];
|
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
this.onContextMenu = function(e) {
|
|
|
|
var uI = e.userInfo;
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( !uI || /^https?:\/\//i.test(uI.pageUrl) === false ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( contexts ) {
|
|
|
|
var invalidContext = true;
|
|
|
|
var ctxMap = vAPI.contextMenu.contextMap;
|
2014-12-19 13:24:30 -07:00
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
for ( var i = 0; i < contexts.length; i++ ) {
|
|
|
|
var ctx = contexts[i];
|
|
|
|
|
|
|
|
if ( ctx === 'audio' || ctx === 'video' ) {
|
|
|
|
if ( uI[ctxMap['image']] && uI.tagName === ctx ) {
|
2014-12-19 13:24:30 -07:00
|
|
|
invalidContext = false;
|
|
|
|
break;
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
2014-12-28 13:26:06 -07:00
|
|
|
} else if ( uI[ctxMap[ctx]] ) {
|
|
|
|
invalidContext = false;
|
|
|
|
break;
|
|
|
|
} else if ( ctx === 'page' ) {
|
|
|
|
if ( !(uI.insideFrame || uI.linkHref
|
|
|
|
|| uI.mediaType || uI.editable) ) {
|
|
|
|
invalidContext = false;
|
|
|
|
break;
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( invalidContext ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-23 10:21:06 -07:00
|
|
|
}
|
2014-12-28 13:26:06 -07:00
|
|
|
|
|
|
|
e.contextMenu.appendContextMenuItem(menuItemId, menuTitle);
|
2014-11-23 10:21:06 -07:00
|
|
|
};
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
this.onContextMenuCmd = function(e) {
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( e.command === menuItemId ) {
|
2014-11-23 10:21:06 -07:00
|
|
|
var tab = e.currentTarget.activeBrowserWindow.activeTab;
|
|
|
|
e.userInfo.menuItemId = menuItemId;
|
|
|
|
callback(e.userInfo, tab ? {
|
|
|
|
id: vAPI.tabs.getTabId(tab),
|
|
|
|
url: tab.url
|
|
|
|
} : undefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
safari.application.addEventListener('contextmenu', this.onContextMenu);
|
2014-12-19 05:17:25 -07:00
|
|
|
safari.application.addEventListener('command', this.onContextMenuCmd);
|
2014-11-23 10:21:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.contextMenu.remove = function() {
|
|
|
|
safari.application.removeEventListener('contextmenu', this.onContextMenu);
|
2014-12-19 05:17:25 -07:00
|
|
|
safari.application.removeEventListener('command', this.onContextMenuCmd);
|
2014-11-23 10:21:06 -07:00
|
|
|
this.onContextMenu = null;
|
|
|
|
this.onContextMenuCmd = null;
|
2014-11-15 11:15:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-23 10:21:06 -07:00
|
|
|
vAPI.lastError = function() {
|
2014-11-15 11:15:11 -07:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-17 06:02:37 -07:00
|
|
|
// This is called only once, when everything has been loaded in memory after
|
|
|
|
// the extension was launched. It can be used to inject content scripts
|
|
|
|
// in already opened web pages, to remove whatever nuisance could make it to
|
|
|
|
// the web pages before uBlock was ready.
|
|
|
|
|
|
|
|
vAPI.onLoadAllCompleted = function() {
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-14 15:45:55 -07:00
|
|
|
vAPI.punycodeHostname = function(hostname) {
|
|
|
|
return hostname;
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.punycodeURL = function(url) {
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
})();
|