mirror of https://github.com/gorhill/uBlock.git
Add ability to lookup effective context from store of frames
Content scripts can't properly look up effective context for sandboxed frames. This commit add ability to extract effective context from already existing store of frames used for each tab.
This commit is contained in:
parent
37c9f966ac
commit
114012ae11
|
@ -1735,7 +1735,6 @@ vAPI.injectScriptlet = function(doc, text) {
|
||||||
vAPI.messaging.send('contentscript', {
|
vAPI.messaging.send('contentscript', {
|
||||||
what: 'retrieveContentScriptParameters',
|
what: 'retrieveContentScriptParameters',
|
||||||
url: vAPI.effectiveSelf.location.href,
|
url: vAPI.effectiveSelf.location.href,
|
||||||
charset: document.characterSet,
|
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
bootstrapPhase1(response);
|
bootstrapPhase1(response);
|
||||||
});
|
});
|
||||||
|
|
|
@ -557,6 +557,13 @@ const retrieveContentScriptParameters = function(sender, request) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A content script may not always be able to successfully look up the
|
||||||
|
// effective context, hence in such case we try again to look up here
|
||||||
|
// using cached information about embedded frames.
|
||||||
|
if ( frameId !== 0 && request.url.startsWith('about:') ) {
|
||||||
|
request.url = pageStore.getEffectiveFrameURL(sender);
|
||||||
|
}
|
||||||
|
|
||||||
const noCosmeticFiltering = pageStore.noCosmeticFiltering === true;
|
const noCosmeticFiltering = pageStore.noCosmeticFiltering === true;
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
|
|
|
@ -181,12 +181,13 @@ const frameStoreJunkyard = [];
|
||||||
const frameStoreJunkyardMax = 50;
|
const frameStoreJunkyardMax = 50;
|
||||||
|
|
||||||
const FrameStore = class {
|
const FrameStore = class {
|
||||||
constructor(frameURL) {
|
constructor(frameURL, parentId) {
|
||||||
this.init(frameURL);
|
this.init(frameURL, parentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
init(frameURL) {
|
init(frameURL, parentId) {
|
||||||
this.t0 = Date.now();
|
this.t0 = Date.now();
|
||||||
|
this.parentId = parentId;
|
||||||
this.exceptCname = undefined;
|
this.exceptCname = undefined;
|
||||||
this.clickToLoad = false;
|
this.clickToLoad = false;
|
||||||
this.rawURL = frameURL;
|
this.rawURL = frameURL;
|
||||||
|
@ -199,7 +200,6 @@ const FrameStore = class {
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
this.exceptCname = undefined;
|
|
||||||
this.rawURL = this.hostname = this.domain = '';
|
this.rawURL = this.hostname = this.domain = '';
|
||||||
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
|
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
|
||||||
frameStoreJunkyard.push(this);
|
frameStoreJunkyard.push(this);
|
||||||
|
@ -207,12 +207,12 @@ const FrameStore = class {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static factory(frameURL) {
|
static factory(frameURL, parentId = -1) {
|
||||||
const entry = frameStoreJunkyard.pop();
|
const entry = frameStoreJunkyard.pop();
|
||||||
if ( entry === undefined ) {
|
if ( entry === undefined ) {
|
||||||
return new FrameStore(frameURL);
|
return new FrameStore(frameURL, parentId);
|
||||||
}
|
}
|
||||||
return entry.init(frameURL);
|
return entry.init(frameURL, parentId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -277,7 +277,7 @@ const PageStore = class {
|
||||||
|
|
||||||
this.frameAddCount = 0;
|
this.frameAddCount = 0;
|
||||||
this.frames = new Map();
|
this.frames = new Map();
|
||||||
this.setFrameURL(0, tabContext.rawURL);
|
this.setFrameURL({ url: tabContext.rawURL });
|
||||||
|
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/314
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/314
|
||||||
const masterSwitch = tabContext.getNetFilteringSwitch();
|
const masterSwitch = tabContext.getNetFilteringSwitch();
|
||||||
|
@ -324,7 +324,7 @@ const PageStore = class {
|
||||||
// As part of https://github.com/chrisaljoudi/uBlock/issues/405
|
// As part of https://github.com/chrisaljoudi/uBlock/issues/405
|
||||||
// URL changed, force a re-evaluation of filtering switch
|
// URL changed, force a re-evaluation of filtering switch
|
||||||
this.rawURL = tabContext.rawURL;
|
this.rawURL = tabContext.rawURL;
|
||||||
this.setFrameURL(0, this.rawURL);
|
this.setFrameURL({ url: this.rawURL });
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -375,14 +375,20 @@ const PageStore = class {
|
||||||
return this.frames.get(frameId) || null;
|
return this.frames.get(frameId) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
setFrameURL(frameId, frameURL) {
|
setFrameURL(details) {
|
||||||
|
let { frameId, url, parentFrameId } = details;
|
||||||
|
if ( frameId === undefined ) { frameId = 0; }
|
||||||
|
if ( parentFrameId === undefined ) { parentFrameId = -1; }
|
||||||
let frameStore = this.frames.get(frameId);
|
let frameStore = this.frames.get(frameId);
|
||||||
if ( frameStore !== undefined ) {
|
if ( frameStore !== undefined ) {
|
||||||
return frameURL === frameStore.rawURL
|
if ( url === frameStore.rawURL ) {
|
||||||
? frameStore
|
frameStore.parentId = parentFrameId;
|
||||||
: frameStore.init(frameURL);
|
} else {
|
||||||
|
frameStore.init(url, parentFrameId);
|
||||||
|
}
|
||||||
|
return frameStore;
|
||||||
}
|
}
|
||||||
frameStore = FrameStore.factory(frameURL);
|
frameStore = FrameStore.factory(url, parentFrameId);
|
||||||
this.frames.set(frameId, frameStore);
|
this.frames.set(frameId, frameStore);
|
||||||
this.frameAddCount += 1;
|
this.frameAddCount += 1;
|
||||||
if ( (this.frameAddCount & 0b111111) === 0 ) {
|
if ( (this.frameAddCount & 0b111111) === 0 ) {
|
||||||
|
@ -391,6 +397,20 @@ const PageStore = class {
|
||||||
return frameStore;
|
return frameStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getEffectiveFrameURL(sender) {
|
||||||
|
let { frameId } = sender;
|
||||||
|
for (;;) {
|
||||||
|
const frameStore = this.getFrameStore(frameId);
|
||||||
|
if ( frameStore === null ) { break; }
|
||||||
|
if ( frameStore.rawURL.startsWith('about:') === false ) {
|
||||||
|
return frameStore.rawURL;
|
||||||
|
}
|
||||||
|
frameId = frameStore.parentId;
|
||||||
|
if ( frameId === -1 ) { break; }
|
||||||
|
}
|
||||||
|
return sender.frameURL;
|
||||||
|
}
|
||||||
|
|
||||||
// There is no event to tell us a specific subframe has been removed from
|
// There is no event to tell us a specific subframe has been removed from
|
||||||
// the main document. The code below will remove subframes which are no
|
// the main document. The code below will remove subframes which are no
|
||||||
// longer present in the root document. Removing obsolete subframes is
|
// longer present in the root document. Removing obsolete subframes is
|
||||||
|
@ -851,7 +871,7 @@ const PageStore = class {
|
||||||
clickToLoad(frameId, frameURL) {
|
clickToLoad(frameId, frameURL) {
|
||||||
let frameStore = this.getFrameStore(frameId);
|
let frameStore = this.getFrameStore(frameId);
|
||||||
if ( frameStore === null ) {
|
if ( frameStore === null ) {
|
||||||
frameStore = this.setFrameURL(frameId, frameURL);
|
frameStore = this.setFrameURL({ frameId, url: frameURL });
|
||||||
}
|
}
|
||||||
this.netFilteringCache.forgetResult(
|
this.netFilteringCache.forgetResult(
|
||||||
this.tabHostname,
|
this.tabHostname,
|
||||||
|
|
|
@ -875,18 +875,23 @@ vAPI.Tabs = class extends vAPI.Tabs {
|
||||||
onNavigation(details) {
|
onNavigation(details) {
|
||||||
super.onNavigation(details);
|
super.onNavigation(details);
|
||||||
const µb = µBlock;
|
const µb = µBlock;
|
||||||
if ( details.frameId === 0 ) {
|
const { frameId, tabId, url } = details;
|
||||||
µb.tabContextManager.commit(details.tabId, details.url);
|
if ( frameId === 0 ) {
|
||||||
let pageStore = µb.bindTabToPageStore(details.tabId, 'tabCommitted');
|
µb.tabContextManager.commit(tabId, url);
|
||||||
if ( pageStore ) {
|
const pageStore = µb.bindTabToPageStore(tabId, 'tabCommitted');
|
||||||
pageStore.journalAddRootFrame('committed', details.url);
|
if ( pageStore !== null ) {
|
||||||
|
pageStore.journalAddRootFrame('committed', url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( µb.canInjectScriptletsNow && µb.URI.isNetworkURI(details.url) ) {
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
||||||
const pageStore = µb.pageStoreFromTabId(details.tabId);
|
if ( pageStore === null ) { return; }
|
||||||
if ( pageStore !== null && pageStore.getNetFilteringSwitch() ) {
|
pageStore.setFrameURL(details);
|
||||||
µb.scriptletFilteringEngine.injectNow(details);
|
if (
|
||||||
}
|
µb.canInjectScriptletsNow &&
|
||||||
|
µb.URI.isNetworkURI(url) &&
|
||||||
|
pageStore.getNetFilteringSwitch()
|
||||||
|
) {
|
||||||
|
µb.scriptletFilteringEngine.injectNow(details);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ const onBeforeRequest = function(details) {
|
||||||
details.parentFrameId !== -1 &&
|
details.parentFrameId !== -1 &&
|
||||||
details.aliasURL === undefined
|
details.aliasURL === undefined
|
||||||
) {
|
) {
|
||||||
pageStore.setFrameURL(details.frameId, details.url);
|
pageStore.setFrameURL(details);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( result === 2 ) {
|
if ( result === 2 ) {
|
||||||
|
|
Loading…
Reference in New Issue