Raymond Hill 2022-12-12 14:02:57 -05:00
parent e537748988
commit 58e60d6d96
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
6 changed files with 54 additions and 29 deletions

View File

@ -19,7 +19,7 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
// For background page /* globals browser */
'use strict'; 'use strict';
@ -239,3 +239,20 @@ vAPI.prefetching = (( ) => {
})(); })();
/******************************************************************************/ /******************************************************************************/
vAPI.scriptletsInjector = ((doc, scriptlets) => {
let script;
try {
script = doc.createElement('script');
script.appendChild(doc.createTextNode(scriptlets));
(doc.head || doc.documentElement).appendChild(script);
self.uBO_scriptletsInjected = true;
} catch (ex) {
}
if ( script ) {
script.remove();
script.textContent = '';
}
}).toString();
/******************************************************************************/

View File

@ -1263,6 +1263,15 @@ vAPI.Net = class {
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
// To be defined by platform-specific code.
vAPI.scriptletsInjector = (( ) => {
self.uBO_scriptletsInjected = true;
}).toString();
/******************************************************************************/
/******************************************************************************/
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus#Browser_compatibility // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus#Browser_compatibility
// Firefox for Android does no support browser.contextMenus. // Firefox for Android does no support browser.contextMenus.

View File

@ -303,3 +303,23 @@ vAPI.Net = class extends vAPI.Net {
}; };
/******************************************************************************/ /******************************************************************************/
vAPI.scriptletsInjector = ((doc, scriptlets) => {
let script, url;
try {
const blob = new self.Blob([ scriptlets ], { type: 'text/javascript' });
url = self.URL.createObjectURL(blob);
script = doc.createElement('script');
script.async = false;
script.src = url;
(doc.head || doc.documentElement).appendChild(script);
self.uBO_scriptletsInjected = true;
} catch (ex) {
}
if ( url ) {
if ( script ) { script.remove(); }
self.URL.revokeObjectURL(url);
}
}).toString();
/******************************************************************************/

View File

@ -541,7 +541,7 @@ class ProceduralFilterer {
styleTokenFromStyle(style) { styleTokenFromStyle(style) {
if ( style === undefined ) { return; } if ( style === undefined ) { return; }
let styleToken = this.styleTokenMap.get(vAPI.hideStyle); let styleToken = this.styleTokenMap.get(style);
if ( styleToken !== undefined ) { return styleToken; } if ( styleToken !== undefined ) { return styleToken; }
styleToken = vAPI.randomToken(); styleToken = vAPI.randomToken();
this.styleTokenMap.set(style, styleToken); this.styleTokenMap.set(style, styleToken);

View File

@ -470,15 +470,13 @@ vAPI.injectScriptlet = function(doc, text) {
const blob = new self.Blob([ text ], { type: 'text/javascript' }); const blob = new self.Blob([ text ], { type: 'text/javascript' });
url = self.URL.createObjectURL(blob); url = self.URL.createObjectURL(blob);
script = doc.createElement('script'); script = doc.createElement('script');
script.async = false;
script.src = url; script.src = url;
(doc.head || doc.documentElement || doc).appendChild(script); (doc.head || doc.documentElement || doc).appendChild(script);
} catch (ex) { } catch (ex) {
} }
if ( script ) {
script.remove();
script.src = '';
}
if ( url ) { if ( url ) {
if ( script ) { script.remove(); }
self.URL.revokeObjectURL(url); self.URL.revokeObjectURL(url);
} }
}; };

View File

@ -88,7 +88,7 @@ const scriptletFilteringEngine = {
const contentscriptCode = (( ) => { const contentscriptCode = (( ) => {
const parts = [ const parts = [
'(', '(',
function(hostname, scriptlets) { function(injector, hostname, scriptlets) {
const doc = document; const doc = document;
if ( if (
doc.location === null || doc.location === null ||
@ -97,29 +97,11 @@ const contentscriptCode = (( ) => {
) { ) {
return; return;
} }
let script, url; injector(doc, decodeURIComponent(scriptlets));
try {
const blob = new self.Blob(
[ decodeURIComponent(scriptlets) ],
{ type: 'text/javascript' }
);
url = self.URL.createObjectURL(blob);
script = doc.createElement('script');
script.src = url;
(doc.head || doc.documentElement).appendChild(script);
self.uBO_scriptletsInjected = true;
} catch (ex) {
}
if ( script ) {
script.remove();
script.src = '';
}
if ( url ) {
self.URL.revokeObjectURL(url);
}
if ( typeof self.uBO_scriptletsInjected === 'boolean' ) { return 0; } if ( typeof self.uBO_scriptletsInjected === 'boolean' ) { return 0; }
}.toString(), }.toString(),
')(', ')(',
vAPI.scriptletsInjector, ', ',
'"', 'hostname-slot', '", ', '"', 'hostname-slot', '", ',
'"', 'scriptlets-slot', '"', '"', 'scriptlets-slot', '"',
');', ');',
@ -130,8 +112,7 @@ const contentscriptCode = (( ) => {
scriptletsSlot: parts.indexOf('scriptlets-slot'), scriptletsSlot: parts.indexOf('scriptlets-slot'),
assemble: function(hostname, scriptlets) { assemble: function(hostname, scriptlets) {
this.parts[this.hostnameSlot] = hostname; this.parts[this.hostnameSlot] = hostname;
this.parts[this.scriptletsSlot] = this.parts[this.scriptletsSlot] = encodeURIComponent(scriptlets);
encodeURIComponent(scriptlets);
return this.parts.join(''); return this.parts.join('');
} }
}; };