1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

minor code review

This commit is contained in:
gorhill 2017-01-09 09:16:37 -05:00
parent 9c4fbeb1fc
commit b21e765f5c

View File

@ -460,6 +460,21 @@ var contentObserver = {
return sandbox; return sandbox;
}, },
injectOtherContentScripts: function(doc, sandbox) {
let docReady = (e) => {
let doc = e.target;
doc.removeEventListener(e.type, docReady, true);
if ( doc.querySelector('a[href^="abp:"],a[href^="https://subscribe.adblockplus.org/?"]') ) {
Services.scriptloader.loadSubScript(this.contentBaseURI + 'scriptlets/subscriber.js', sandbox);
}
};
if ( doc.readyState === 'loading') {
doc.addEventListener('DOMContentLoaded', docReady, true);
} else {
docReady({ target: doc, type: 'DOMContentLoaded' });
}
},
ignorePopup: function(e) { ignorePopup: function(e) {
if ( e.isTrusted === false ) { return; } if ( e.isTrusted === false ) { return; }
let contObs = contentObserver; let contObs = contentObserver;
@ -467,6 +482,7 @@ var contentObserver = {
this.removeEventListener('keydown', contObs.ignorePopup, true); this.removeEventListener('keydown', contObs.ignorePopup, true);
this.removeEventListener('mousedown', contObs.ignorePopup, true); this.removeEventListener('mousedown', contObs.ignorePopup, true);
}, },
lookupPopupOpener: function(popup) { lookupPopupOpener: function(popup) {
for (;;) { for (;;) {
let opener = popup.opener; let opener = popup.opener;
@ -483,15 +499,28 @@ var contentObserver = {
popup = opener; popup = opener;
} }
}, },
reValidPopups: /^(?:blob|data|https?|javascript):/, reValidPopups: /^(?:blob|data|https?|javascript):/,
reMustInjectScript: /^(?:file|https?):/,
observe: function(subject, topic) { observe: function(subject, topic) {
// For whatever reason, sometimes the global scope is completely
// uninitialized at this point. Repro steps:
// - Launch FF with uBlock enabled
// - Disable uBlock
// - Enable uBlock
// - Services and all other global variables are undefined
// Hopefully will eventually understand why this happens.
if ( Services === undefined ) { return; }
// https://github.com/gorhill/uBlock/issues/2290 // https://github.com/gorhill/uBlock/issues/2290
if ( topic === 'content-document-global-created' ) { if ( topic === 'content-document-global-created' ) {
if ( subject !== subject.top ) { return; } if ( subject !== subject.top || !subject.opener ) { return; }
if ( this.ignoredPopups.has(subject) ) { return; } if ( this.ignoredPopups.has(subject) ) { return; }
let opener = this.lookupPopupOpener(subject); let opener = this.lookupPopupOpener(subject);
if ( !opener ) { return; } if ( !opener ) { return; }
subject.addEventListener('keydown', this.ignorePopup, true);
subject.addEventListener('mousedown', this.ignorePopup, true);
let popupMessager = getMessageManager(subject); let popupMessager = getMessageManager(subject);
if ( !popupMessager ) { return; } if ( !popupMessager ) { return; }
let openerMessager = getMessageManager(opener); let openerMessager = getMessageManager(opener);
@ -508,24 +537,12 @@ var contentObserver = {
return; return;
} }
// For whatever reason, sometimes the global scope is completely // topic === 'document-element-inserted'
// uninitialized at this point. Repro steps:
// - Launch FF with uBlock enabled
// - Disable uBlock
// - Enable uBlock
// - Services and all other global variables are undefined
// Hopefully will eventually understand why this happens.
if ( Services === undefined ) { return; }
let doc = subject; let doc = subject;
let win = doc.defaultView || null; let win = doc.defaultView || null;
if ( win === null ) { return; } if ( win === null ) { return; }
if ( win.opener && this.ignoredPopups.has(win) === false ) {
win.addEventListener('keydown', this.ignorePopup, true);
win.addEventListener('mousedown', this.ignorePopup, true);
}
// https://github.com/gorhill/uBlock/issues/260 // https://github.com/gorhill/uBlock/issues/260
// https://developer.mozilla.org/en-US/docs/Web/API/Document/contentType // https://developer.mozilla.org/en-US/docs/Web/API/Document/contentType
// "Non-standard, only supported by Gecko. To be used in // "Non-standard, only supported by Gecko. To be used in
@ -536,7 +553,7 @@ var contentObserver = {
if ( doc.contentType.startsWith('image/') ) { return; } if ( doc.contentType.startsWith('image/') ) { return; }
let loc = win.location; let loc = win.location;
if ( loc.protocol !== 'http:' && loc.protocol !== 'https:' && loc.protocol !== 'file:' ) { if ( this.reMustInjectScript.test(loc.protocol) === false ) {
if ( loc.protocol === 'chrome:' && loc.host === hostName ) { if ( loc.protocol === 'chrome:' && loc.host === hostName ) {
this.initContentScripts(win); this.initContentScripts(win);
} }
@ -544,9 +561,9 @@ var contentObserver = {
return; return;
} }
// Content scripts injection.
let lss = Services.scriptloader.loadSubScript; let lss = Services.scriptloader.loadSubScript;
let sandbox = this.initContentScripts(win, true); let sandbox = this.initContentScripts(win, true);
try { try {
lss(this.contentBaseURI + 'vapi-client.js', sandbox); lss(this.contentBaseURI + 'vapi-client.js', sandbox);
lss(this.contentBaseURI + 'contentscript.js', sandbox); lss(this.contentBaseURI + 'contentscript.js', sandbox);
@ -554,20 +571,10 @@ var contentObserver = {
//console.exception(ex.msg, ex.stack); //console.exception(ex.msg, ex.stack);
return; return;
} }
// The remaining scripts are worth injecting only on a top-level window
let docReady = (e) => { // and at document_idle time.
let doc = e.target; if ( win === win.top ) {
doc.removeEventListener(e.type, docReady, true); this.injectOtherContentScripts(doc, sandbox);
if ( doc.querySelector('a[href^="abp:"],a[href^="https://subscribe.adblockplus.org/?"]') ) {
lss(this.contentBaseURI + 'scriptlets/subscriber.js', sandbox);
}
};
if ( doc.readyState === 'loading') {
doc.addEventListener('DOMContentLoaded', docReady, true);
} else {
docReady({ target: doc, type: 'DOMContentLoaded' });
} }
} }
}; };