Skip to content

Commit

Permalink
Try to inject scriptlets at onResponseStarted() time
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-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:
- AdguardTeam/AdguardBrowserExtension#1029
- https://github.com/AdguardTeam/AdguardBrowserExtension/blob/9ab85be5/Extension/src/background/webrequest.js#L620
  • Loading branch information
gorhill committed Nov 6, 2022
1 parent e0e68a2 commit 49df063
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/js/scriptlet-filtering.js
Expand Up @@ -96,7 +96,6 @@ const contentscriptCode = (( ) => {
) {
return;
}
self.uBO_scriptletsInjected = true;
const injectScriptlets = function(d) {
let script;
try {
Expand All @@ -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 = '';
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/js/start.js
Expand Up @@ -19,6 +19,8 @@
Home: https://github.com/gorhill/uBlock
*/

/* globals browser */

'use strict';

/******************************************************************************/
Expand Down
5 changes: 1 addition & 4 deletions src/js/tab.js
Expand Up @@ -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);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/js/traffic.js
Expand Up @@ -19,6 +19,8 @@
Home: https://github.com/gorhill/uBlock
*/

/* globals browser */

'use strict';

/******************************************************************************/
Expand Down Expand Up @@ -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,

Expand All @@ -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 });
};
})(),
Expand Down

0 comments on commit 49df063

Please sign in to comment.