Created
December 31, 2018 22:53
-
-
Save regevbr/a36ee039b1b49b676618ad4465741113 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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