Created
August 27, 2015 12:00
-
-
Save sndrs/b27055580940da749ad5 to your computer and use it in GitHub Desktop.
Override document.write to prevent 3rd parties injecting blocking scripts
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<script> | |
(function(Document) { | |
Document.prototype.write = function(s) { | |
var allScripts = document.getElementsByTagName('script'), | |
thisScript = allScripts[allScripts.length-1]; | |
var writeTarget = document.createElement('div'); | |
writeTarget.innerHTML = s; | |
window.onload = requestAnimationFrame(function () { | |
thisScript.parentElement.insertBefore(writeTarget, thisScript); | |
}); | |
} | |
})(Document) | |
</script> | |
</head> | |
<body> | |
<div>div 1</div> | |
<script> | |
document.write("between 2 divs") | |
</script> | |
<div>div2</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A mistake :
})(Document) => })(document)