this should fix #284

pull/2/head
gorhill 9 years ago
parent d333eb96ea
commit e088e9cf70

@ -19,11 +19,13 @@
Home: https://github.com/gorhill/uMatrix Home: https://github.com/gorhill/uMatrix
*/ */
/* global Components */
'use strict'; 'use strict';
/******************************************************************************/ /******************************************************************************/
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);
@ -236,6 +238,41 @@ const contentObserver = {
/******************************************************************************/ /******************************************************************************/
const locationChangedMessageName = hostName + ':locationChanged';
const LocationChangeListener = function(docShell) {
if ( !docShell ) {
return;
}
var requestor = docShell.QueryInterface(Ci.nsIInterfaceRequestor);
var ds = requestor.getInterface(Ci.nsIWebProgress);
var mm = requestor.getInterface(Ci.nsIContentFrameMessageManager);
if ( ds && mm && typeof mm.sendAsyncMessage === 'function' ) {
this.docShell = ds;
this.messageManager = mm;
ds.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);
}
};
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,15 @@ let onLoadCompleted = function() {
addMessageListener('umatrix-load-completed', onLoadCompleted); addMessageListener('umatrix-load-completed', onLoadCompleted);
if ( docShell ) {
let Ci = Components.interfaces;
let wp = docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebProgress);
let dw = wp.DOMWindow;
if ( dw === dw.top ) {
locationChangeListener = new LocationChangeListener(docShell);
}
}
/******************************************************************************/ /******************************************************************************/
})(); })();

@ -890,6 +890,45 @@ var tabWatcher = (function() {
vAPI.setIcon(tabIdFromTarget(target), getOwnerWindow(target)); vAPI.setIcon(tabIdFromTarget(target), getOwnerWindow(target));
}; };
var locationChangedMessageName = location.host + ':locationChanged';
var onLocationChanged = function(e) {
var vapi = vAPI;
var details = e.data;
// Ignore notifications related to our popup
if ( details.url.lastIndexOf(vapi.getURL('popup.html'), 0) === 0 ) {
return;
}
var browser = e.target;
var tabId = tabIdFromTarget(browser);
if ( tabId === vapi.noTabId ) {
return;
}
//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/chrisaljoudi/uBlock/issues/105
// Allow any kind of pages
vapi.tabs.onNavigation({
frameId: 0,
tabId: tabId,
url: details.url,
});
};
var onWindowLoad = function(ev) { var onWindowLoad = function(ev) {
if ( ev ) { if ( ev ) {
this.removeEventListener(ev.type, onWindowLoad); this.removeEventListener(ev.type, onWindowLoad);
@ -899,19 +938,16 @@ var tabWatcher = (function() {
if ( wintype !== 'navigator:browser' ) { if ( wintype !== 'navigator:browser' ) {
return; return;
} }
var tabBrowser = getTabBrowser(this); var tabBrowser = getTabBrowser(this);
if ( !tabBrowser ) { if ( !tabBrowser ) {
return; return;
} }
var tabContainer = tabBrowser.tabContainer;
var tabContainer; if ( !tabContainer ) {
if ( tabBrowser.tabContainer ) {
tabContainer = tabBrowser.tabContainer;
vAPI.contextMenu.register(this.document);
} else {
return; return;
} }
vAPI.contextMenu.register(this.document);
tabContainer.addEventListener('TabOpen', onOpen); tabContainer.addEventListener('TabOpen', onOpen);
tabContainer.addEventListener('TabShow', onShow); tabContainer.addEventListener('TabShow', onShow);
tabContainer.addEventListener('TabClose', onClose); tabContainer.addEventListener('TabClose', onClose);
@ -991,10 +1027,20 @@ var tabWatcher = (function() {
} }
} }
vAPI.messaging.globalMessageManager.addMessageListener(
locationChangedMessageName,
onLocationChanged
);
Services.ww.registerNotification(windowWatcher); Services.ww.registerNotification(windowWatcher);
}; };
var stop = function() { var stop = function() {
vAPI.messaging.globalMessageManager.removeMessageListener(
locationChangedMessageName,
onLocationChanged
);
Services.ww.unregisterNotification(windowWatcher); Services.ww.unregisterNotification(windowWatcher);
for ( var win of vAPI.tabs.getWindows() ) { for ( var win of vAPI.tabs.getWindows() ) {

Loading…
Cancel
Save