1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-24 11:22:44 +01:00

code review for c5d8588118

This commit is contained in:
Raymond Hill 2018-05-17 16:24:08 -04:00
parent 21670a031f
commit e693e308d4
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -36,14 +36,16 @@
scriptletsRegister = new Map(),
reEscapeScriptArg = /[\\'"]/g;
let contentscriptCodeParts = [
'(',
function() {
let contentscriptCode = (function() {
let parts = [
'(',
function(hostname, scriptlets) {
if ( hostname !== window.location.hostname ) { return; }
let d = document;
let script = d.createElement('script');
try {
script.appendChild(d.createTextNode(
decodeURIComponent(arguments[0]))
decodeURIComponent(scriptlets))
);
(d.head || d.documentElement).appendChild(script);
} catch (ex) {
@ -52,12 +54,24 @@
script.parentNode.removeChild(script);
}
}.toString(),
')("', 'scriptlets-slot', '");\n',
'void 0;',
];
let contentscriptCodeScriptletsSlot =
contentscriptCodeParts.indexOf('scriptlets-slot');
')(',
'"', 'hostname-slot', '", ',
'"', 'scriptlets-slot', '"',
'); void 0;',
];
return {
parts: parts,
hostnameSlot: parts.indexOf('hostname-slot'),
scriptletsSlot: parts.indexOf('scriptlets-slot'),
assemble: function(hostname, scriptlets) {
this.parts[this.hostnameSlot] = hostname;
this.parts[this.scriptletsSlot] =
encodeURIComponent(scriptlets);
return this.parts.join('');
}
};
})();
let lookupScriptlet = function(raw, reng, toInject) {
if ( toInject.has(raw) ) { return; }
if ( scriptletCache.resetTime < reng.modifyTime ) {
@ -288,15 +302,14 @@
request.entity = µb.URI.entityFromDomain(request.domain);
let scriptlets = µb.scriptletFilteringEngine.retrieve(request);
if ( scriptlets === undefined ) { return; }
let code = contentscriptCode.assemble(request.hostname, scriptlets);
if ( µb.hiddenSettings.debugScriptlets ) {
scriptlets = 'debugger;\n' + scriptlets;
code = 'debugger;\n' + code;
}
contentscriptCodeParts[contentscriptCodeScriptletsSlot] =
encodeURIComponent(scriptlets);
chrome.tabs.executeScript(
details.tabId,
{
code: contentscriptCodeParts.join(''),
code: code,
frameId: details.frameId,
matchAboutBlank: true,
runAt: 'document_start'