2014-11-24 20:00:27 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-11-24 20:00:27 +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
|
|
|
|
*/
|
|
|
|
|
2015-01-21 14:59:23 +01:00
|
|
|
/* jshint esnext: true, bitwise: false */
|
2015-03-16 17:30:05 +01:00
|
|
|
/* global self, Components, punycode, µBlock */
|
2015-01-15 13:24:35 +01:00
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
// For background page
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
2015-01-09 07:58:07 +01:00
|
|
|
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-14 23:45:55 +01:00
|
|
|
var vAPI = self.vAPI = self.vAPI || {};
|
2014-11-24 20:00:27 +01:00
|
|
|
vAPI.firefox = true;
|
2015-02-28 20:18:58 +01:00
|
|
|
vAPI.fennec = Services.appinfo.ID === '{aa3c5121-dab2-40e2-81ca-7ea25febc110}';
|
2015-02-12 19:19:17 +01:00
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-02 08:35:25 +01:00
|
|
|
vAPI.app = {
|
2015-04-16 05:25:30 +02:00
|
|
|
name: 'uBlock Origin',
|
2015-01-29 14:12:21 +01:00
|
|
|
version: location.hash.slice(1)
|
2014-12-02 08:35:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-29 17:54:18 +01:00
|
|
|
vAPI.app.restart = function() {
|
2015-01-04 13:58:17 +01:00
|
|
|
// Listening in bootstrap.js
|
|
|
|
Cc['@mozilla.org/childprocessmessagemanager;1']
|
|
|
|
.getService(Ci.nsIMessageSender)
|
|
|
|
.sendAsyncMessage(location.host + '-restart');
|
2014-12-29 17:54:18 +01:00
|
|
|
};
|
2014-12-07 20:51:49 +01:00
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
// List of things that needs to be destroyed when disabling the extension
|
|
|
|
// Only functions should be added to it
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2015-01-12 20:39:23 +01:00
|
|
|
var cleanupTasks = [];
|
2014-12-07 20:51:49 +01:00
|
|
|
|
2015-03-18 12:47:07 +01:00
|
|
|
// This must be updated manually, every time a new task is added/removed
|
2015-04-10 03:50:23 +02:00
|
|
|
|
|
|
|
// Fixed by github.com/AlexVallat:
|
|
|
|
// https://github.com/AlexVallat/uBlock/commit/7b781248f00cbe3d61b1cc367c440db80fa06049
|
|
|
|
// 7 instances of cleanupTasks.push, but one is unique to fennec, and one to desktop.
|
2015-05-31 23:43:19 +02:00
|
|
|
var expectedNumberOfCleanups = 6;
|
2015-03-18 12:47:07 +01:00
|
|
|
|
|
|
|
window.addEventListener('unload', function() {
|
2015-05-31 23:43:19 +02:00
|
|
|
if ( typeof vAPI.app.onShutdown === 'function' ) {
|
|
|
|
vAPI.app.onShutdown();
|
|
|
|
}
|
|
|
|
|
2015-03-18 12:47:07 +01:00
|
|
|
for ( var cleanup of cleanupTasks ) {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( cleanupTasks.length < expectedNumberOfCleanups ) {
|
|
|
|
console.error(
|
|
|
|
'uBlock> Cleanup tasks performed: %s (out of %s)',
|
|
|
|
cleanupTasks.length,
|
|
|
|
expectedNumberOfCleanups
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// frameModule needs to be cleared too
|
|
|
|
var frameModule = {};
|
|
|
|
Cu.import(vAPI.getURL('frameModule.js'), frameModule);
|
|
|
|
frameModule.contentObserver.unregister();
|
|
|
|
Cu.unload(vAPI.getURL('frameModule.js'));
|
|
|
|
});
|
|
|
|
|
2014-12-07 20:51:49 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
// API matches that of chrome.storage.local:
|
|
|
|
// https://developer.chrome.com/extensions/storage
|
|
|
|
|
|
|
|
vAPI.storage = (function() {
|
|
|
|
var db = null;
|
2015-05-30 17:36:37 +02:00
|
|
|
var vacuumTimer = null;
|
2015-05-27 21:31:36 +02:00
|
|
|
|
|
|
|
var close = function() {
|
2015-05-30 17:36:37 +02:00
|
|
|
if ( vacuumTimer !== null ) {
|
|
|
|
clearTimeout(vacuumTimer);
|
|
|
|
vacuumTimer = null;
|
|
|
|
}
|
2015-05-27 21:31:36 +02:00
|
|
|
if ( db === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
db.asyncClose();
|
|
|
|
db = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
var open = function() {
|
|
|
|
if ( db !== null ) {
|
2015-05-28 20:49:01 +02:00
|
|
|
return db;
|
2015-05-27 21:31:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create path
|
2014-11-27 20:45:54 +01:00
|
|
|
var path = Services.dirsvc.get('ProfD', Ci.nsIFile);
|
|
|
|
path.append('extension-data');
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !path.exists() ) {
|
2014-11-27 20:45:54 +01:00
|
|
|
path.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0774', 8));
|
|
|
|
}
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !path.isDirectory() ) {
|
2014-11-27 20:45:54 +01:00
|
|
|
throw Error('Should be a directory...');
|
|
|
|
}
|
2014-12-18 11:59:04 +01:00
|
|
|
path.append(location.host + '.sqlite');
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
// Open database
|
2015-05-28 20:49:01 +02:00
|
|
|
try {
|
|
|
|
db = Services.storage.openDatabase(path);
|
|
|
|
if ( db.connectionReady === false ) {
|
|
|
|
db.asyncClose();
|
|
|
|
db = null;
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( db === null ) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
// Database was opened, register cleanup task
|
|
|
|
cleanupTasks.push(close);
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
// Setup database
|
2015-05-30 17:36:37 +02:00
|
|
|
db.createAsyncStatement('CREATE TABLE IF NOT EXISTS "settings" ("name" TEXT PRIMARY KEY NOT NULL, "value" TEXT);')
|
2015-05-27 21:31:36 +02:00
|
|
|
.executeAsync();
|
2015-05-28 20:49:01 +02:00
|
|
|
|
2015-05-30 17:36:37 +02:00
|
|
|
if ( vacuum !== null ) {
|
|
|
|
vacuumTimer = vAPI.setTimeout(vacuum, 60000);
|
|
|
|
}
|
|
|
|
|
2015-05-28 20:49:01 +02:00
|
|
|
return db;
|
2015-05-27 21:31:36 +02:00
|
|
|
};
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-30 17:36:37 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Storage/Performance#Vacuuming_and_zero-fill
|
|
|
|
// Vacuum only once, and only while idle
|
|
|
|
var vacuum = function() {
|
|
|
|
vacuumTimer = null;
|
|
|
|
if ( db === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var idleSvc = Cc['@mozilla.org/widget/idleservice;1']
|
|
|
|
.getService(Ci.nsIIdleService);
|
|
|
|
if ( idleSvc.idleTime < 60000 ) {
|
|
|
|
vacuumTimer = vAPI.setTimeout(vacuum, 60000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
db.createAsyncStatement('VACUUM').executeAsync();
|
|
|
|
vacuum = null;
|
|
|
|
};
|
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
// Execute a query
|
|
|
|
var runStatement = function(stmt, callback) {
|
|
|
|
var result = {};
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
stmt.executeAsync({
|
2014-11-27 20:45:54 +01:00
|
|
|
handleResult: function(rows) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !rows || typeof callback !== 'function' ) {
|
2014-11-27 20:45:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var row;
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
while ( row = rows.getNextRow() ) {
|
2014-11-27 20:45:54 +01:00
|
|
|
// we assume that there will be two columns, since we're
|
|
|
|
// using it only for preferences
|
|
|
|
result[row.getResultByIndex(0)] = row.getResultByIndex(1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleCompletion: function(reason) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( typeof callback === 'function' && reason === 0 ) {
|
2014-11-27 20:45:54 +01:00
|
|
|
callback(result);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleError: function(error) {
|
|
|
|
console.error('SQLite error ', error.result, error.message);
|
2015-05-28 20:49:01 +02:00
|
|
|
// Caller expects an answer regardless of failure.
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback(null);
|
|
|
|
}
|
2014-11-27 20:45:54 +01:00
|
|
|
}
|
|
|
|
});
|
2015-05-27 21:31:36 +02:00
|
|
|
};
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
var bindNames = function(stmt, names) {
|
|
|
|
if ( Array.isArray(names) === false || names.length === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var params = stmt.newBindingParamsArray();
|
|
|
|
var i = names.length, bp;
|
|
|
|
while ( i-- ) {
|
|
|
|
bp = params.newBindingParams();
|
|
|
|
bp.bindByName('name', names[i]);
|
|
|
|
params.addParams(bp);
|
|
|
|
}
|
|
|
|
stmt.bindParameters(params);
|
|
|
|
};
|
|
|
|
|
|
|
|
var clear = function(callback) {
|
2015-05-28 20:49:01 +02:00
|
|
|
if ( open() === null ) {
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
return;
|
2015-05-27 21:31:36 +02:00
|
|
|
}
|
2015-05-30 17:36:37 +02:00
|
|
|
runStatement(db.createAsyncStatement('DELETE FROM "settings";'), callback);
|
2015-05-27 21:31:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var getBytesInUse = function(keys, callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-28 20:49:01 +02:00
|
|
|
if ( open() === null ) {
|
|
|
|
callback(0);
|
|
|
|
return;
|
2015-05-27 21:31:36 +02:00
|
|
|
}
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
var stmt;
|
|
|
|
if ( Array.isArray(keys) ) {
|
2015-05-30 17:36:37 +02:00
|
|
|
stmt = db.createAsyncStatement('SELECT "size" AS "size", SUM(LENGTH("value")) FROM "settings" WHERE "name" = :name');
|
2015-05-27 21:31:36 +02:00
|
|
|
bindNames(keys);
|
|
|
|
} else {
|
2015-05-30 17:36:37 +02:00
|
|
|
stmt = db.createAsyncStatement('SELECT "size" AS "size", SUM(LENGTH("value")) FROM "settings"');
|
2014-11-27 20:45:54 +01:00
|
|
|
}
|
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
runStatement(stmt, function(result) {
|
|
|
|
callback(result.size);
|
|
|
|
});
|
|
|
|
};
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
var read = function(details, callback) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( typeof callback !== 'function' ) {
|
2014-11-27 20:45:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-28 20:49:01 +02:00
|
|
|
var prepareResult = function(result) {
|
|
|
|
var key;
|
|
|
|
for ( key in result ) {
|
|
|
|
if ( result.hasOwnProperty(key) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result[key] = JSON.parse(result[key]);
|
|
|
|
}
|
|
|
|
if ( typeof details === 'object' && details !== null ) {
|
|
|
|
for ( key in details ) {
|
|
|
|
if ( result.hasOwnProperty(key) === false ) {
|
|
|
|
result[key] = details[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( open() === null ) {
|
|
|
|
prepareResult({});
|
|
|
|
return;
|
2015-05-27 21:31:36 +02:00
|
|
|
}
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
var names = [];
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( details !== null ) {
|
|
|
|
if ( Array.isArray(details) ) {
|
2015-05-27 21:31:36 +02:00
|
|
|
names = details;
|
2014-12-28 21:26:06 +01:00
|
|
|
} else if ( typeof details === 'object' ) {
|
2015-05-27 21:31:36 +02:00
|
|
|
names = Object.keys(details);
|
2014-12-28 21:26:06 +01:00
|
|
|
} else {
|
2015-05-27 21:31:36 +02:00
|
|
|
names = [details.toString()];
|
2014-11-27 20:45:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
var stmt;
|
|
|
|
if ( names.length === 0 ) {
|
2015-05-30 17:36:37 +02:00
|
|
|
stmt = db.createAsyncStatement('SELECT * FROM "settings"');
|
2015-05-27 21:31:36 +02:00
|
|
|
} else {
|
2015-05-30 17:36:37 +02:00
|
|
|
stmt = db.createAsyncStatement('SELECT * FROM "settings" WHERE "name" = :name');
|
2015-05-27 21:31:36 +02:00
|
|
|
bindNames(stmt, names);
|
|
|
|
}
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-28 20:49:01 +02:00
|
|
|
runStatement(stmt, prepareResult);
|
2015-05-27 21:31:36 +02:00
|
|
|
};
|
2014-11-27 20:45:54 +01:00
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
var remove = function(keys, callback) {
|
2015-05-28 20:49:01 +02:00
|
|
|
if ( open() === null ) {
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
return;
|
2014-11-27 20:45:54 +01:00
|
|
|
}
|
2015-05-30 17:36:37 +02:00
|
|
|
var stmt = db.createAsyncStatement('DELETE FROM "settings" WHERE "name" = :name');
|
2015-05-27 21:31:36 +02:00
|
|
|
bindNames(stmt, typeof keys === 'string' ? [keys] : keys);
|
|
|
|
runStatement(stmt, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
var write = function(details, callback) {
|
2015-05-28 20:49:01 +02:00
|
|
|
if ( open() === null ) {
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
return;
|
2014-11-27 20:45:54 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 17:36:37 +02:00
|
|
|
var stmt = db.createAsyncStatement('INSERT OR REPLACE INTO "settings" ("name", "value") VALUES(:name, :value)');
|
2015-05-27 21:31:36 +02:00
|
|
|
var params = stmt.newBindingParamsArray(), bp;
|
|
|
|
for ( var key in details ) {
|
|
|
|
if ( details.hasOwnProperty(key) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bp = params.newBindingParams();
|
|
|
|
bp.bindByName('name', key);
|
|
|
|
bp.bindByName('value', JSON.stringify(details[key]));
|
|
|
|
params.addParams(bp);
|
2014-11-27 20:45:54 +01:00
|
|
|
}
|
2015-05-27 21:31:36 +02:00
|
|
|
if ( params.length === 0 ) {
|
2014-11-27 20:45:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:31:36 +02:00
|
|
|
stmt.bindParameters(params);
|
|
|
|
runStatement(stmt, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Export API
|
|
|
|
var api = {
|
|
|
|
QUOTA_BYTES: 100 * 1024 * 1024,
|
|
|
|
clear: clear,
|
|
|
|
get: read,
|
|
|
|
getBytesInUse: getBytesInUse,
|
|
|
|
remove: remove,
|
|
|
|
set: write
|
|
|
|
};
|
|
|
|
return api;
|
|
|
|
})();
|
2014-11-27 20:45:54 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
var getTabBrowser = function(win) {
|
|
|
|
return vAPI.fennec && win.BrowserApp || win.gBrowser || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var getOwnerWindow = function(target) {
|
|
|
|
if ( target.ownerDocument ) {
|
|
|
|
return target.ownerDocument.defaultView;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fennec
|
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
for ( var tab of win.BrowserApp.tabs) {
|
|
|
|
if ( tab === target || tab.window === target ) {
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-31 06:35:53 +02:00
|
|
|
vAPI.isBehindTheSceneTabId = function(tabId) {
|
|
|
|
return tabId.toString() === '-1';
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.noTabId = '-1';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-27 11:13:33 +01:00
|
|
|
vAPI.tabs = {};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-02 08:35:25 +01:00
|
|
|
vAPI.tabs.registerListeners = function() {
|
2015-05-31 23:43:19 +02:00
|
|
|
tabWatcher.start();
|
2014-12-02 08:35:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
// Firefox:
|
|
|
|
//
|
|
|
|
// browser --> ownerDocument --> defaultView --> gBrowser --> browsers --+
|
|
|
|
// ^ |
|
|
|
|
// | |
|
|
|
|
// +-------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// browser (browser)
|
|
|
|
// contentTitle
|
|
|
|
// currentURI
|
|
|
|
// ownerDocument (XULDocument)
|
|
|
|
// defaultView (ChromeWindow)
|
2015-06-01 14:11:25 +02:00
|
|
|
// gBrowser (tabbrowser OR browser)
|
2015-05-31 23:43:19 +02:00
|
|
|
// browsers (browser)
|
|
|
|
// selectedBrowser
|
|
|
|
// selectedTab
|
|
|
|
// tabs (tab.tabbrowser-tab)
|
|
|
|
//
|
2015-06-01 14:11:25 +02:00
|
|
|
// Fennec: (what I figured so far)
|
2015-05-31 23:43:19 +02:00
|
|
|
//
|
2015-06-01 14:11:25 +02:00
|
|
|
// tab --> browser windows --> window --> BrowserApp --> tabs --+
|
|
|
|
// ^ window |
|
|
|
|
// | |
|
|
|
|
// +---------------------------------------------------------------+
|
|
|
|
//
|
|
|
|
// tab
|
|
|
|
// browser
|
|
|
|
// [manual search to go back to tab from list of windows]
|
2015-05-31 23:43:19 +02:00
|
|
|
|
2014-12-02 08:35:25 +01:00
|
|
|
vAPI.tabs.get = function(tabId, callback) {
|
2015-05-31 23:43:19 +02:00
|
|
|
var win, browser;
|
2014-12-02 08:35:25 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( tabId === null ) {
|
2015-02-12 19:19:17 +01:00
|
|
|
win = Services.wm.getMostRecentWindow('navigator:browser');
|
2015-05-31 23:43:19 +02:00
|
|
|
browser = tabWatcher.browserFromTarget(getTabBrowser(win).selectedTab);
|
|
|
|
tabId = tabWatcher.tabIdFromTarget(browser);
|
2014-12-28 21:26:06 +01:00
|
|
|
} else {
|
2015-05-31 23:43:19 +02:00
|
|
|
browser = tabWatcher.browserFromTabId(tabId);
|
|
|
|
if ( browser ) {
|
|
|
|
win = getOwnerWindow(browser);
|
2014-12-02 08:35:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-01 21:38:51 +01:00
|
|
|
// For internal use
|
2015-01-08 21:11:43 +01:00
|
|
|
if ( typeof callback !== 'function' ) {
|
2015-05-31 23:43:19 +02:00
|
|
|
return browser;
|
2014-12-16 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
if ( !browser ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:30:05 +01:00
|
|
|
var windows = this.getWindows();
|
2015-02-28 20:18:58 +01:00
|
|
|
var tabBrowser = getTabBrowser(win);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
id: tabId,
|
2015-05-31 23:43:19 +02:00
|
|
|
index: tabWatcher.indexFromTarget(browser),
|
2015-02-28 20:18:58 +01:00
|
|
|
windowId: windows.indexOf(win),
|
2015-05-31 23:43:19 +02:00
|
|
|
active: browser === tabBrowser.selectedBrowser,
|
2015-02-28 20:18:58 +01:00
|
|
|
url: browser.currentURI.asciiSpec,
|
2015-05-31 23:43:19 +02:00
|
|
|
title: browser.contentTitle
|
2015-02-28 20:18:58 +01:00
|
|
|
});
|
2014-12-02 08:35:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.tabs.getAll = function(window) {
|
2015-02-28 20:18:58 +01:00
|
|
|
var win, tab;
|
|
|
|
var tabs = [];
|
2014-12-02 08:35:25 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
for ( win of this.getWindows() ) {
|
|
|
|
if ( window && window !== win ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
var tabBrowser = getTabBrowser(win);
|
|
|
|
if ( tabBrowser === null ) {
|
|
|
|
continue;
|
2015-02-12 19:19:17 +01:00
|
|
|
}
|
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
for ( tab of tabBrowser.tabs ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
tabs.push(tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tabs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.tabs.getWindows = function() {
|
|
|
|
var winumerator = Services.wm.getEnumerator('navigator:browser');
|
|
|
|
var windows = [];
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
while ( winumerator.hasMoreElements() ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
var win = winumerator.getNext();
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !win.closed ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
windows.push(win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return windows;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
vAPI.tabs.open = function(details) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !details.url ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// extension pages
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( /^[\w-]{2,}:/.test(details.url) === false ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
details.url = vAPI.getURL(details.url);
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:00:10 +02:00
|
|
|
var tab;
|
2014-12-02 08:35:25 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( details.select ) {
|
2015-01-15 13:24:35 +01:00
|
|
|
var URI = Services.io.newURI(details.url, null, null);
|
2014-12-02 08:35:25 +01:00
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
for ( tab of this.getAll() ) {
|
2015-05-31 23:43:19 +02:00
|
|
|
var browser = tabWatcher.browserFromTarget(tab);
|
2014-12-02 08:35:25 +01:00
|
|
|
|
2015-01-15 13:24:35 +01:00
|
|
|
// Or simply .equals if we care about the fragment
|
2015-02-28 20:18:58 +01:00
|
|
|
if ( URI.equalsExceptRef(browser.currentURI) === false ) {
|
|
|
|
continue;
|
2014-12-02 08:35:25 +01:00
|
|
|
}
|
2015-02-28 20:18:58 +01:00
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
this.select(tab);
|
2015-02-28 20:18:58 +01:00
|
|
|
return;
|
2014-12-02 08:35:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( details.active === undefined ) {
|
2014-12-02 08:35:25 +01:00
|
|
|
details.active = true;
|
|
|
|
}
|
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
if ( details.tabId ) {
|
2015-05-31 23:43:19 +02:00
|
|
|
tab = tabWatcher.browserFromTabId(details.tabId);
|
2015-03-16 17:30:05 +01:00
|
|
|
if ( tab ) {
|
2015-05-31 23:43:19 +02:00
|
|
|
tabWatcher.browserFromTarget(tab).loadURI(details.url);
|
2015-03-16 17:30:05 +01:00
|
|
|
return;
|
2015-02-12 19:19:17 +01:00
|
|
|
}
|
2015-02-28 20:18:58 +01:00
|
|
|
}
|
2015-02-12 19:19:17 +01:00
|
|
|
|
2015-06-01 15:00:10 +02:00
|
|
|
var win = Services.wm.getMostRecentWindow('navigator:browser');
|
|
|
|
var tabBrowser = getTabBrowser(win);
|
2015-02-12 19:19:17 +01:00
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
if ( vAPI.fennec ) {
|
|
|
|
tabBrowser.addTab(details.url, {selected: details.active !== false});
|
|
|
|
// Note that it's impossible to move tabs on Fennec, so don't bother
|
|
|
|
return;
|
|
|
|
}
|
2015-02-12 19:19:17 +01:00
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
if ( details.index === -1 ) {
|
|
|
|
details.index = tabBrowser.browsers.indexOf(tabBrowser.selectedBrowser) + 1;
|
|
|
|
}
|
2015-02-12 19:19:17 +01:00
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
tab = tabBrowser.loadOneTab(details.url, {inBackground: !details.active});
|
2015-02-12 19:19:17 +01:00
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
if ( details.index !== undefined ) {
|
|
|
|
tabBrowser.moveTabTo(tab, details.index);
|
|
|
|
}
|
|
|
|
};
|
2014-12-02 08:35:25 +01:00
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
/******************************************************************************/
|
2014-12-02 08:35:25 +01:00
|
|
|
|
2015-03-27 18:00:55 +01:00
|
|
|
// Replace the URL of a tab. Noop if the tab does not exist.
|
|
|
|
|
|
|
|
vAPI.tabs.replace = function(tabId, url) {
|
|
|
|
var targetURL = url;
|
|
|
|
|
|
|
|
// extension pages
|
|
|
|
if ( /^[\w-]{2,}:/.test(targetURL) !== true ) {
|
|
|
|
targetURL = vAPI.getURL(targetURL);
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:00:10 +02:00
|
|
|
var browser = tabWatcher.browserFromTabId(tabId);
|
|
|
|
if ( browser ) {
|
|
|
|
browser.loadURI(targetURL);
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
vAPI.tabs._remove = function(tab, tabBrowser) {
|
|
|
|
if ( vAPI.fennec ) {
|
|
|
|
tabBrowser.closeTab(tab);
|
|
|
|
return;
|
2014-12-02 08:35:25 +01:00
|
|
|
}
|
2015-02-28 20:18:58 +01:00
|
|
|
tabBrowser.removeTab(tab);
|
2014-12-02 08:35:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-31 06:35:53 +02:00
|
|
|
vAPI.tabs.remove = function(tabId) {
|
2015-06-01 14:49:41 +02:00
|
|
|
var browser = tabWatcher.browserFromTabId(tabId);
|
|
|
|
if ( !browser ) {
|
2015-03-16 17:30:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-06-01 14:49:41 +02:00
|
|
|
var tab = tabWatcher.tabFromBrowser(browser);
|
|
|
|
if ( !tab ) {
|
|
|
|
return;
|
2014-12-02 08:35:25 +01:00
|
|
|
}
|
2015-06-01 14:49:41 +02:00
|
|
|
this._remove(tab, getTabBrowser(getOwnerWindow(browser)));
|
2014-12-02 08:35:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 21:11:43 +01:00
|
|
|
vAPI.tabs.reload = function(tabId) {
|
|
|
|
var tab = this.get(tabId);
|
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
if ( !tab ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
tabWatcher.browserFromTarget(tab).webNavigation.reload(
|
2015-03-12 15:46:33 +01:00
|
|
|
Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE
|
|
|
|
);
|
2015-01-08 21:11:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
vAPI.tabs.select = function(tab) {
|
|
|
|
tab = typeof tab === 'object' ? tab : this.get(tab);
|
2015-03-02 19:49:34 +01:00
|
|
|
|
|
|
|
if ( !tab ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tabBrowser = getTabBrowser(getOwnerWindow(tab));
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
if ( vAPI.fennec ) {
|
2015-03-02 19:49:34 +01:00
|
|
|
tabBrowser.selectTab(tab);
|
|
|
|
} else {
|
|
|
|
tabBrowser.selectedTab = tab;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
vAPI.tabs.injectScript = function(tabId, details, callback) {
|
2015-02-28 20:18:58 +01:00
|
|
|
var tab = this.get(tabId);
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !tab ) {
|
2014-12-16 22:31:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:31:17 +01:00
|
|
|
if ( typeof details.file !== 'string' ) {
|
|
|
|
return;
|
2014-12-28 10:56:09 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 13:31:17 +01:00
|
|
|
details.file = vAPI.getURL(details.file);
|
2015-05-31 23:43:19 +02:00
|
|
|
tabWatcher.browserFromTarget(tab).messageManager.sendAsyncMessage(
|
2014-12-18 11:59:04 +01:00
|
|
|
location.host + ':broadcast',
|
2014-12-16 22:31:03 +01:00
|
|
|
JSON.stringify({
|
|
|
|
broadcast: true,
|
2014-12-28 21:26:06 +01:00
|
|
|
channelName: 'vAPI',
|
2014-12-16 22:31:03 +01:00
|
|
|
msg: {
|
|
|
|
cmd: 'injectScript',
|
|
|
|
details: details
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( typeof callback === 'function' ) {
|
2015-05-17 19:02:56 +02:00
|
|
|
vAPI.setTimeout(callback, 13);
|
2014-12-16 22:31:03 +01:00
|
|
|
}
|
2014-12-16 13:44:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var tabWatcher = (function() {
|
|
|
|
// TODO: find out whether we need a janitor to take care of stale entries.
|
|
|
|
var browserToTabIdMap = new Map();
|
|
|
|
var tabIdToBrowserMap = new Map();
|
|
|
|
var tabIdGenerator = 1;
|
|
|
|
|
|
|
|
var indexFromBrowser = function(browser) {
|
|
|
|
var win = getOwnerWindow(browser);
|
|
|
|
if ( !win ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
var tabbrowser = getTabBrowser(win);
|
|
|
|
if ( !tabbrowser ) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-06-01 14:11:25 +02:00
|
|
|
// This can happen, for example, the `view-source:` window, there is
|
|
|
|
// no tabbrowser object, the browser object sits directly in the
|
|
|
|
// window.
|
|
|
|
if ( tabbrowser === browser ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-31 23:43:19 +02:00
|
|
|
return vAPI.fennec ?
|
|
|
|
tabbrowser.tabs.indexOf(browser) :
|
|
|
|
tabbrowser.browsers.indexOf(browser);
|
|
|
|
};
|
|
|
|
|
|
|
|
var indexFromTarget = function(target) {
|
|
|
|
return indexFromBrowser(browserFromTarget(target));
|
|
|
|
};
|
|
|
|
|
2015-06-01 14:49:41 +02:00
|
|
|
var tabFromBrowser = function(browser) {
|
|
|
|
var i = indexFromBrowser(browser);
|
|
|
|
if ( i === -1 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var win = getOwnerWindow(browser);
|
|
|
|
if ( !win ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var tabbrowser = getTabBrowser(win);
|
|
|
|
if ( !tabbrowser ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( !tabbrowser.tabs || i >= tabbrowser.tabs.length ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return tabbrowser.tabs[i];
|
|
|
|
};
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var browserFromTarget = function(target) {
|
|
|
|
if ( !target ) {
|
|
|
|
return null;
|
2015-05-31 06:35:53 +02:00
|
|
|
}
|
2015-05-31 23:43:19 +02:00
|
|
|
if ( vAPI.fennec ) {
|
|
|
|
if ( target.browser ) { // target is a tab
|
|
|
|
target = target.browser;
|
|
|
|
}
|
|
|
|
} else if ( target.linkedPanel ) { // target is a tab
|
|
|
|
target = target.linkedBrowser;
|
|
|
|
}
|
|
|
|
if ( target.localName !== 'browser' ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
};
|
2015-05-31 06:35:53 +02:00
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var tabIdFromTarget = function(target) {
|
|
|
|
var browser = browserFromTarget(target);
|
|
|
|
if ( browser === null ) {
|
|
|
|
return vAPI.noTabId;
|
|
|
|
}
|
|
|
|
var tabId = browserToTabIdMap.get(browser);
|
|
|
|
if ( tabId === undefined ) {
|
|
|
|
tabId = 't' + tabIdGenerator++;
|
|
|
|
browserToTabIdMap.set(browser, tabId);
|
|
|
|
tabIdToBrowserMap.set(tabId, browser);
|
|
|
|
}
|
|
|
|
return tabId;
|
|
|
|
};
|
|
|
|
|
|
|
|
var browserFromTabId = function(tabId) {
|
|
|
|
var browser = tabIdToBrowserMap.get(tabId);
|
|
|
|
if ( browser === undefined ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Verify that the browser is still live
|
|
|
|
if ( indexFromBrowser(browser) !== -1 ) {
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
removeBrowserEntry(tabId, browser);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
var removeBrowserEntry = function(tabId, browser) {
|
|
|
|
if ( tabId && tabId !== vAPI.noTabId ) {
|
|
|
|
vAPI.tabs.onClosed(tabId);
|
|
|
|
delete vAPI.toolbarButton.tabs[tabId];
|
|
|
|
tabIdToBrowserMap.delete(tabId);
|
|
|
|
}
|
|
|
|
if ( browser ) {
|
|
|
|
browserToTabIdMap.delete(browser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/Events/TabOpen
|
|
|
|
var onOpen = function({target}) {
|
|
|
|
var tabId = tabIdFromTarget(target);
|
|
|
|
var browser = browserFromTabId(tabId);
|
|
|
|
vAPI.tabs.onNavigation({
|
|
|
|
frameId: 0,
|
|
|
|
tabId: tabId,
|
|
|
|
url: browser.currentURI.asciiSpec,
|
|
|
|
});
|
|
|
|
};
|
2015-05-31 06:35:53 +02:00
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/Events/TabShow
|
|
|
|
var onShow = function({target}) {
|
|
|
|
tabIdFromTarget(target);
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/Events/TabClose
|
|
|
|
var onClose = function({target}) {
|
|
|
|
// target is tab in Firefox, browser in Fennec
|
|
|
|
var browser = browserFromTarget(target);
|
|
|
|
var tabId = browserToTabIdMap.get(browser);
|
|
|
|
removeBrowserEntry(tabId, browser);
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/Events/TabSelect
|
|
|
|
var onSelect = function({target}) {
|
|
|
|
vAPI.setIcon(tabIdFromTarget(target), getOwnerWindow(target));
|
|
|
|
};
|
|
|
|
|
|
|
|
var onWindowLoad = function(ev) {
|
|
|
|
if ( ev ) {
|
|
|
|
this.removeEventListener(ev.type, onWindowLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
var wintype = this.document.documentElement.getAttribute('windowtype');
|
2015-05-31 06:35:53 +02:00
|
|
|
if ( wintype !== 'navigator:browser' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tabBrowser = getTabBrowser(this);
|
|
|
|
if ( !tabBrowser ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var tabContainer;
|
2015-05-31 06:35:53 +02:00
|
|
|
if ( tabBrowser.deck ) {
|
|
|
|
// Fennec
|
|
|
|
tabContainer = tabBrowser.deck;
|
|
|
|
} else if ( tabBrowser.tabContainer ) {
|
|
|
|
// desktop Firefox
|
|
|
|
tabContainer = tabBrowser.tabContainer;
|
|
|
|
vAPI.contextMenu.register(this.document);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-31 23:43:19 +02:00
|
|
|
tabContainer.addEventListener('TabOpen', onOpen);
|
|
|
|
tabContainer.addEventListener('TabShow', onShow);
|
|
|
|
tabContainer.addEventListener('TabClose', onClose);
|
|
|
|
tabContainer.addEventListener('TabSelect', onSelect);
|
2015-05-31 06:35:53 +02:00
|
|
|
|
|
|
|
// when new window is opened TabSelect doesn't run on the selected tab?
|
2015-05-31 23:43:19 +02:00
|
|
|
};
|
2015-05-31 06:35:53 +02:00
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var onWindowUnload = function() {
|
|
|
|
vAPI.contextMenu.unregister(this.document);
|
|
|
|
this.removeEventListener('DOMContentLoaded', onWindowLoad);
|
2015-05-31 06:35:53 +02:00
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var tabBrowser = getTabBrowser(this);
|
|
|
|
if ( !tabBrowser ) {
|
|
|
|
return;
|
2015-05-31 06:35:53 +02:00
|
|
|
}
|
2015-05-31 23:43:19 +02:00
|
|
|
|
|
|
|
var tabContainer = null;
|
|
|
|
if ( tabBrowser.deck ) {
|
|
|
|
// Fennec
|
|
|
|
tabContainer = tabBrowser.deck;
|
|
|
|
} else if ( tabBrowser.tabContainer ) {
|
|
|
|
tabContainer = tabBrowser.tabContainer;
|
2015-05-31 06:35:53 +02:00
|
|
|
}
|
2015-05-31 23:43:19 +02:00
|
|
|
if ( tabContainer ) {
|
|
|
|
tabContainer.removeEventListener('TabOpen', onOpen);
|
|
|
|
tabContainer.removeEventListener('TabShow', onShow);
|
|
|
|
tabContainer.removeEventListener('TabClose', onClose);
|
|
|
|
tabContainer.removeEventListener('TabSelect', onSelect);
|
2015-05-31 06:35:53 +02:00
|
|
|
}
|
2015-05-31 23:43:19 +02:00
|
|
|
|
|
|
|
// Close extension tabs
|
|
|
|
var browser, URI, tabId;
|
|
|
|
for ( var tab of tabBrowser.tabs ) {
|
|
|
|
browser = tabWatcher.browserFromTarget(tab);
|
|
|
|
if ( browser === null ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
URI = browser.currentURI;
|
|
|
|
if ( URI.schemeIs('chrome') && URI.host === location.host ) {
|
|
|
|
vAPI.tabs._remove(tab, getTabBrowser(this));
|
|
|
|
}
|
|
|
|
browser = browserFromTarget(tab);
|
|
|
|
tabId = browserToTabIdMap.get(browser);
|
|
|
|
if ( tabId !== undefined ) {
|
|
|
|
tabIdToBrowserMap.delete(tabId);
|
|
|
|
}
|
|
|
|
browserToTabIdMap.delete(browser);
|
2015-05-31 06:35:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWindowWatcher
|
|
|
|
var windowWatcher = {
|
|
|
|
observe: function(win, topic) {
|
|
|
|
if ( topic === 'domwindowopened' ) {
|
|
|
|
win.addEventListener('DOMContentLoaded', onWindowLoad);
|
2015-05-31 06:35:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize map with existing active tabs
|
2015-05-31 23:43:19 +02:00
|
|
|
var start = function() {
|
|
|
|
var tabBrowser, tab;
|
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
onWindowLoad.call(win);
|
|
|
|
tabBrowser = getTabBrowser(win);
|
|
|
|
if ( tabBrowser === null ) {
|
2015-05-31 06:35:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-31 23:43:19 +02:00
|
|
|
for ( tab of tabBrowser.tabs ) {
|
|
|
|
if ( vAPI.fennec || !tab.hasAttribute('pending') ) {
|
|
|
|
tabIdFromTarget(tab);
|
|
|
|
}
|
|
|
|
}
|
2015-05-31 06:35:53 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
Services.ww.registerNotification(windowWatcher);
|
2015-05-31 06:35:53 +02:00
|
|
|
};
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var stop = function() {
|
|
|
|
Services.ww.unregisterNotification(windowWatcher);
|
2015-05-31 06:35:53 +02:00
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
onWindowUnload.call(win);
|
|
|
|
}
|
2015-05-31 06:35:53 +02:00
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
browserToTabIdMap.clear();
|
|
|
|
tabIdToBrowserMap.clear();
|
2015-05-31 06:35:53 +02:00
|
|
|
};
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
cleanupTasks.push(stop);
|
|
|
|
|
2015-05-31 06:35:53 +02:00
|
|
|
return {
|
2015-05-31 23:43:19 +02:00
|
|
|
start: start,
|
|
|
|
browserFromTarget: browserFromTarget,
|
|
|
|
tabs: function() { return browserToTabIdMap.keys(); },
|
|
|
|
tabIdFromTarget: tabIdFromTarget,
|
|
|
|
browserFromTabId: browserFromTabId,
|
2015-06-01 14:49:41 +02:00
|
|
|
indexFromTarget: indexFromTarget,
|
|
|
|
tabFromBrowser: tabFromBrowser
|
2015-05-31 06:35:53 +02:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-26 21:41:44 +01:00
|
|
|
vAPI.setIcon = function(tabId, iconStatus, badge) {
|
|
|
|
// If badge is undefined, then setIcon was called from the TabSelect event
|
2015-01-11 18:41:52 +01:00
|
|
|
var win = badge === undefined
|
2014-12-26 21:41:44 +01:00
|
|
|
? iconStatus
|
|
|
|
: Services.wm.getMostRecentWindow('navigator:browser');
|
2015-05-31 23:43:19 +02:00
|
|
|
var curTabId = tabWatcher.tabIdFromTarget(getTabBrowser(win).selectedTab);
|
2015-01-11 18:41:52 +01:00
|
|
|
var tb = vAPI.toolbarButton;
|
2014-12-16 13:44:34 +01:00
|
|
|
|
|
|
|
// from 'TabSelect' event
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( tabId === undefined ) {
|
2014-12-16 13:44:34 +01:00
|
|
|
tabId = curTabId;
|
2014-12-28 21:26:06 +01:00
|
|
|
} else if ( badge !== undefined ) {
|
2015-01-27 11:13:33 +01:00
|
|
|
tb.tabs[tabId] = { badge: badge, img: iconStatus === 'on' };
|
2014-12-16 13:44:34 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
if ( tabId === curTabId ) {
|
|
|
|
tb.updateState(win, tabId);
|
2014-12-16 13:44:34 +01:00
|
|
|
}
|
2014-12-07 20:51:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
vAPI.messaging = {
|
2014-12-16 13:44:34 +01:00
|
|
|
get globalMessageManager() {
|
|
|
|
return Cc['@mozilla.org/globalmessagemanager;1']
|
|
|
|
.getService(Ci.nsIMessageListenerManager);
|
|
|
|
},
|
|
|
|
frameScript: vAPI.getURL('frameScript.js'),
|
2014-11-24 20:00:27 +01:00
|
|
|
listeners: {},
|
|
|
|
defaultHandler: null,
|
|
|
|
NOOPFUNC: function(){},
|
|
|
|
UNHANDLED: 'vAPI.messaging.notHandled'
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.messaging.listen = function(listenerName, callback) {
|
|
|
|
this.listeners[listenerName] = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-25 14:53:30 +01:00
|
|
|
vAPI.messaging.onMessage = function({target, data}) {
|
|
|
|
var messageManager = target.messageManager;
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !messageManager ) {
|
2014-12-16 13:44:34 +01:00
|
|
|
// Message came from a popup, and its message manager is not usable.
|
|
|
|
// So instead we broadcast to the parent window.
|
2015-02-28 20:18:58 +01:00
|
|
|
messageManager = getOwnerWindow(
|
|
|
|
target.webNavigation.QueryInterface(Ci.nsIDocShell).chromeEventHandler
|
|
|
|
).messageManager;
|
2014-12-16 13:44:34 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 16:15:35 +01:00
|
|
|
var channelNameRaw = data.channelName;
|
|
|
|
var pos = channelNameRaw.indexOf('|');
|
|
|
|
var channelName = channelNameRaw.slice(pos + 1);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
var callback = vAPI.messaging.NOOPFUNC;
|
2015-02-01 16:15:35 +01:00
|
|
|
if ( data.requestId !== undefined ) {
|
|
|
|
callback = CallbackWrapper.factory(
|
|
|
|
messageManager,
|
|
|
|
channelName,
|
|
|
|
channelNameRaw.slice(0, pos),
|
|
|
|
data.requestId
|
|
|
|
).callback;
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var sender = {
|
|
|
|
tab: {
|
2015-05-31 23:43:19 +02:00
|
|
|
id: tabWatcher.tabIdFromTarget(target)
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specific handler
|
|
|
|
var r = vAPI.messaging.UNHANDLED;
|
2014-12-28 21:26:06 +01:00
|
|
|
var listener = vAPI.messaging.listeners[channelName];
|
2014-11-24 20:00:27 +01:00
|
|
|
if ( typeof listener === 'function' ) {
|
2014-12-25 14:53:30 +01:00
|
|
|
r = listener(data.msg, sender, callback);
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
|
|
|
if ( r !== vAPI.messaging.UNHANDLED ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default handler
|
2014-12-25 14:53:30 +01:00
|
|
|
r = vAPI.messaging.defaultHandler(data.msg, sender, callback);
|
2014-11-24 20:00:27 +01:00
|
|
|
if ( r !== vAPI.messaging.UNHANDLED ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-18 12:47:07 +01:00
|
|
|
console.error('uBlock> messaging > unknown request: %o', data);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
// Unhandled:
|
|
|
|
// Need to callback anyways in case caller expected an answer, or
|
|
|
|
// else there is a memory leak on caller's side
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.messaging.setup = function(defaultHandler) {
|
|
|
|
// Already setup?
|
|
|
|
if ( this.defaultHandler !== null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof defaultHandler !== 'function' ) {
|
|
|
|
defaultHandler = function(){ return vAPI.messaging.UNHANDLED; };
|
|
|
|
}
|
|
|
|
this.defaultHandler = defaultHandler;
|
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
this.globalMessageManager.addMessageListener(
|
2014-12-18 11:59:04 +01:00
|
|
|
location.host + ':background',
|
2014-12-07 20:51:49 +01:00
|
|
|
this.onMessage
|
|
|
|
);
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
this.globalMessageManager.loadFrameScript(this.frameScript, true);
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2015-01-12 17:45:09 +01:00
|
|
|
cleanupTasks.push(function() {
|
2014-12-16 13:44:34 +01:00
|
|
|
var gmm = vAPI.messaging.globalMessageManager;
|
|
|
|
|
|
|
|
gmm.removeDelayedFrameScript(vAPI.messaging.frameScript);
|
|
|
|
gmm.removeMessageListener(
|
2014-12-18 11:59:04 +01:00
|
|
|
location.host + ':background',
|
2014-12-16 13:44:34 +01:00
|
|
|
vAPI.messaging.onMessage
|
|
|
|
);
|
2015-05-31 23:43:19 +02:00
|
|
|
|
|
|
|
vAPI.messaging.defaultHandler = null;
|
2014-12-16 13:44:34 +01:00
|
|
|
});
|
2014-11-24 20:00:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-27 20:45:54 +01:00
|
|
|
vAPI.messaging.broadcast = function(message) {
|
2014-12-16 13:44:34 +01:00
|
|
|
this.globalMessageManager.broadcastAsyncMessage(
|
2014-12-18 11:59:04 +01:00
|
|
|
location.host + ':broadcast',
|
2014-11-27 20:45:54 +01:00
|
|
|
JSON.stringify({broadcast: true, msg: message})
|
|
|
|
);
|
2014-11-24 20:00:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-01 16:15:35 +01:00
|
|
|
// This allows to avoid creating a closure for every single message which
|
|
|
|
// expects an answer. Having a closure created each time a message is processed
|
2015-02-17 22:05:23 +01:00
|
|
|
// has been always bothering me. Another benefit of the implementation here
|
2015-02-01 16:15:35 +01:00
|
|
|
// is to reuse the callback proxy object, so less memory churning.
|
|
|
|
//
|
|
|
|
// https://developers.google.com/speed/articles/optimizing-javascript
|
|
|
|
// "Creating a closure is significantly slower then creating an inner
|
2015-02-17 22:05:23 +01:00
|
|
|
// function without a closure, and much slower than reusing a static
|
2015-02-01 16:15:35 +01:00
|
|
|
// function"
|
|
|
|
//
|
|
|
|
// http://hacksoflife.blogspot.ca/2015/01/the-four-horsemen-of-performance.html
|
2015-02-17 22:05:23 +01:00
|
|
|
// "the dreaded 'uniformly slow code' case where every function takes 1%
|
|
|
|
// of CPU and you have to make one hundred separate performance optimizations
|
2015-02-01 16:15:35 +01:00
|
|
|
// to improve performance at all"
|
2015-02-01 18:25:14 +01:00
|
|
|
//
|
|
|
|
// http://jsperf.com/closure-no-closure/2
|
2015-02-01 16:15:35 +01:00
|
|
|
|
|
|
|
var CallbackWrapper = function(messageManager, channelName, listenerId, requestId) {
|
|
|
|
this.callback = this.proxy.bind(this); // bind once
|
|
|
|
this.init(messageManager, channelName, listenerId, requestId);
|
|
|
|
};
|
|
|
|
|
|
|
|
CallbackWrapper.junkyard = [];
|
|
|
|
|
|
|
|
CallbackWrapper.factory = function(messageManager, channelName, listenerId, requestId) {
|
|
|
|
var wrapper = CallbackWrapper.junkyard.pop();
|
|
|
|
if ( wrapper ) {
|
|
|
|
wrapper.init(messageManager, channelName, listenerId, requestId);
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
return new CallbackWrapper(messageManager, channelName, listenerId, requestId);
|
|
|
|
};
|
|
|
|
|
|
|
|
CallbackWrapper.prototype.init = function(messageManager, channelName, listenerId, requestId) {
|
|
|
|
this.messageManager = messageManager;
|
|
|
|
this.channelName = channelName;
|
|
|
|
this.listenerId = listenerId;
|
|
|
|
this.requestId = requestId;
|
|
|
|
};
|
|
|
|
|
|
|
|
CallbackWrapper.prototype.proxy = function(response) {
|
|
|
|
var message = JSON.stringify({
|
|
|
|
requestId: this.requestId,
|
|
|
|
channelName: this.channelName,
|
|
|
|
msg: response !== undefined ? response : null
|
|
|
|
});
|
|
|
|
|
|
|
|
if ( this.messageManager.sendAsyncMessage ) {
|
|
|
|
this.messageManager.sendAsyncMessage(this.listenerId, message);
|
|
|
|
} else {
|
|
|
|
this.messageManager.broadcastAsyncMessage(this.listenerId, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark for reuse
|
2015-02-17 22:05:23 +01:00
|
|
|
this.messageManager =
|
|
|
|
this.channelName =
|
|
|
|
this.requestId =
|
2015-02-01 16:15:35 +01:00
|
|
|
this.listenerId = null;
|
|
|
|
CallbackWrapper.junkyard.push(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-24 23:11:36 +01:00
|
|
|
var httpObserver = {
|
2015-01-02 18:41:41 +01:00
|
|
|
classDescription: 'net-channel-event-sinks for ' + location.host,
|
|
|
|
classID: Components.ID('{dc8d6319-5f6e-4438-999e-53722db99e84}'),
|
|
|
|
contractID: '@' + location.host + '/net-channel-event-sinks;1',
|
2015-03-02 19:49:34 +01:00
|
|
|
REQDATAKEY: location.host + 'reqdata',
|
2014-12-24 23:11:36 +01:00
|
|
|
ABORT: Components.results.NS_BINDING_ABORTED,
|
2015-01-02 18:41:41 +01:00
|
|
|
ACCEPT: Components.results.NS_SUCCEEDED,
|
2015-05-17 16:32:40 +02:00
|
|
|
// Request types:
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIContentPolicy#Constants
|
2015-01-02 18:41:41 +01:00
|
|
|
MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
|
2015-01-13 17:54:54 +01:00
|
|
|
VALID_CSP_TARGETS: 1 << Ci.nsIContentPolicy.TYPE_DOCUMENT |
|
|
|
|
1 << Ci.nsIContentPolicy.TYPE_SUBDOCUMENT,
|
2015-01-02 18:41:41 +01:00
|
|
|
typeMap: {
|
2015-01-26 20:26:45 +01:00
|
|
|
1: 'other',
|
2015-01-02 18:41:41 +01:00
|
|
|
2: 'script',
|
|
|
|
3: 'image',
|
|
|
|
4: 'stylesheet',
|
|
|
|
5: 'object',
|
|
|
|
6: 'main_frame',
|
|
|
|
7: 'sub_frame',
|
2015-01-21 01:39:13 +01:00
|
|
|
11: 'xmlhttprequest',
|
|
|
|
12: 'object',
|
2015-03-20 13:52:01 +01:00
|
|
|
14: 'font',
|
|
|
|
21: 'image'
|
2015-01-02 18:41:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
get componentRegistrar() {
|
|
|
|
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
},
|
|
|
|
|
|
|
|
get categoryManager() {
|
|
|
|
return Cc['@mozilla.org/categorymanager;1']
|
|
|
|
.getService(Ci.nsICategoryManager);
|
2014-12-24 23:11:36 +01:00
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-01-04 13:58:17 +01:00
|
|
|
QueryInterface: (function() {
|
2015-01-11 18:41:52 +01:00
|
|
|
var {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
|
2015-01-04 13:58:17 +01:00
|
|
|
|
|
|
|
return XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIFactory,
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsIChannelEventSink,
|
|
|
|
Ci.nsISupportsWeakReference
|
|
|
|
]);
|
|
|
|
})(),
|
2015-01-02 18:41:41 +01:00
|
|
|
|
|
|
|
createInstance: function(outer, iid) {
|
|
|
|
if ( outer ) {
|
|
|
|
throw Components.results.NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
register: function() {
|
2015-05-17 16:32:40 +02:00
|
|
|
this.pendingRingBufferInit();
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
Services.obs.addObserver(this, 'http-on-opening-request', true);
|
|
|
|
Services.obs.addObserver(this, 'http-on-examine-response', true);
|
|
|
|
|
2015-03-17 19:08:29 +01:00
|
|
|
// Guard against stale instances not having been unregistered
|
|
|
|
if ( this.componentRegistrar.isCIDRegistered(this.classID) ) {
|
|
|
|
try {
|
2015-03-18 07:04:30 +01:00
|
|
|
this.componentRegistrar.unregisterFactory(this.classID, Components.manager.getClassObject(this.classID, Ci.nsIFactory));
|
2015-03-17 19:08:29 +01:00
|
|
|
} catch (ex) {
|
2015-03-18 12:47:07 +01:00
|
|
|
console.error('uBlock> httpObserver > unable to unregister stale instance: ', ex);
|
2015-03-17 19:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
this.componentRegistrar.registerFactory(
|
|
|
|
this.classID,
|
|
|
|
this.classDescription,
|
|
|
|
this.contractID,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
this.categoryManager.addCategoryEntry(
|
|
|
|
'net-channel-event-sinks',
|
|
|
|
this.contractID,
|
|
|
|
this.contractID,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
2014-12-28 10:56:09 +01:00
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
unregister: function() {
|
2015-01-02 18:41:41 +01:00
|
|
|
Services.obs.removeObserver(this, 'http-on-opening-request');
|
|
|
|
Services.obs.removeObserver(this, 'http-on-examine-response');
|
|
|
|
|
|
|
|
this.componentRegistrar.unregisterFactory(this.classID, this);
|
|
|
|
this.categoryManager.deleteCategoryEntry(
|
|
|
|
'net-channel-event-sinks',
|
|
|
|
this.contractID,
|
|
|
|
false
|
|
|
|
);
|
2014-12-28 10:56:09 +01:00
|
|
|
},
|
2014-12-28 21:26:06 +01:00
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
PendingRequest: function() {
|
|
|
|
this.frameId = 0;
|
|
|
|
this.parentFrameId = 0;
|
|
|
|
this.rawtype = 0;
|
|
|
|
this.sourceTabId = null;
|
|
|
|
this.tabId = 0;
|
|
|
|
this._key = ''; // key is url, from URI.spec
|
|
|
|
},
|
2015-05-28 20:49:01 +02:00
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
// If all work fine, this map should not grow indefinitely. It can have
|
|
|
|
// stale items in it, but these will be taken care of when entries in
|
|
|
|
// the ring buffer are overwritten.
|
|
|
|
pendingURLToIndex: new Map(),
|
|
|
|
pendingWritePointer: 0,
|
|
|
|
pendingRingBuffer: new Array(32),
|
|
|
|
pendingRingBufferInit: function() {
|
|
|
|
// Use and reuse pre-allocated PendingRequest objects = less memory
|
|
|
|
// churning.
|
|
|
|
var i = this.pendingRingBuffer.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
this.pendingRingBuffer[i] = new this.PendingRequest();
|
|
|
|
}
|
|
|
|
},
|
2015-05-28 20:49:01 +02:00
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
createPendingRequest: function(url) {
|
|
|
|
var bucket;
|
|
|
|
var i = this.pendingWritePointer;
|
|
|
|
this.pendingWritePointer = i + 1 & 31;
|
|
|
|
var preq = this.pendingRingBuffer[i];
|
|
|
|
// Cleanup unserviced pending request
|
|
|
|
if ( preq._key !== '' ) {
|
|
|
|
bucket = this.pendingURLToIndex.get(preq._key);
|
|
|
|
if ( Array.isArray(bucket) ) {
|
|
|
|
// Assuming i in array
|
|
|
|
var pos = bucket.indexOf(i);
|
|
|
|
bucket.splice(pos, 1);
|
|
|
|
if ( bucket.length === 1 ) {
|
|
|
|
this.pendingURLToIndex.set(preq._key, bucket[0]);
|
|
|
|
}
|
|
|
|
} else if ( typeof bucket === 'number' ) {
|
|
|
|
// Assuming bucket === i
|
|
|
|
this.pendingURLToIndex.delete(preq._key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Would be much simpler if a url could not appear more than once.
|
|
|
|
bucket = this.pendingURLToIndex.get(url);
|
|
|
|
if ( bucket === undefined ) {
|
|
|
|
this.pendingURLToIndex.set(url, i);
|
|
|
|
} else if ( Array.isArray(bucket) ) {
|
|
|
|
bucket = bucket.push(i);
|
|
|
|
} else {
|
|
|
|
bucket = [bucket, i];
|
|
|
|
}
|
|
|
|
preq._key = url;
|
|
|
|
return preq;
|
|
|
|
},
|
2015-05-28 20:49:01 +02:00
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
lookupPendingRequest: function(url) {
|
|
|
|
var i = this.pendingURLToIndex.get(url);
|
|
|
|
if ( i === undefined ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( Array.isArray(i) ) {
|
|
|
|
var bucket = i;
|
|
|
|
i = bucket.shift();
|
|
|
|
if ( bucket.length === 1 ) {
|
|
|
|
this.pendingURLToIndex.set(url, bucket[0]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.pendingURLToIndex.delete(url);
|
|
|
|
}
|
|
|
|
var preq = this.pendingRingBuffer[i];
|
|
|
|
preq._key = ''; // mark as "serviced"
|
|
|
|
return preq;
|
|
|
|
},
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
handlePopup: function(URI, tabId, sourceTabId) {
|
|
|
|
if ( !sourceTabId ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-15 13:24:35 +01:00
|
|
|
if ( !URI.schemeIs('http') && !URI.schemeIs('https') ) {
|
2015-01-02 18:41:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = vAPI.tabs.onPopup({
|
2015-03-08 16:06:36 +01:00
|
|
|
targetTabId: tabId,
|
|
|
|
openerTabId: sourceTabId,
|
|
|
|
targetURL: URI.asciiSpec
|
2015-01-02 18:41:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return result === true;
|
|
|
|
},
|
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
handleRequest: function(channel, URI, details) {
|
2015-01-02 18:41:41 +01:00
|
|
|
var onBeforeRequest = vAPI.net.onBeforeRequest;
|
2015-05-17 16:32:40 +02:00
|
|
|
var type = this.typeMap[details.rawtype] || 'other';
|
2015-01-02 18:41:41 +01:00
|
|
|
|
2015-05-09 07:16:27 +02:00
|
|
|
if ( onBeforeRequest.types && onBeforeRequest.types.has(type) === false ) {
|
2015-01-02 18:41:41 +01:00
|
|
|
return false;
|
2014-12-24 23:11:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
var result = onBeforeRequest.callback({
|
|
|
|
frameId: details.frameId,
|
2015-01-26 20:26:45 +01:00
|
|
|
hostname: URI.asciiHost,
|
2015-01-21 01:39:13 +01:00
|
|
|
parentFrameId: details.parentFrameId,
|
|
|
|
tabId: details.tabId,
|
|
|
|
type: type,
|
2015-01-26 20:26:45 +01:00
|
|
|
url: URI.asciiSpec
|
2015-01-02 18:41:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if ( !result || typeof result !== 'object' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( result.cancel === true ) {
|
|
|
|
channel.cancel(this.ABORT);
|
|
|
|
return true;
|
2015-01-21 01:39:13 +01:00
|
|
|
}
|
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
return false;
|
|
|
|
},
|
2014-12-24 23:11:36 +01:00
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
observe: function(channel, topic) {
|
2015-03-12 18:48:43 +01:00
|
|
|
if ( channel instanceof Ci.nsIHttpChannel === false ) {
|
2014-12-24 23:11:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-03-10 19:44:31 +01:00
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
var URI = channel.URI;
|
|
|
|
var channelData, result;
|
|
|
|
|
|
|
|
if ( topic === 'http-on-examine-response' ) {
|
|
|
|
if ( !(channel instanceof Ci.nsIWritablePropertyBag) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-24 23:11:36 +01:00
|
|
|
try {
|
2015-03-02 19:49:34 +01:00
|
|
|
channelData = channel.getProperty(this.REQDATAKEY);
|
2014-12-24 23:11:36 +01:00
|
|
|
} catch (ex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-13 17:54:54 +01:00
|
|
|
if ( !channelData ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:36:04 +01:00
|
|
|
if ( (1 << channelData[4] & this.VALID_CSP_TARGETS) === 0 ) {
|
2014-12-24 23:11:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-26 21:41:44 +01:00
|
|
|
topic = 'Content-Security-Policy';
|
2014-12-24 23:11:36 +01:00
|
|
|
|
|
|
|
try {
|
2015-01-02 18:41:41 +01:00
|
|
|
result = channel.getResponseHeader(topic);
|
2014-12-24 23:11:36 +01:00
|
|
|
} catch (ex) {
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = vAPI.net.onHeadersReceived.callback({
|
2015-01-21 01:39:13 +01:00
|
|
|
hostname: URI.asciiHost,
|
2015-03-02 18:36:04 +01:00
|
|
|
parentFrameId: channelData[1],
|
2015-01-21 01:39:13 +01:00
|
|
|
responseHeaders: result ? [{name: topic, value: result}] : [],
|
2015-03-02 18:36:04 +01:00
|
|
|
tabId: channelData[3],
|
2015-05-28 20:49:01 +02:00
|
|
|
type: this.typeMap[channelData[4]] || 'other',
|
2015-01-21 01:39:13 +01:00
|
|
|
url: URI.asciiSpec
|
2014-12-24 23:11:36 +01:00
|
|
|
});
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( result ) {
|
2015-01-02 18:41:41 +01:00
|
|
|
channel.setResponseHeader(
|
2014-12-26 21:41:44 +01:00
|
|
|
topic,
|
|
|
|
result.responseHeaders.pop().value,
|
2014-12-24 23:11:36 +01:00
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// http-on-opening-request
|
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
//console.log('http-on-opening-request:', URI.spec);
|
2014-12-24 23:11:36 +01:00
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
var pendingRequest = this.lookupPendingRequest(URI.spec);
|
2015-03-02 12:52:04 +01:00
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
// Behind-the-scene request
|
|
|
|
if ( pendingRequest === null ) {
|
|
|
|
var rawtype = channel.loadInfo && channel.loadInfo.contentPolicyType || 1;
|
|
|
|
if ( this.handleRequest(channel, URI, { tabId: vAPI.noTabId, rawtype: rawtype }) ) {
|
2015-03-02 12:52:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
// Carry data for behind-the-scene redirects
|
|
|
|
if ( channel instanceof Ci.nsIWritablePropertyBag ) {
|
|
|
|
channel.setProperty( this.REQDATAKEY, [0, -1, null, vAPI.noTabId, rawtype]);
|
2015-03-02 12:52:04 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 23:11:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
if ( this.handleRequest(channel, URI, pendingRequest) ) {
|
2014-12-24 23:11:36 +01:00
|
|
|
return;
|
2015-01-02 18:41:41 +01:00
|
|
|
}
|
2014-12-24 23:11:36 +01:00
|
|
|
|
2015-01-12 17:45:09 +01:00
|
|
|
// If request is not handled we may use the data in on-modify-request
|
2015-01-02 18:41:41 +01:00
|
|
|
if ( channel instanceof Ci.nsIWritablePropertyBag ) {
|
2015-03-02 17:49:25 +01:00
|
|
|
channel.setProperty(this.REQDATAKEY, [
|
2015-05-17 16:32:40 +02:00
|
|
|
pendingRequest.frameId,
|
|
|
|
pendingRequest.parentFrameId,
|
|
|
|
pendingRequest.sourceTabId,
|
|
|
|
pendingRequest.tabId,
|
|
|
|
pendingRequest.rawtype
|
2015-03-02 17:49:25 +01:00
|
|
|
]);
|
2014-12-24 23:11:36 +01:00
|
|
|
}
|
2015-01-02 18:41:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// contentPolicy.shouldLoad doesn't detect redirects, this needs to be used
|
|
|
|
asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback) {
|
|
|
|
var result = this.ACCEPT;
|
|
|
|
|
|
|
|
// If error thrown, the redirect will fail
|
|
|
|
try {
|
2015-01-15 13:24:35 +01:00
|
|
|
var URI = newChannel.URI;
|
2015-01-02 18:41:41 +01:00
|
|
|
|
2015-01-15 13:24:35 +01:00
|
|
|
if ( !URI.schemeIs('http') && !URI.schemeIs('https') ) {
|
2015-01-02 18:41:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !(oldChannel instanceof Ci.nsIWritablePropertyBag) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-02 19:49:34 +01:00
|
|
|
var channelData = oldChannel.getProperty(this.REQDATAKEY);
|
2015-01-02 18:41:41 +01:00
|
|
|
|
2015-03-02 18:36:04 +01:00
|
|
|
if ( this.handlePopup(URI, channelData[3], channelData[2]) ) {
|
2015-01-02 18:41:41 +01:00
|
|
|
result = this.ABORT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var details = {
|
2015-03-02 18:36:04 +01:00
|
|
|
frameId: channelData[0],
|
|
|
|
parentFrameId: channelData[1],
|
|
|
|
tabId: channelData[3],
|
2015-05-17 16:32:40 +02:00
|
|
|
rawtype: channelData[4]
|
2015-01-02 18:41:41 +01:00
|
|
|
};
|
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( this.handleRequest(newChannel, URI, details) ) {
|
2015-01-02 18:41:41 +01:00
|
|
|
result = this.ABORT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:45:09 +01:00
|
|
|
// Carry the data on in case of multiple redirects
|
2015-01-02 18:41:41 +01:00
|
|
|
if ( newChannel instanceof Ci.nsIWritablePropertyBag ) {
|
2015-03-02 19:49:34 +01:00
|
|
|
newChannel.setProperty(this.REQDATAKEY, channelData);
|
2015-01-02 18:41:41 +01:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
// console.error(ex);
|
|
|
|
} finally {
|
|
|
|
callback.onRedirectVerifyCallback(result);
|
|
|
|
}
|
2014-12-24 23:11:36 +01:00
|
|
|
}
|
2014-12-02 08:35:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
vAPI.net = {};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
vAPI.net.registerListeners = function() {
|
2015-01-11 18:41:52 +01:00
|
|
|
// Since it's not used
|
|
|
|
this.onBeforeSendHeaders = null;
|
|
|
|
|
2015-05-09 07:16:27 +02:00
|
|
|
this.onBeforeRequest.types = this.onBeforeRequest.types ?
|
|
|
|
new Set(this.onBeforeRequest.types) :
|
|
|
|
null;
|
2014-12-07 20:51:49 +01:00
|
|
|
|
2014-12-24 23:11:36 +01:00
|
|
|
var shouldLoadListenerMessageName = location.host + ':shouldLoad';
|
|
|
|
var shouldLoadListener = function(e) {
|
2015-05-17 16:32:40 +02:00
|
|
|
// Non blocking: it is assumed that the http observer is fired after
|
|
|
|
// shouldLoad recorded the pending requests. If this is not the case,
|
|
|
|
// a request would end up being categorized as a behind-the-scene
|
|
|
|
// requests.
|
2015-01-02 18:41:41 +01:00
|
|
|
var details = e.data;
|
2015-05-31 23:43:19 +02:00
|
|
|
var tabId = tabWatcher.tabIdFromTarget(e.target);
|
2015-03-02 18:36:04 +01:00
|
|
|
var sourceTabId = null;
|
|
|
|
|
|
|
|
// Popup candidate
|
|
|
|
if ( details.openerURL ) {
|
2015-05-31 23:43:19 +02:00
|
|
|
for ( var tab of tabWatcher.tabs() ) {
|
|
|
|
var URI = tab.currentURI;
|
2015-03-02 18:36:04 +01:00
|
|
|
|
|
|
|
// Probably isn't the best method to identify the source tab
|
|
|
|
if ( URI.spec !== details.openerURL ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
sourceTabId = tabWatcher.tabIdFromTarget(tab);
|
2015-03-02 18:36:04 +01:00
|
|
|
|
|
|
|
if ( sourceTabId === tabId ) {
|
|
|
|
sourceTabId = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
URI = Services.io.newURI(details.url, null, null);
|
|
|
|
|
|
|
|
if ( httpObserver.handlePopup(URI, tabId, sourceTabId) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-17 16:32:40 +02:00
|
|
|
//console.log('shouldLoadListener:', details.url);
|
|
|
|
|
|
|
|
var pendingReq = httpObserver.createPendingRequest(details.url);
|
|
|
|
pendingReq.frameId = details.frameId;
|
|
|
|
pendingReq.parentFrameId = details.parentFrameId;
|
|
|
|
pendingReq.rawtype = details.rawtype;
|
|
|
|
pendingReq.sourceTabId = sourceTabId;
|
|
|
|
pendingReq.tabId = tabId;
|
2014-12-09 21:56:17 +01:00
|
|
|
};
|
2014-12-07 20:51:49 +01:00
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
vAPI.messaging.globalMessageManager.addMessageListener(
|
2014-12-24 23:11:36 +01:00
|
|
|
shouldLoadListenerMessageName,
|
|
|
|
shouldLoadListener
|
2014-12-09 21:56:17 +01:00
|
|
|
);
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2015-03-26 22:00:56 +01:00
|
|
|
var locationChangedListenerMessageName = location.host + ':locationChanged';
|
|
|
|
var locationChangedListener = function(e) {
|
|
|
|
var details = e.data;
|
|
|
|
var browser = e.target;
|
2015-05-31 23:43:19 +02:00
|
|
|
var tabId = tabWatcher.tabIdFromTarget(browser);
|
2015-05-16 22:31:44 +02:00
|
|
|
|
2015-05-28 20:49:01 +02:00
|
|
|
// Ignore notifications related to our popup
|
|
|
|
if ( details.url.lastIndexOf(vAPI.getURL('popup.html'), 0) === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:00:56 +01:00
|
|
|
//console.debug("nsIWebProgressListener: onLocationChange: " + details.url + " (" + details.flags + ")");
|
|
|
|
|
|
|
|
// LOCATION_CHANGE_SAME_DOCUMENT = "did not load a new document"
|
|
|
|
if ( details.flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT ) {
|
|
|
|
vAPI.tabs.onUpdated(tabId, {url: details.url}, {
|
|
|
|
frameId: 0,
|
|
|
|
tabId: tabId,
|
|
|
|
url: browser.currentURI.asciiSpec
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/105
|
2015-03-26 22:00:56 +01:00
|
|
|
// Allow any kind of pages
|
|
|
|
vAPI.tabs.onNavigation({
|
|
|
|
frameId: 0,
|
|
|
|
tabId: tabId,
|
|
|
|
url: details.url,
|
|
|
|
});
|
2015-03-31 13:03:35 +02:00
|
|
|
};
|
2015-03-26 22:00:56 +01:00
|
|
|
|
|
|
|
vAPI.messaging.globalMessageManager.addMessageListener(
|
|
|
|
locationChangedListenerMessageName,
|
|
|
|
locationChangedListener
|
|
|
|
);
|
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
httpObserver.register();
|
2014-12-24 23:11:36 +01:00
|
|
|
|
2015-01-12 17:45:09 +01:00
|
|
|
cleanupTasks.push(function() {
|
2014-12-16 13:44:34 +01:00
|
|
|
vAPI.messaging.globalMessageManager.removeMessageListener(
|
2014-12-24 23:11:36 +01:00
|
|
|
shouldLoadListenerMessageName,
|
|
|
|
shouldLoadListener
|
2014-12-16 13:44:34 +01:00
|
|
|
);
|
2014-12-24 23:11:36 +01:00
|
|
|
|
2015-03-26 22:00:56 +01:00
|
|
|
vAPI.messaging.globalMessageManager.removeMessageListener(
|
|
|
|
locationChangedListenerMessageName,
|
|
|
|
locationChangedListener
|
|
|
|
);
|
|
|
|
|
2014-12-28 10:56:09 +01:00
|
|
|
httpObserver.unregister();
|
2014-12-16 13:44:34 +01:00
|
|
|
});
|
2014-12-09 21:56:17 +01:00
|
|
|
};
|
2014-12-07 20:51:49 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
vAPI.toolbarButton = {
|
|
|
|
id: location.host + '-button',
|
|
|
|
type: 'view',
|
|
|
|
viewId: location.host + '-panel',
|
|
|
|
label: vAPI.app.name,
|
|
|
|
tooltiptext: vAPI.app.name,
|
|
|
|
tabs: {/*tabId: {badge: 0, img: boolean}*/}
|
|
|
|
};
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Toolbar button UI for desktop Firefox
|
|
|
|
vAPI.toolbarButton.init = function() {
|
|
|
|
if ( vAPI.fennec ) {
|
|
|
|
// Menu UI for Fennec
|
|
|
|
var tb = {
|
|
|
|
menuItemIds: new WeakMap(),
|
|
|
|
label: vAPI.app.name,
|
|
|
|
tabs: {}
|
|
|
|
};
|
|
|
|
vAPI.toolbarButton = tb;
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
tb.getMenuItemLabel = function(tabId) {
|
|
|
|
var label = this.label;
|
|
|
|
if ( tabId === undefined ) {
|
|
|
|
return label;
|
|
|
|
}
|
2015-03-02 19:49:34 +01:00
|
|
|
var tabDetails = this.tabs[tabId];
|
2015-03-12 18:48:43 +01:00
|
|
|
if ( !tabDetails ) {
|
|
|
|
return label;
|
2015-03-02 19:49:34 +01:00
|
|
|
}
|
2015-03-12 18:48:43 +01:00
|
|
|
if ( !tabDetails.img ) {
|
|
|
|
label += ' (' + vAPI.i18n('fennecMenuItemBlockingOff') + ')';
|
|
|
|
} else if ( tabDetails.badge ) {
|
|
|
|
label += ' (' + tabDetails.badge + ')';
|
|
|
|
}
|
|
|
|
return label;
|
|
|
|
};
|
|
|
|
|
|
|
|
tb.onClick = function() {
|
|
|
|
var win = Services.wm.getMostRecentWindow('navigator:browser');
|
2015-05-31 23:43:19 +02:00
|
|
|
var curTabId = tabWatcher.tabIdFromTarget(getTabBrowser(win).selectedTab);
|
2015-03-12 18:48:43 +01:00
|
|
|
vAPI.tabs.open({
|
|
|
|
url: 'popup.html?tabId=' + curTabId,
|
|
|
|
index: -1,
|
|
|
|
select: true
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
tb.updateState = function(win, tabId) {
|
|
|
|
var id = this.menuItemIds.get(win);
|
|
|
|
if ( !id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
win.NativeWindow.menu.update(id, {
|
|
|
|
name: this.getMenuItemLabel(tabId)
|
|
|
|
});
|
|
|
|
};
|
2015-01-11 18:41:52 +01:00
|
|
|
|
2015-03-02 19:49:34 +01:00
|
|
|
// Only actually expecting one window under Fennec (note, not tabs, windows)
|
2015-03-12 18:48:43 +01:00
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
var label = tb.getMenuItemLabel();
|
|
|
|
var id = win.NativeWindow.menu.add({
|
|
|
|
name: label,
|
|
|
|
callback: tb.onClick
|
|
|
|
});
|
|
|
|
tb.menuItemIds.set(win, id);
|
2015-03-02 19:49:34 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
cleanupTasks.push(function() {
|
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
var id = tb.menuItemIds.get(win);
|
|
|
|
if ( id ) {
|
|
|
|
win.NativeWindow.menu.remove(id);
|
|
|
|
tb.menuItemIds.delete(win);
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 19:49:34 +01:00
|
|
|
});
|
2015-02-28 20:18:58 +01:00
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-02-28 11:58:09 +01:00
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
var CustomizableUI;
|
2015-01-16 11:42:34 +01:00
|
|
|
try {
|
2015-02-28 20:18:58 +01:00
|
|
|
CustomizableUI = Cu.import('resource:///modules/CustomizableUI.jsm', null).CustomizableUI;
|
2015-01-16 11:42:34 +01:00
|
|
|
} catch (ex) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
this.defaultArea = CustomizableUI.AREA_NAVBAR;
|
|
|
|
this.styleURI = [
|
|
|
|
'#' + this.id + ' {',
|
|
|
|
'list-style-image: url(',
|
2015-03-15 13:17:38 +01:00
|
|
|
vAPI.getURL('img/browsericons/icon16-off.svg'),
|
2015-01-11 18:41:52 +01:00
|
|
|
');',
|
|
|
|
'}',
|
|
|
|
'#' + this.viewId + ', #' + this.viewId + ' > iframe {',
|
|
|
|
'width: 160px;',
|
|
|
|
'height: 290px;',
|
|
|
|
'overflow: hidden !important;',
|
|
|
|
'}'
|
|
|
|
];
|
|
|
|
|
|
|
|
var platformVersion = Services.appinfo.platformVersion;
|
|
|
|
|
|
|
|
if ( Services.vc.compare(platformVersion, '36.0') < 0 ) {
|
|
|
|
this.styleURI.push(
|
|
|
|
'#' + this.id + '[badge]:not([badge=""])::after {',
|
|
|
|
'position: absolute;',
|
|
|
|
'margin-left: -16px;',
|
|
|
|
'margin-top: 3px;',
|
|
|
|
'padding: 1px 2px;',
|
|
|
|
'font-size: 9px;',
|
|
|
|
'font-weight: bold;',
|
|
|
|
'color: #fff;',
|
|
|
|
'background: #666;',
|
|
|
|
'content: attr(badge);',
|
2015-03-02 19:49:34 +01:00
|
|
|
'}'
|
2015-01-11 18:41:52 +01:00
|
|
|
);
|
2015-03-10 19:44:31 +01:00
|
|
|
} else {
|
2015-02-28 11:58:09 +01:00
|
|
|
this.CUIEvents = {};
|
2015-03-12 20:38:21 +01:00
|
|
|
var updateBadge = function() {
|
2015-02-28 11:58:09 +01:00
|
|
|
var wId = vAPI.toolbarButton.id;
|
|
|
|
var buttonInPanel = CustomizableUI.getWidget(wId).areaType === CustomizableUI.TYPE_MENU_PANEL;
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-02-28 11:58:09 +01:00
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
var button = win.document.getElementById(wId);
|
2015-03-02 19:49:34 +01:00
|
|
|
if ( buttonInPanel ) {
|
2015-02-28 11:58:09 +01:00
|
|
|
button.classList.remove('badged-button');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( button === null ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
button.classList.add('badged-button');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( buttonInPanel ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anonymous elements need some time to be reachable
|
2015-05-17 19:02:56 +02:00
|
|
|
vAPI.setTimeout(this.updateBadgeStyle, 250);
|
2015-03-12 20:38:21 +01:00
|
|
|
}.bind(this.CUIEvents);
|
2015-04-24 16:55:11 +02:00
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/CustomizableUI.jsm#Listeners
|
2015-03-12 20:38:21 +01:00
|
|
|
this.CUIEvents.onCustomizeEnd = updateBadge;
|
2015-04-24 16:55:11 +02:00
|
|
|
this.CUIEvents.onWidgetAdded = updateBadge;
|
2015-03-12 20:38:21 +01:00
|
|
|
this.CUIEvents.onWidgetUnderflow = updateBadge;
|
2015-03-08 07:36:30 +01:00
|
|
|
|
2015-02-28 11:58:09 +01:00
|
|
|
this.CUIEvents.updateBadgeStyle = function() {
|
|
|
|
var css = [
|
|
|
|
'background: #666',
|
|
|
|
'color: #fff'
|
|
|
|
].join(';');
|
2015-01-11 18:41:52 +01:00
|
|
|
|
2015-02-28 11:58:09 +01:00
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
var button = win.document.getElementById(vAPI.toolbarButton.id);
|
|
|
|
if ( button === null ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var badge = button.ownerDocument.getAnonymousElementByAttribute(
|
|
|
|
button,
|
|
|
|
'class',
|
|
|
|
'toolbarbutton-badge'
|
|
|
|
);
|
|
|
|
if ( !badge ) {
|
|
|
|
return;
|
2015-03-02 19:49:34 +01:00
|
|
|
}
|
2015-01-11 18:41:52 +01:00
|
|
|
|
2015-02-28 11:58:09 +01:00
|
|
|
badge.style.cssText = css;
|
|
|
|
}
|
|
|
|
};
|
2015-01-11 18:41:52 +01:00
|
|
|
|
2015-02-28 11:58:09 +01:00
|
|
|
this.onCreated = function(button) {
|
|
|
|
button.setAttribute('badge', '');
|
2015-05-17 19:02:56 +02:00
|
|
|
vAPI.setTimeout(updateBadge, 250);
|
2015-02-28 11:58:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
CustomizableUI.addListener(this.CUIEvents);
|
2015-01-11 18:41:52 +01:00
|
|
|
}
|
2015-02-28 11:58:09 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
this.styleURI = Services.io.newURI(
|
|
|
|
'data:text/css,' + encodeURIComponent(this.styleURI.join('')),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
this.closePopup = function({target}) {
|
|
|
|
CustomizableUI.hidePanelForNode(
|
|
|
|
target.ownerDocument.getElementById(vAPI.toolbarButton.viewId)
|
2015-03-02 19:49:34 +01:00
|
|
|
);
|
2015-01-11 18:41:52 +01:00
|
|
|
};
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
CustomizableUI.createWidget(this);
|
|
|
|
vAPI.messaging.globalMessageManager.addMessageListener(
|
|
|
|
location.host + ':closePopup',
|
|
|
|
this.closePopup
|
|
|
|
);
|
|
|
|
|
2015-01-12 17:45:09 +01:00
|
|
|
cleanupTasks.push(function() {
|
2015-02-28 11:58:09 +01:00
|
|
|
if ( this.CUIEvents ) {
|
|
|
|
CustomizableUI.removeListener(this.CUIEvents);
|
|
|
|
}
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
CustomizableUI.destroyWidget(this.id);
|
|
|
|
vAPI.messaging.globalMessageManager.removeMessageListener(
|
|
|
|
location.host + ':closePopup',
|
|
|
|
this.closePopup
|
|
|
|
);
|
|
|
|
|
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
var panel = win.document.getElementById(this.viewId);
|
|
|
|
panel.parentNode.removeChild(panel);
|
|
|
|
win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.removeSheet(this.styleURI, 1);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2015-03-12 18:48:43 +01:00
|
|
|
|
|
|
|
this.init = null;
|
2015-01-11 18:41:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.toolbarButton.onBeforeCreated = function(doc) {
|
|
|
|
var panel = doc.createElement('panelview');
|
|
|
|
panel.setAttribute('id', this.viewId);
|
|
|
|
|
|
|
|
var iframe = doc.createElement('iframe');
|
|
|
|
iframe.setAttribute('type', 'content');
|
|
|
|
|
|
|
|
doc.getElementById('PanelUI-multiView')
|
|
|
|
.appendChild(panel)
|
|
|
|
.appendChild(iframe);
|
|
|
|
|
|
|
|
var updateTimer = null;
|
|
|
|
var delayedResize = function() {
|
|
|
|
if ( updateTimer ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-17 19:02:56 +02:00
|
|
|
updateTimer = vAPI.setTimeout(resizePopup, 10);
|
2015-01-11 18:41:52 +01:00
|
|
|
};
|
|
|
|
var resizePopup = function() {
|
2015-02-12 15:24:45 +01:00
|
|
|
updateTimer = null;
|
2015-01-11 18:41:52 +01:00
|
|
|
var body = iframe.contentDocument.body;
|
|
|
|
panel.parentNode.style.maxWidth = 'none';
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/730
|
2015-02-12 15:24:45 +01:00
|
|
|
// Voodoo programming: this recipe works
|
|
|
|
panel.style.height = iframe.style.height = body.clientHeight.toString() + 'px';
|
|
|
|
panel.style.width = iframe.style.width = body.clientWidth.toString() + 'px';
|
|
|
|
if ( iframe.clientHeight !== body.clientHeight || iframe.clientWidth !== body.clientWidth ) {
|
2015-03-02 19:49:34 +01:00
|
|
|
delayedResize();
|
2015-02-12 15:24:45 +01:00
|
|
|
}
|
2015-01-11 18:41:52 +01:00
|
|
|
};
|
|
|
|
var onPopupReady = function() {
|
|
|
|
var win = this.contentWindow;
|
|
|
|
|
|
|
|
if ( !win || win.location.host !== location.host ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-24 16:55:11 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/83
|
|
|
|
// Add `portrait` class if width is constrained.
|
|
|
|
try {
|
|
|
|
var CustomizableUI = Cu.import('resource:///modules/CustomizableUI.jsm', null).CustomizableUI;
|
|
|
|
iframe.contentDocument.body.classList.toggle(
|
|
|
|
'portrait',
|
|
|
|
CustomizableUI.getWidget(vAPI.toolbarButton.id).areaType === CustomizableUI.TYPE_MENU_PANEL
|
|
|
|
);
|
|
|
|
} catch (ex) {
|
|
|
|
/* noop */
|
|
|
|
}
|
|
|
|
|
2015-01-13 17:20:16 +01:00
|
|
|
new win.MutationObserver(delayedResize).observe(win.document.body, {
|
2015-01-11 18:41:52 +01:00
|
|
|
attributes: true,
|
|
|
|
characterData: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
|
|
|
|
delayedResize();
|
2015-03-02 19:49:34 +01:00
|
|
|
};
|
2015-01-11 18:41:52 +01:00
|
|
|
|
|
|
|
iframe.addEventListener('load', onPopupReady, true);
|
|
|
|
|
|
|
|
doc.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.loadSheet(this.styleURI, 1);
|
|
|
|
};
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
/******************************************************************************/
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
vAPI.toolbarButton.onViewShowing = function({target}) {
|
|
|
|
target.firstChild.setAttribute('src', vAPI.getURL('popup.html'));
|
|
|
|
};
|
2015-03-02 19:49:34 +01:00
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.toolbarButton.onViewHiding = function({target}) {
|
|
|
|
target.parentNode.style.maxWidth = '';
|
|
|
|
target.firstChild.setAttribute('src', 'about:blank');
|
|
|
|
};
|
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-10 19:44:31 +01:00
|
|
|
vAPI.toolbarButton.updateState = function(win, tabId) {
|
|
|
|
var button = win.document.getElementById(this.id);
|
|
|
|
|
|
|
|
if ( !button ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var icon = this.tabs[tabId];
|
|
|
|
button.setAttribute('badge', icon && icon.badge || '');
|
|
|
|
|
|
|
|
if ( !icon || !icon.img ) {
|
|
|
|
icon = '';
|
|
|
|
}
|
|
|
|
else {
|
2015-03-15 13:17:38 +01:00
|
|
|
icon = 'url(' + vAPI.getURL('img/browsericons/icon16.svg') + ')';
|
2015-03-10 19:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
button.style.listStyleImage = icon;
|
|
|
|
};
|
|
|
|
|
2015-01-11 18:41:52 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.toolbarButton.init();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-19 21:24:30 +01:00
|
|
|
vAPI.contextMenu = {
|
|
|
|
contextMap: {
|
|
|
|
frame: 'inFrame',
|
|
|
|
link: 'onLink',
|
|
|
|
image: 'onImage',
|
|
|
|
audio: 'onAudio',
|
|
|
|
video: 'onVideo',
|
|
|
|
editable: 'onEditableArea'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-28 20:18:58 +01:00
|
|
|
vAPI.contextMenu.displayMenuItem = function({target}) {
|
|
|
|
var doc = target.ownerDocument;
|
2014-12-19 21:24:30 +01:00
|
|
|
var gContextMenu = doc.defaultView.gContextMenu;
|
2015-01-11 18:41:52 +01:00
|
|
|
|
|
|
|
if ( !gContextMenu.browser ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:24:30 +01:00
|
|
|
var menuitem = doc.getElementById(vAPI.contextMenu.menuItemId);
|
2015-01-15 13:24:35 +01:00
|
|
|
var currentURI = gContextMenu.browser.currentURI;
|
2014-12-19 21:24:30 +01:00
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/105
|
2015-01-21 17:13:32 +01:00
|
|
|
// TODO: Should the element picker works on any kind of pages?
|
2015-01-15 13:24:35 +01:00
|
|
|
if ( !currentURI.schemeIs('http') && !currentURI.schemeIs('https') ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
menuitem.hidden = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx = vAPI.contextMenu.contexts;
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !ctx ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
menuitem.hidden = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctxMap = vAPI.contextMenu.contextMap;
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
for ( var context of ctx ) {
|
|
|
|
if ( context === 'page' && !gContextMenu.onLink && !gContextMenu.onImage
|
2014-12-19 21:24:30 +01:00
|
|
|
&& !gContextMenu.onEditableArea && !gContextMenu.inFrame
|
2014-12-28 21:26:06 +01:00
|
|
|
&& !gContextMenu.onVideo && !gContextMenu.onAudio ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
menuitem.hidden = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( gContextMenu[ctxMap[context]] ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
menuitem.hidden = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
menuitem.hidden = true;
|
|
|
|
};
|
2014-12-16 13:44:34 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-19 21:24:30 +01:00
|
|
|
vAPI.contextMenu.register = function(doc) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !this.menuItemId ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:19:17 +01:00
|
|
|
if ( vAPI.fennec ) {
|
2015-02-28 20:18:58 +01:00
|
|
|
// TODO https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/NativeWindow/contextmenus/add
|
|
|
|
/*var nativeWindow = doc.defaultView.NativeWindow;
|
2015-02-12 19:19:17 +01:00
|
|
|
contextId = nativeWindow.contextmenus.add(
|
2015-02-28 20:18:58 +01:00
|
|
|
this.menuLabel,
|
|
|
|
nativeWindow.contextmenus.linkOpenableContext,
|
|
|
|
this.onCommand
|
|
|
|
);*/
|
|
|
|
return;
|
2015-02-12 19:19:17 +01:00
|
|
|
}
|
2015-02-28 20:18:58 +01:00
|
|
|
|
|
|
|
var contextMenu = doc.getElementById('contentAreaContextMenu');
|
|
|
|
var menuitem = doc.createElement('menuitem');
|
|
|
|
menuitem.setAttribute('id', this.menuItemId);
|
|
|
|
menuitem.setAttribute('label', this.menuLabel);
|
2015-03-15 13:17:38 +01:00
|
|
|
menuitem.setAttribute('image', vAPI.getURL('img/browsericons/icon16.svg'));
|
2015-02-28 20:18:58 +01:00
|
|
|
menuitem.setAttribute('class', 'menuitem-iconic');
|
|
|
|
menuitem.addEventListener('command', this.onCommand);
|
|
|
|
contextMenu.addEventListener('popupshowing', this.displayMenuItem);
|
|
|
|
contextMenu.insertBefore(menuitem, doc.getElementById('inspect-separator'));
|
2014-12-19 21:24:30 +01:00
|
|
|
};
|
2014-12-16 13:44:34 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-19 21:24:30 +01:00
|
|
|
vAPI.contextMenu.unregister = function(doc) {
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !this.menuItemId ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:19:17 +01:00
|
|
|
if ( vAPI.fennec ) {
|
|
|
|
// TODO
|
2015-02-28 20:18:58 +01:00
|
|
|
return;
|
2015-02-12 19:19:17 +01:00
|
|
|
}
|
2015-02-28 20:18:58 +01:00
|
|
|
|
|
|
|
var menuitem = doc.getElementById(this.menuItemId);
|
|
|
|
var contextMenu = menuitem.parentNode;
|
|
|
|
menuitem.removeEventListener('command', this.onCommand);
|
|
|
|
contextMenu.removeEventListener('popupshowing', this.displayMenuItem);
|
|
|
|
contextMenu.removeChild(menuitem);
|
2014-12-19 21:24:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.contextMenu.create = function(details, callback) {
|
|
|
|
this.menuItemId = details.id;
|
|
|
|
this.menuLabel = details.title;
|
|
|
|
this.contexts = details.contexts;
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( Array.isArray(this.contexts) && this.contexts.length ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
this.contexts = this.contexts.indexOf('all') === -1 ? this.contexts : null;
|
2014-12-28 21:26:06 +01:00
|
|
|
} else {
|
2014-12-19 21:24:30 +01:00
|
|
|
// default in Chrome
|
|
|
|
this.contexts = ['page'];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onCommand = function() {
|
2015-02-28 20:18:58 +01:00
|
|
|
var gContextMenu = getOwnerWindow(this).gContextMenu;
|
2014-12-19 21:24:30 +01:00
|
|
|
var details = {
|
2015-01-16 09:01:40 +01:00
|
|
|
menuItemId: this.id
|
2014-12-19 21:24:30 +01:00
|
|
|
};
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( gContextMenu.inFrame ) {
|
2015-01-16 09:01:40 +01:00
|
|
|
details.tagName = 'iframe';
|
2015-03-01 20:21:05 +01:00
|
|
|
// Probably won't work with e10s
|
2015-06-01 14:11:25 +02:00
|
|
|
details.frameUrl = gContextMenu.focusedWindow && gContextMenu.focusedWindow.location.href || '';
|
2015-01-16 09:01:40 +01:00
|
|
|
} else if ( gContextMenu.onImage ) {
|
|
|
|
details.tagName = 'img';
|
|
|
|
details.srcUrl = gContextMenu.mediaURL;
|
|
|
|
} else if ( gContextMenu.onAudio ) {
|
|
|
|
details.tagName = 'audio';
|
|
|
|
details.srcUrl = gContextMenu.mediaURL;
|
|
|
|
} else if ( gContextMenu.onVideo ) {
|
|
|
|
details.tagName = 'video';
|
2014-12-19 21:24:30 +01:00
|
|
|
details.srcUrl = gContextMenu.mediaURL;
|
2014-12-28 21:26:06 +01:00
|
|
|
} else if ( gContextMenu.onLink ) {
|
2015-01-16 09:01:40 +01:00
|
|
|
details.tagName = 'a';
|
2014-12-19 21:24:30 +01:00
|
|
|
details.linkUrl = gContextMenu.linkURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(details, {
|
2015-05-31 23:43:19 +02:00
|
|
|
id: tabWatcher.tabIdFromTarget(gContextMenu.browser),
|
2015-01-14 23:45:55 +01:00
|
|
|
url: gContextMenu.browser.currentURI.asciiSpec
|
2014-12-19 21:24:30 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
2014-12-19 21:24:30 +01:00
|
|
|
this.register(win.document);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-03-21 00:17:07 +01:00
|
|
|
|
|
|
|
vAPI.contextMenu.remove = function() {
|
|
|
|
for ( var win of vAPI.tabs.getWindows() ) {
|
|
|
|
this.unregister(win.document);
|
|
|
|
}
|
2015-03-21 13:22:34 +01:00
|
|
|
|
|
|
|
this.menuItemId = null;
|
|
|
|
this.menuLabel = null;
|
|
|
|
this.contexts = null;
|
|
|
|
this.onCommand = null;
|
2015-03-21 00:17:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-12-19 21:24:30 +01:00
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
var optionsObserver = {
|
2015-04-09 13:15:14 +02:00
|
|
|
addonId: 'uBlock0@raymondhill.net',
|
2015-03-15 17:49:36 +01:00
|
|
|
|
2015-03-12 18:48:43 +01:00
|
|
|
register: function() {
|
|
|
|
Services.obs.addObserver(this, 'addon-options-displayed', false);
|
|
|
|
cleanupTasks.push(this.unregister.bind(this));
|
2015-03-15 17:49:36 +01:00
|
|
|
|
2015-05-31 23:43:19 +02:00
|
|
|
var browser = tabWatcher.browserFromTarget(vAPI.tabs.get(null));
|
2015-03-17 19:08:29 +01:00
|
|
|
if ( browser && browser.currentURI && browser.currentURI.spec === 'about:addons' ) {
|
|
|
|
this.observe(browser.contentDocument, 'addon-enabled', this.addonId);
|
2015-03-15 17:49:36 +01:00
|
|
|
}
|
2015-03-12 18:48:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
unregister: function() {
|
|
|
|
Services.obs.removeObserver(this, 'addon-options-displayed');
|
|
|
|
},
|
|
|
|
|
|
|
|
setupOptionsButton: function(doc, id, page) {
|
|
|
|
var button = doc.getElementById(id);
|
2015-03-18 07:04:30 +01:00
|
|
|
if ( button === null ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-12 18:48:43 +01:00
|
|
|
button.addEventListener('command', function() {
|
|
|
|
vAPI.tabs.open({ url: page, index: -1 });
|
|
|
|
});
|
|
|
|
button.label = vAPI.i18n(id);
|
|
|
|
},
|
|
|
|
|
2015-03-15 17:49:36 +01:00
|
|
|
observe: function(doc, topic, addonId) {
|
|
|
|
if ( addonId !== this.addonId ) {
|
2015-03-12 18:48:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setupOptionsButton(doc, 'showDashboardButton', 'dashboard.html');
|
2015-05-09 00:28:01 +02:00
|
|
|
this.setupOptionsButton(doc, 'showNetworkLogButton', 'logger-ui.html');
|
2015-03-12 18:48:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
optionsObserver.register();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-09 21:56:17 +01:00
|
|
|
vAPI.lastError = function() {
|
|
|
|
return null;
|
2014-12-07 20:51:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-20 11:08:33 +01: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.
|
|
|
|
|
2015-03-12 18:20:48 +01:00
|
|
|
vAPI.onLoadAllCompleted = function() {
|
|
|
|
var µb = µBlock;
|
2015-05-31 23:43:19 +02:00
|
|
|
var tabId;
|
|
|
|
for ( var tab of tabWatcher.tabs() ) {
|
|
|
|
tabId = tabWatcher.tabIdFromTarget(tab);
|
|
|
|
µb.tabContextManager.commit(tabId, tab.currentURI.asciiSpec);
|
2015-04-29 16:29:23 +02:00
|
|
|
µb.bindTabToPageStats(tabId);
|
2015-05-31 23:43:19 +02:00
|
|
|
tab.messageManager.sendAsyncMessage(
|
2015-03-12 18:20:48 +01:00
|
|
|
location.host + '-load-completed'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2014-12-20 11:08:33 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-15 01:43:10 +01:00
|
|
|
// Likelihood is that we do not have to punycode: given punycode overhead,
|
|
|
|
// it's faster to check and skip than do it unconditionally all the time.
|
|
|
|
|
|
|
|
var punycodeHostname = punycode.toASCII;
|
|
|
|
var isNotASCII = /[^\x21-\x7F]/;
|
|
|
|
|
|
|
|
vAPI.punycodeHostname = function(hostname) {
|
|
|
|
return isNotASCII.test(hostname) ? punycodeHostname(hostname) : hostname;
|
|
|
|
};
|
|
|
|
|
2015-01-14 23:45:55 +01:00
|
|
|
vAPI.punycodeURL = function(url) {
|
2015-01-15 13:24:35 +01:00
|
|
|
if ( isNotASCII.test(url) ) {
|
|
|
|
return Services.io.newURI(url, null, null).asciiSpec;
|
2015-01-15 01:43:10 +01:00
|
|
|
}
|
2015-01-15 13:24:35 +01:00
|
|
|
return url;
|
2015-01-14 23:45:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|