1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-25 03:43:25 +01:00

Try to inject scriptlets at onResponseStarted() time

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2350

As per AdGuard findings, it's possible (though unreliable) to try
to inject scriptlets at webRequest.onResponseStarted time, which
increases scriptlet injection reliability overall when injecting
from multiple entry points.

uBO was already injecting at webNavigation.onCommitted and
main content script time, and adding webRequest.onResponseStarted
as an entry point for scriptlet injection increases reliability
for webpages which executes inline scripts at the top of the DOM.

References:
- https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1029
- https://github.com/AdguardTeam/AdguardBrowserExtension/blob/9ab85be5/Extension/src/background/webrequest.js#L620
This commit is contained in:
Raymond Hill 2022-11-06 16:54:32 -05:00
parent e0e68a24d2
commit 49df063191
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 25 additions and 8 deletions

View File

@ -96,7 +96,6 @@ const contentscriptCode = (( ) => {
) {
return;
}
self.uBO_scriptletsInjected = true;
const injectScriptlets = function(d) {
let script;
try {
@ -105,12 +104,11 @@ const contentscriptCode = (( ) => {
decodeURIComponent(scriptlets))
);
(d.head || d.documentElement).appendChild(script);
self.uBO_scriptletsInjected = true;
} catch (ex) {
}
if ( script ) {
if ( script.parentNode ) {
script.parentNode.removeChild(script);
}
script.remove();
script.textContent = '';
}
};

View File

@ -19,6 +19,8 @@
Home: https://github.com/gorhill/uBlock
*/
/* globals browser */
'use strict';
/******************************************************************************/

View File

@ -933,10 +933,7 @@ vAPI.Tabs = class extends vAPI.Tabs {
const pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore === null ) { return; }
pageStore.setFrameURL(details);
if (
vAPI.webextFlavor.soup.has('firefox') === false &&
pageStore.getNetFilteringSwitch()
) {
if ( pageStore.getNetFilteringSwitch() ) {
scriptletFilteringEngine.injectNow(details);
}
}

View File

@ -19,6 +19,8 @@
Home: https://github.com/gorhill/uBlock
*/
/* globals browser */
'use strict';
/******************************************************************************/
@ -1131,6 +1133,11 @@ const strictBlockBypasser = {
/******************************************************************************/
// https://github.com/uBlockOrigin/uBlock-issues/issues/2350
// Added scriptlet injection attempt at onResponseStarted time as per
// https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1029 and
// https://github.com/AdguardTeam/AdguardBrowserExtension/blob/9ab85be5/Extension/src/background/webrequest.js#L620
const webRequest = {
onBeforeRequest,
@ -1148,6 +1155,19 @@ const webRequest = {
{ urls: [ 'http://*/*', 'https://*/*' ] },
[ 'blocking', 'responseHeaders' ]
);
vAPI.net.addListener(
'onResponseStarted',
details => {
const pageStore = µb.pageStoreFromTabId(details.tabId);
if ( pageStore === null ) { return; }
if ( pageStore.getNetFilteringSwitch() === false ) { return; }
scriptletFilteringEngine.injectNow(details);
},
{
types: [ 'main_frame', 'sub_frame' ],
urls: [ 'http://*/*', 'https://*/*' ]
}
);
vAPI.net.unsuspend({ all: true });
};
})(),