1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00
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
*/
// For background page
/* globals browser */
'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
// 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) {
if ( style === undefined ) { return; }
let styleToken = this.styleTokenMap.get(vAPI.hideStyle);
let styleToken = this.styleTokenMap.get(style);
if ( styleToken !== undefined ) { return styleToken; }
styleToken = vAPI.randomToken();
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' });
url = self.URL.createObjectURL(blob);
script = doc.createElement('script');
script.async = false;
script.src = url;
(doc.head || doc.documentElement || doc).appendChild(script);
} catch (ex) {
}
if ( script ) {
script.remove();
script.src = '';
}
if ( url ) {
if ( script ) { script.remove(); }
self.URL.revokeObjectURL(url);
}
};

View File

@ -88,7 +88,7 @@ const scriptletFilteringEngine = {
const contentscriptCode = (( ) => {
const parts = [
'(',
function(hostname, scriptlets) {
function(injector, hostname, scriptlets) {
const doc = document;
if (
doc.location === null ||
@ -97,29 +97,11 @@ const contentscriptCode = (( ) => {
) {
return;
}
let script, url;
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);
}
injector(doc, decodeURIComponent(scriptlets));
if ( typeof self.uBO_scriptletsInjected === 'boolean' ) { return 0; }
}.toString(),
')(',
vAPI.scriptletsInjector, ', ',
'"', 'hostname-slot', '", ',
'"', 'scriptlets-slot', '"',
');',
@ -130,8 +112,7 @@ const contentscriptCode = (( ) => {
scriptletsSlot: parts.indexOf('scriptlets-slot'),
assemble: function(hostname, scriptlets) {
this.parts[this.hostnameSlot] = hostname;
this.parts[this.scriptletsSlot] =
encodeURIComponent(scriptlets);
this.parts[this.scriptletsSlot] = encodeURIComponent(scriptlets);
return this.parts.join('');
}
};