mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-03 01:29:39 +02:00
Use a nsIWebProgressListener instead of a tabsProgressListener for location change monitoring
This commit is contained in:
parent
2504f8ad35
commit
6123335c51
@ -23,10 +23,12 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
this.EXPORTED_SYMBOLS = ['contentObserver'];
|
this.EXPORTED_SYMBOLS = ['contentObserver', 'LocationChangeListener'];
|
||||||
|
|
||||||
const {interfaces: Ci, utils: Cu} = Components;
|
const {interfaces: Ci, utils: Cu} = Components;
|
||||||
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
|
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
|
||||||
|
const {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
|
||||||
|
|
||||||
const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
|
const hostName = Services.io.newURI(Components.stack.filename, null, null).host;
|
||||||
|
|
||||||
// Cu.import('resource://gre/modules/devtools/Console.jsm');
|
// Cu.import('resource://gre/modules/devtools/Console.jsm');
|
||||||
@ -65,16 +67,12 @@ const contentObserver = {
|
|||||||
.getService(Ci.nsICategoryManager);
|
.getService(Ci.nsICategoryManager);
|
||||||
},
|
},
|
||||||
|
|
||||||
QueryInterface: (function() {
|
QueryInterface: XPCOMUtils.generateQI([
|
||||||
let {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null);
|
|
||||||
|
|
||||||
return XPCOMUtils.generateQI([
|
|
||||||
Ci.nsIFactory,
|
Ci.nsIFactory,
|
||||||
Ci.nsIObserver,
|
Ci.nsIObserver,
|
||||||
Ci.nsIContentPolicy,
|
Ci.nsIContentPolicy,
|
||||||
Ci.nsISupportsWeakReference
|
Ci.nsISupportsWeakReference
|
||||||
]);
|
]),
|
||||||
})(),
|
|
||||||
|
|
||||||
createInstance: function(outer, iid) {
|
createInstance: function(outer, iid) {
|
||||||
if ( outer ) {
|
if ( outer ) {
|
||||||
@ -309,6 +307,36 @@ const contentObserver = {
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
const locationChangedMessageName = hostName + ':locationChanged';
|
||||||
|
|
||||||
|
const LocationChangeListener = function(docShell) {
|
||||||
|
if (docShell) {
|
||||||
|
docShell.QueryInterface(Ci.nsIInterfaceRequestor);
|
||||||
|
|
||||||
|
this.docShell = docShell.getInterface(Ci.nsIWebProgress);
|
||||||
|
this.messageManager = docShell.getInterface(Ci.nsIContentFrameMessageManager);
|
||||||
|
|
||||||
|
if (this.messageManager && typeof this.messageManager.sendAsyncMessage === 'function') {
|
||||||
|
this.docShell.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_ALL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LocationChangeListener.prototype.QueryInterface = XPCOMUtils.generateQI(["nsIWebProgressListener", "nsISupportsWeakReference"]);
|
||||||
|
|
||||||
|
LocationChangeListener.prototype.onLocationChange = function(webProgress, request, location, flags) {
|
||||||
|
if ( !webProgress.isTopLevel ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.messageManager.sendAsyncMessage(locationChangedMessageName, {
|
||||||
|
url: location.asciiSpec,
|
||||||
|
flags: flags,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
contentObserver.register();
|
contentObserver.register();
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -21,13 +21,15 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
var locationChangeListener; // Keep alive while frameScript is alive
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
let {contentObserver} = Components.utils.import(
|
let {contentObserver, LocationChangeListener} = Components.utils.import(
|
||||||
Components.stack.filename.replace('Script', 'Module'),
|
Components.stack.filename.replace('Script', 'Module'),
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
@ -54,6 +56,8 @@ let onLoadCompleted = function() {
|
|||||||
|
|
||||||
addMessageListener('ublock-load-completed', onLoadCompleted);
|
addMessageListener('ublock-load-completed', onLoadCompleted);
|
||||||
|
|
||||||
|
locationChangeListener = new LocationChangeListener(docShell);
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -298,7 +298,6 @@ var windowWatcher = {
|
|||||||
} else if ( tabBrowser.tabContainer ) {
|
} else if ( tabBrowser.tabContainer ) {
|
||||||
// desktop Firefox
|
// desktop Firefox
|
||||||
tabContainer = tabBrowser.tabContainer;
|
tabContainer = tabBrowser.tabContainer;
|
||||||
tabBrowser.addTabsProgressListener(tabWatcher);
|
|
||||||
vAPI.contextMenu.register(this.document);
|
vAPI.contextMenu.register(this.document);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
@ -320,8 +319,6 @@ var windowWatcher = {
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var tabWatcher = {
|
var tabWatcher = {
|
||||||
SAME_DOCUMENT: Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT,
|
|
||||||
|
|
||||||
onTabClose: function({target}) {
|
onTabClose: function({target}) {
|
||||||
// target is tab in Firefox, browser in Fennec
|
// target is tab in Firefox, browser in Fennec
|
||||||
var tabId = vAPI.tabs.getTabId(target);
|
var tabId = vAPI.tabs.getTabId(target);
|
||||||
@ -349,32 +346,6 @@ var tabWatcher = {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onLocationChange: function(browser, webProgress, request, location, flags) {
|
|
||||||
if ( !webProgress.isTopLevel ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tabId = vAPI.tabs.getTabId(browser);
|
|
||||||
|
|
||||||
// LOCATION_CHANGE_SAME_DOCUMENT = "did not load a new document"
|
|
||||||
if ( flags & this.SAME_DOCUMENT ) {
|
|
||||||
vAPI.tabs.onUpdated(tabId, {url: location.asciiSpec}, {
|
|
||||||
frameId: 0,
|
|
||||||
tabId: tabId,
|
|
||||||
url: browser.currentURI.asciiSpec
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/105
|
|
||||||
// Allow any kind of pages
|
|
||||||
vAPI.tabs.onNavigation({
|
|
||||||
frameId: 0,
|
|
||||||
tabId: tabId,
|
|
||||||
url: location.asciiSpec
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@ -426,7 +397,6 @@ vAPI.tabs = {};
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
vAPI.tabs.registerListeners = function() {
|
vAPI.tabs.registerListeners = function() {
|
||||||
// onNavigation and onUpdated handled with tabWatcher.onLocationChange
|
|
||||||
// onClosed - handled in tabWatcher.onTabClose
|
// onClosed - handled in tabWatcher.onTabClose
|
||||||
// onPopup - handled in httpObserver.handlePopup
|
// onPopup - handled in httpObserver.handlePopup
|
||||||
|
|
||||||
@ -1232,14 +1202,6 @@ var httpObserver = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( vAPI.fennec && lastRequest.type === this.MAIN_FRAME ) {
|
|
||||||
vAPI.tabs.onNavigation({
|
|
||||||
frameId: 0,
|
|
||||||
tabId: lastRequest.tabId,
|
|
||||||
url: URI.asciiSpec
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// If request is not handled we may use the data in on-modify-request
|
// If request is not handled we may use the data in on-modify-request
|
||||||
if ( channel instanceof Ci.nsIWritablePropertyBag ) {
|
if ( channel instanceof Ci.nsIWritablePropertyBag ) {
|
||||||
channel.setProperty(this.REQDATAKEY, [
|
channel.setProperty(this.REQDATAKEY, [
|
||||||
@ -1361,6 +1323,38 @@ vAPI.net.registerListeners = function() {
|
|||||||
shouldLoadListener
|
shouldLoadListener
|
||||||
);
|
);
|
||||||
|
|
||||||
|
var locationChangedListenerMessageName = location.host + ':locationChanged';
|
||||||
|
var locationChangedListener = function(e) {
|
||||||
|
var details = e.data;
|
||||||
|
var browser = e.target;
|
||||||
|
var tabId = vAPI.tabs.getTabId(browser);
|
||||||
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/gorhill/uBlock/issues/105
|
||||||
|
// Allow any kind of pages
|
||||||
|
vAPI.tabs.onNavigation({
|
||||||
|
frameId: 0,
|
||||||
|
tabId: tabId,
|
||||||
|
url: details.url,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
vAPI.messaging.globalMessageManager.addMessageListener(
|
||||||
|
locationChangedListenerMessageName,
|
||||||
|
locationChangedListener
|
||||||
|
);
|
||||||
|
|
||||||
httpObserver.register();
|
httpObserver.register();
|
||||||
|
|
||||||
cleanupTasks.push(function() {
|
cleanupTasks.push(function() {
|
||||||
@ -1369,6 +1363,11 @@ vAPI.net.registerListeners = function() {
|
|||||||
shouldLoadListener
|
shouldLoadListener
|
||||||
);
|
);
|
||||||
|
|
||||||
|
vAPI.messaging.globalMessageManager.removeMessageListener(
|
||||||
|
locationChangedListenerMessageName,
|
||||||
|
locationChangedListener
|
||||||
|
);
|
||||||
|
|
||||||
httpObserver.unregister();
|
httpObserver.unregister();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user