Skip to content

Instantly share code, notes, and snippets.

@regevbr
Created December 31, 2018 22:53
Show Gist options
  • Save regevbr/a36ee039b1b49b676618ad4465741113 to your computer and use it in GitHub Desktop.
Save regevbr/a36ee039b1b49b676618ad4465741113 to your computer and use it in GitHub Desktop.
const createElementBackup = document.createElement;
function myCreateElement(tagName: string, options?: ElementCreationOptions): HTMLElement {
const scriptElt = createElementBackup.call(document, tagName, options);
if (tagName.toLowerCase() !== 'script') {
return scriptElt;
}
const originalSetAttribute = scriptElt.setAttribute.bind(scriptElt);
Object.defineProperties(scriptElt, {
'src': {
get() {
return scriptElt.getAttribute('src');
},
set(value: string) {
// This is where the magic happends
const mutated = urlMutator(value);
originalSetAttribute('src', mutated || value);
return true;
}
}
});
return scriptElt;
}
document.createElement = myCreateElement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment