mirror of
https://github.com/gorhill/uMatrix.git
synced 2024-09-30 09:06:56 +13:00
firefox: draft work for fixing #165
This commit is contained in:
parent
7d3ff66560
commit
2cd6961ed4
2 changed files with 49 additions and 98 deletions
|
@ -55,7 +55,6 @@ const contentObserver = {
|
||||||
MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
|
MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
|
||||||
contentBaseURI: 'chrome://' + hostName + '/content/js/',
|
contentBaseURI: 'chrome://' + hostName + '/content/js/',
|
||||||
cpMessageName: hostName + ':shouldLoad',
|
cpMessageName: hostName + ':shouldLoad',
|
||||||
ignoredPopups: new WeakMap(),
|
|
||||||
uniqueSandboxId: 1,
|
uniqueSandboxId: 1,
|
||||||
|
|
||||||
get componentRegistrar() {
|
get componentRegistrar() {
|
||||||
|
@ -120,6 +119,7 @@ const contentObserver = {
|
||||||
|
|
||||||
// https://bugzil.la/612921
|
// https://bugzil.la/612921
|
||||||
shouldLoad: function(type, location, origin, context) {
|
shouldLoad: function(type, location, origin, context) {
|
||||||
|
/*
|
||||||
if ( !context ) {
|
if ( !context ) {
|
||||||
return this.ACCEPT;
|
return this.ACCEPT;
|
||||||
}
|
}
|
||||||
|
@ -128,15 +128,8 @@ const contentObserver = {
|
||||||
return this.ACCEPT;
|
return this.ACCEPT;
|
||||||
}
|
}
|
||||||
|
|
||||||
let openerURL = null;
|
|
||||||
|
|
||||||
if ( type === this.MAIN_FRAME ) {
|
if ( type === this.MAIN_FRAME ) {
|
||||||
context = context.contentWindow || context;
|
context = context.contentWindow || context;
|
||||||
|
|
||||||
if ( context.opener && context.opener !== context
|
|
||||||
&& this.ignoredPopups.has(context) === false ) {
|
|
||||||
openerURL = context.opener.location.href;
|
|
||||||
}
|
|
||||||
} else if ( type === 7 ) { // SUB_DOCUMENT
|
} else if ( type === 7 ) { // SUB_DOCUMENT
|
||||||
context = context.contentWindow;
|
context = context.contentWindow;
|
||||||
} else {
|
} else {
|
||||||
|
@ -163,7 +156,6 @@ const contentObserver = {
|
||||||
let messageManager = getMessageManager(context);
|
let messageManager = getMessageManager(context);
|
||||||
let details = {
|
let details = {
|
||||||
frameId: isTopLevel ? 0 : this.getFrameId(context),
|
frameId: isTopLevel ? 0 : this.getFrameId(context),
|
||||||
openerURL: openerURL,
|
|
||||||
parentFrameId: parentFrameId,
|
parentFrameId: parentFrameId,
|
||||||
type: type,
|
type: type,
|
||||||
url: location.spec
|
url: location.spec
|
||||||
|
@ -176,7 +168,7 @@ const contentObserver = {
|
||||||
// Compatibility for older versions
|
// Compatibility for older versions
|
||||||
messageManager.sendSyncMessage(this.cpMessageName, details);
|
messageManager.sendSyncMessage(this.cpMessageName, details);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
return this.ACCEPT;
|
return this.ACCEPT;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -247,17 +239,6 @@ const contentObserver = {
|
||||||
return sandbox;
|
return sandbox;
|
||||||
},
|
},
|
||||||
|
|
||||||
ignorePopup: function(e) {
|
|
||||||
if ( e.isTrusted === false ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let contObs = contentObserver;
|
|
||||||
contObs.ignoredPopups.set(this, true);
|
|
||||||
this.removeEventListener('keydown', contObs.ignorePopup, true);
|
|
||||||
this.removeEventListener('mousedown', contObs.ignorePopup, true);
|
|
||||||
},
|
|
||||||
|
|
||||||
observe: function(doc) {
|
observe: function(doc) {
|
||||||
let win = doc.defaultView;
|
let win = doc.defaultView;
|
||||||
|
|
||||||
|
@ -265,11 +246,6 @@ const contentObserver = {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( win.opener && this.ignoredPopups.has(win) === false ) {
|
|
||||||
win.addEventListener('keydown', this.ignorePopup, true);
|
|
||||||
win.addEventListener('mousedown', this.ignorePopup, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
let loc = win.location;
|
let loc = win.location;
|
||||||
|
|
||||||
if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' && loc.protocol !== 'file:' ) {
|
if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' && loc.protocol !== 'file:' ) {
|
||||||
|
@ -291,10 +267,6 @@ const contentObserver = {
|
||||||
let doc = e.target;
|
let doc = e.target;
|
||||||
doc.removeEventListener(e.type, docReady, true);
|
doc.removeEventListener(e.type, docReady, true);
|
||||||
lss(this.contentBaseURI + 'contentscript-end.js', sandbox);
|
lss(this.contentBaseURI + 'contentscript-end.js', sandbox);
|
||||||
|
|
||||||
if ( doc.querySelector('a[href^="abp:"]') ) {
|
|
||||||
lss(this.contentBaseURI + 'subscriber.js', sandbox);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if ( doc.readyState === 'loading') {
|
if ( doc.readyState === 'loading') {
|
||||||
|
|
|
@ -1027,7 +1027,6 @@ var httpObserver = {
|
||||||
16: 'websocket',
|
16: 'websocket',
|
||||||
21: 'image'
|
21: 'image'
|
||||||
},
|
},
|
||||||
lastRequest: [{}, {}],
|
|
||||||
|
|
||||||
get componentRegistrar() {
|
get componentRegistrar() {
|
||||||
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
|
return Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
|
||||||
|
@ -1104,9 +1103,8 @@ var httpObserver = {
|
||||||
var type = this.typeMap[details.type] || 'other';
|
var type = this.typeMap[details.type] || 'other';
|
||||||
var result;
|
var result;
|
||||||
var callbackDetails = {
|
var callbackDetails = {
|
||||||
frameId: details.frameId,
|
|
||||||
hostname: URI.asciiHost,
|
hostname: URI.asciiHost,
|
||||||
parentFrameId: details.parentFrameId,
|
parentFrameId: type === 'main_frame' ? -1 : 0,
|
||||||
tabId: details.tabId,
|
tabId: details.tabId,
|
||||||
type: type,
|
type: type,
|
||||||
url: URI.asciiSpec
|
url: URI.asciiSpec
|
||||||
|
@ -1172,7 +1170,7 @@ var httpObserver = {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
type = this.frameTypeMap[channelData[4]];
|
type = this.frameTypeMap[channelData[1]];
|
||||||
if ( !type ) {
|
if ( !type ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1187,10 +1185,10 @@ var httpObserver = {
|
||||||
|
|
||||||
result = vAPI.net.onHeadersReceived.callback({
|
result = vAPI.net.onHeadersReceived.callback({
|
||||||
hostname: URI.asciiHost,
|
hostname: URI.asciiHost,
|
||||||
parentFrameId: channelData[1],
|
parentFrameId: type === 6 ? -1 : 0,
|
||||||
responseHeaders: result ? [{name: topic, value: result}] : [],
|
responseHeaders: result ? [{name: topic, value: result}] : [],
|
||||||
tabId: channelData[3],
|
tabId: channelData[0],
|
||||||
type: type,
|
type: this.typeMap[type] || 'other',
|
||||||
url: URI.asciiSpec
|
url: URI.asciiSpec
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1207,57 +1205,54 @@ var httpObserver = {
|
||||||
|
|
||||||
// http-on-opening-request
|
// http-on-opening-request
|
||||||
|
|
||||||
var lastRequest = this.lastRequest[0];
|
// https://github.com/gorhill/uMatrix/issues/165
|
||||||
|
// https://developer.mozilla.org/en-US/Firefox/Releases/3.5/Updating_extensions#Getting_a_load_context_from_a_request
|
||||||
if ( lastRequest.url !== URI.spec ) {
|
// Not sure `umatrix:shouldLoad` is still needed, uMatrix does not
|
||||||
if ( this.lastRequest[1].url === URI.spec ) {
|
// care about embedded frames topography.
|
||||||
lastRequest = this.lastRequest[1];
|
var tabId = vAPI.noTabId;
|
||||||
} else {
|
var loadCtx;
|
||||||
lastRequest.url = null;
|
try {
|
||||||
|
loadCtx = channel
|
||||||
|
.QueryInterface(Components.interfaces.nsIChannel)
|
||||||
|
.notificationCallbacks
|
||||||
|
.getInterface(Components.interfaces.nsILoadContext);
|
||||||
|
} catch (ex) {
|
||||||
|
try {
|
||||||
|
loadCtx = channel
|
||||||
|
.loadGroup.notificationCallbacks
|
||||||
|
.getInterface(Components.interfaces.nsILoadContext);
|
||||||
|
} catch (ex) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ( loadCtx && loadCtx.associatedWindow ) {
|
||||||
if ( lastRequest.url === null ) {
|
tabId = vAPI.tabs.getTabId(
|
||||||
lastRequest.type = channel.loadInfo && channel.loadInfo.contentPolicyType || 1;
|
loadCtx
|
||||||
result = this.handleRequest(channel, URI, {
|
.associatedWindow
|
||||||
tabId: vAPI.noTabId,
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
||||||
type: lastRequest.type
|
.getInterface(Ci.nsIWebNavigation)
|
||||||
});
|
.QueryInterface(Ci.nsIDocShell)
|
||||||
|
.rootTreeItem
|
||||||
if ( result === true ) {
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
||||||
return;
|
.getInterface(Ci.nsIDOMWindow)
|
||||||
}
|
.gBrowser
|
||||||
|
.getBrowserForContentWindow(loadCtx.associatedWindow)
|
||||||
if ( channel instanceof Ci.nsIWritablePropertyBag === false ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Carry data for behind-the-scene redirects
|
|
||||||
channel.setProperty(
|
|
||||||
this.REQDATAKEY,
|
|
||||||
[lastRequest.type, vAPI.noTabId, null, 0, -1]
|
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
type = channel.loadInfo && channel.loadInfo.contentPolicyType || 1;
|
||||||
|
result = this.handleRequest(channel, URI, { tabId: tabId, type: type });
|
||||||
|
|
||||||
|
if ( result === true ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Important! When loading file via XHR for mirroring,
|
if ( channel instanceof Ci.nsIWritablePropertyBag === false ) {
|
||||||
// the URL will be the same, so it could fall into an infinite loop
|
|
||||||
lastRequest.url = null;
|
|
||||||
|
|
||||||
if ( this.handleRequest(channel, URI, lastRequest) ) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If request is not handled we may use the data in on-modify-request
|
// Carry data for behind-the-scene redirects
|
||||||
if ( channel instanceof Ci.nsIWritablePropertyBag ) {
|
channel.setProperty(this.REQDATAKEY, [tabId, type]);
|
||||||
channel.setProperty(this.REQDATAKEY, [
|
return;
|
||||||
lastRequest.frameId,
|
|
||||||
lastRequest.parentFrameId,
|
|
||||||
lastRequest.sourceTabId,
|
|
||||||
lastRequest.tabId,
|
|
||||||
lastRequest.type
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// contentPolicy.shouldLoad doesn't detect redirects, this needs to be used
|
// contentPolicy.shouldLoad doesn't detect redirects, this needs to be used
|
||||||
|
@ -1279,10 +1274,8 @@ var httpObserver = {
|
||||||
var channelData = oldChannel.getProperty(this.REQDATAKEY);
|
var channelData = oldChannel.getProperty(this.REQDATAKEY);
|
||||||
|
|
||||||
var details = {
|
var details = {
|
||||||
frameId: channelData[0],
|
tabId: channelData[0],
|
||||||
parentFrameId: channelData[1],
|
type: channelData[1]
|
||||||
tabId: channelData[3],
|
|
||||||
type: channelData[4]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if ( this.handleRequest(newChannel, URI, details) ) {
|
if ( this.handleRequest(newChannel, URI, details) ) {
|
||||||
|
@ -1317,21 +1310,7 @@ vAPI.net.registerListeners = function() {
|
||||||
null;
|
null;
|
||||||
|
|
||||||
var shouldLoadListenerMessageName = location.host + ':shouldLoad';
|
var shouldLoadListenerMessageName = location.host + ':shouldLoad';
|
||||||
var shouldLoadListener = function(e) {
|
var shouldLoadListener = function(e) { };
|
||||||
var details = e.data;
|
|
||||||
var tabId = vAPI.tabs.getTabId(e.target);
|
|
||||||
var sourceTabId = null;
|
|
||||||
var lastRequest = httpObserver.lastRequest;
|
|
||||||
lastRequest[1] = lastRequest[0];
|
|
||||||
lastRequest[0] = {
|
|
||||||
frameId: details.frameId,
|
|
||||||
parentFrameId: details.parentFrameId,
|
|
||||||
sourceTabId: sourceTabId,
|
|
||||||
tabId: tabId,
|
|
||||||
type: details.type,
|
|
||||||
url: details.url
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
vAPI.messaging.globalMessageManager.addMessageListener(
|
vAPI.messaging.globalMessageManager.addMessageListener(
|
||||||
shouldLoadListenerMessageName,
|
shouldLoadListenerMessageName,
|
||||||
|
|
Loading…
Reference in a new issue