Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
this should fix gorhill#284
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Jul 17, 2015
1 parent d4f2bf2 commit 1181efc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
39 changes: 38 additions & 1 deletion platform/firefox/frameModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
Home: https://github.com/gorhill/uMatrix
*/

/* global Components */

'use strict';

/******************************************************************************/

this.EXPORTED_SYMBOLS = ['contentObserver'];
this.EXPORTED_SYMBOLS = ['contentObserver', 'LocationChangeListener'];

const {interfaces: Ci, utils: Cu} = Components;
const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
Expand Down Expand Up @@ -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();

/******************************************************************************/
13 changes: 12 additions & 1 deletion platform/firefox/frameScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

/******************************************************************************/

var locationChangeListener; // Keep alive while frameScript is alive

(function() {

'use strict';

/******************************************************************************/

let {contentObserver} = Components.utils.import(
let {contentObserver, LocationChangeListener} = Components.utils.import(
Components.stack.filename.replace('Script', 'Module'),
null
);
Expand All @@ -54,6 +56,15 @@ let onLoadCompleted = function() {

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);
}
}

/******************************************************************************/

})();
Expand Down
60 changes: 53 additions & 7 deletions platform/firefox/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,45 @@ var tabWatcher = (function() {
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) {
if ( ev ) {
this.removeEventListener(ev.type, onWindowLoad);
Expand All @@ -899,19 +938,16 @@ var tabWatcher = (function() {
if ( wintype !== 'navigator:browser' ) {
return;
}

var tabBrowser = getTabBrowser(this);
if ( !tabBrowser ) {
return;
}

var tabContainer;
if ( tabBrowser.tabContainer ) {
tabContainer = tabBrowser.tabContainer;
vAPI.contextMenu.register(this.document);
} else {
var tabContainer = tabBrowser.tabContainer;
if ( !tabContainer ) {
return;
}

vAPI.contextMenu.register(this.document);
tabContainer.addEventListener('TabOpen', onOpen);
tabContainer.addEventListener('TabShow', onShow);
tabContainer.addEventListener('TabClose', onClose);
Expand Down Expand Up @@ -991,10 +1027,20 @@ var tabWatcher = (function() {
}
}

vAPI.messaging.globalMessageManager.addMessageListener(
locationChangedMessageName,
onLocationChanged
);

Services.ww.registerNotification(windowWatcher);
};

var stop = function() {
vAPI.messaging.globalMessageManager.removeMessageListener(
locationChangedMessageName,
onLocationChanged
);

Services.ww.unregisterNotification(windowWatcher);

for ( var win of vAPI.tabs.getWindows() ) {
Expand Down

0 comments on commit 1181efc

Please sign in to comment.