Fix assembling of scriptlets for isolated world

This commit is contained in:
Raymond Hill 2023-06-01 08:48:27 -04:00
parent 9b682dff03
commit 40141e2dfc
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -143,7 +143,15 @@ const isolatedWorldInjector = (( ) => {
scriptletSlot: parts.indexOf('scriptlet-slot'),
assemble: function(hostname, scriptlets) {
this.parts[this.jsonSlot] = JSON.stringify({ hostname });
return this.parts.join('').replace('function(){}', scriptlets);
const code = this.parts.join('');
// Manually substitute noop function with scriptlet wrapper
// function, so as to not suffer instances of special
// replacement characters `$`,`\` when using String.replace()
// with in scriptlet code.
const match = /function\(\)\{\}/.exec(code);
return code.slice(0, match.index) +
scriptlets +
code.slice(match.index + match[0].length);
},
};
})();