Browse Source

Firefox: add basic blocking + some fixes

pull/2/head
Deathamns 10 years ago
committed by gorhill
parent
commit
e724d1a9e2
  1. 156
      platform/firefox/vapi-background.js

156
platform/firefox/vapi-background.js

@ -19,7 +19,7 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* global Services */
/* global Services, XPCOMUtils */
// For background page // For background page
@ -34,6 +34,7 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components; const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu['import']('resource://gre/modules/Services.jsm'); Cu['import']('resource://gre/modules/Services.jsm');
Cu['import']('resource://gre/modules/XPCOMUtils.jsm');
/******************************************************************************/ /******************************************************************************/
@ -45,11 +46,18 @@ vAPI.firefox = true;
vAPI.app = { vAPI.app = {
name: 'µBlock', name: 'µBlock',
cleanName: 'ublock',
version: '0.7.2.0' version: '0.7.2.0'
}; };
/******************************************************************************/ /******************************************************************************/
vAPI.app.restart = function() {
};
/******************************************************************************/
var SQLite = { var SQLite = {
open: function() { open: function() {
var path = Services.dirsvc.get('ProfD', Ci.nsIFile); var path = Services.dirsvc.get('ProfD', Ci.nsIFile);
@ -63,7 +71,7 @@ var SQLite = {
throw Error('Should be a directory...'); throw Error('Should be a directory...');
} }
path.append('uBlock.sqlite');
path.append(vAPI.app.cleanName + '.sqlite');
this.db = Services.storage.openDatabase(path); this.db = Services.storage.openDatabase(path);
this.db.executeSimpleSQL( this.db.executeSimpleSQL(
'CREATE TABLE IF NOT EXISTS settings' + 'CREATE TABLE IF NOT EXISTS settings' +
@ -229,7 +237,6 @@ var windowWatcher = {
vAPI.tabs.onClosed(vAPI.tabs.getTabId(e.target)); vAPI.tabs.onClosed(vAPI.tabs.getTabId(e.target));
}, },
onTabSelect: function(e) { onTabSelect: function(e) {
console.log(vAPI.tabs.getTabId(e.target));
// vAPI.setIcon(); // vAPI.setIcon();
}, },
onLoad: function(e) { onLoad: function(e) {
@ -243,7 +250,7 @@ var windowWatcher = {
return; return;
} }
if (!this.gBrowser || this.gBrowser.tabContainer) {
if (!this.gBrowser || !this.gBrowser.tabContainer) {
return; return;
} }
@ -513,9 +520,15 @@ vAPI.tabs.close = function(tabIds) {
/******************************************************************************/ /******************************************************************************/
vAPI.setIcon = function() {
};
/******************************************************************************/
vAPI.messaging = { vAPI.messaging = {
gmm: Cc['@mozilla.org/globalmessagemanager;1'].getService(Ci.nsIMessageListenerManager), gmm: Cc['@mozilla.org/globalmessagemanager;1'].getService(Ci.nsIMessageListenerManager),
frameScript: 'chrome://ublock/content/frameScript.js',
frameScript: 'chrome://' + vAPI.app.cleanName + '/content/frameScript.js',
listeners: {}, listeners: {},
defaultHandler: null, defaultHandler: null,
NOOPFUNC: function(){}, NOOPFUNC: function(){},
@ -598,14 +611,17 @@ vAPI.messaging.setup = function(defaultHandler) {
} }
this.defaultHandler = defaultHandler; this.defaultHandler = defaultHandler;
this.gmm.addMessageListener(vAPI.app.name + ':background', this.onMessage);
this.gmm.addMessageListener(
vAPI.app.cleanName + ':background',
this.onMessage
);
}; };
/******************************************************************************/ /******************************************************************************/
vAPI.messaging.broadcast = function(message) { vAPI.messaging.broadcast = function(message) {
this.gmm.broadcastAsyncMessage( this.gmm.broadcastAsyncMessage(
vAPI.app.name + ':broadcast',
vAPI.app.cleanName + ':broadcast',
JSON.stringify({broadcast: true, msg: message}) JSON.stringify({broadcast: true, msg: message})
); );
}; };
@ -614,7 +630,7 @@ vAPI.messaging.broadcast = function(message) {
vAPI.messaging.unload = function() { vAPI.messaging.unload = function() {
this.gmm.removeMessageListener( this.gmm.removeMessageListener(
vAPI.app.name + ':background',
vAPI.app.cleanName + ':background',
this.onMessage this.onMessage
); );
this.gmm.removeDelayedFrameScript(this.frameScript); this.gmm.removeDelayedFrameScript(this.frameScript);
@ -628,15 +644,137 @@ vAPI.lastError = function() {
/******************************************************************************/ /******************************************************************************/
var conentPolicy = {
classDescription: vAPI.app.name + ' ContentPolicy',
classID: Components.ID('{e6d173c8-8dbf-4189-a6fd-189e8acffd27}'),
contractID: '@' + vAPI.app.cleanName + '/content-policy;1',
ACCEPT: Ci.nsIContentPolicy.ACCEPT,
REJECT: Ci.nsIContentPolicy.REJECT_REQUEST,
types: {
7: 'sub_frame',
4: 'stylesheet',
2: 'script',
3: 'image',
5: 'object',
11: 'xmlhttprequest'
},
get registrar() {
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
},
get catManager() {
return Cc['@mozilla.org/categorymanager;1']
.getService(Ci.nsICategoryManager);
},
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIFactory,
Ci.nsIContentPolicy,
Ci.nsISupportsWeakReference
]),
createInstance: function(outer, iid) {
if (outer) {
throw Components.results.NS_ERROR_NO_AGGREGATION;
}
return this.QueryInterface(iid);
},
shouldLoad: function(type, location, origin, context) {
if (type === 6 || !context || !/^https?$/.test(location.scheme)) {
return this.ACCEPT;
}
var win = (context.ownerDocument || context).defaultView;
if (!win) {
return this.ACCEPT;
}
var block = vAPI.net.onBeforeRequest;
type = this.types[type] || 'other';
if (block.types.indexOf(type) === -1) {
return this.ACCEPT;
}
var browser = win.top.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell)
.chromeEventHandler;
if (!browser) {
return this.ACCEPT;
}
block = block.callback({
url: location.spec,
type: type,
tabId: vAPI.tabs.getTabId(browser),
frameId: win === win.top ? 0 : 1,
parentFrameId: win === win.top ? -1 : 0
});
if (block && typeof block === 'object') {
if (block.cancel === true) {
return this.REJECT;
}
else if (block.redirectURL) {
location.spec = block.redirectURL;
return this.REJECT;
}
}
return this.ACCEPT;
},/*
shouldProcess: function() {
return this.ACCEPT;
}*/
};
/******************************************************************************/
vAPI.net = {};
/******************************************************************************/
vAPI.net.registerListeners = function() {
conentPolicy.registrar.registerFactory(
conentPolicy.classID,
conentPolicy.classDescription,
conentPolicy.contractID,
conentPolicy
);
conentPolicy.catManager.addCategoryEntry(
'content-policy',
conentPolicy.contractID,
conentPolicy.contractID,
false,
true
);
};
/******************************************************************************/
vAPI.net.unregisterListeners = function() {
conentPolicy.registrar.unregisterFactory(conentPolicy.classID, conentPolicy);
conentPolicy.catManager.deleteCategoryEntry(
'content-policy',
conentPolicy.contractID,
false
);
};
/******************************************************************************/
// clean up when the extension is disabled // clean up when the extension is disabled
window.addEventListener('unload', function() { window.addEventListener('unload', function() {
SQLite.close(); SQLite.close();
windowWatcher.unregister(); windowWatcher.unregister();
vAPI.messaging.unload(); vAPI.messaging.unload();
vAPI.net.unregisterListeners();
// close extension tabs // close extension tabs
var extURI, win, tab, host = 'ublock';
var extURI, win, tab, host = vAPI.app.cleanName;
for (win of vAPI.tabs.getWindows()) { for (win of vAPI.tabs.getWindows()) {
for (tab of win.gBrowser.tabs) { for (tab of win.gBrowser.tabs) {

Loading…
Cancel
Save