Skip to content

Instantly share code, notes, and snippets.

@brianloveswords
Created June 18, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianloveswords/b0360822500df13b6e61ebb47dd768f4 to your computer and use it in GitHub Desktop.
Save brianloveswords/b0360822500df13b6e61ebb47dd768f4 to your computer and use it in GitHub Desktop.
function addH1() {
document.body.innerHTML += '<h1>okay!</h1>';
}
// this lets callers from `file.js` through to the original version of
// innerHTML but not other callers
(function(){
const innerHTMLDesc = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML');
Object.defineProperty(Element.prototype, 'innerHTML', {
get() {
let stack;
try { throw new Error(); }
catch(e) { stack = e.stack;}
if (/file.js/.test(stack)) {
return innerHTMLDesc.get.call(this);
}
return "nah";
},
set(value) {
let stack;
try { throw new Error(); }
catch(e) { stack = e.stack; }
if (/file.js/.test(stack)) {
return innerHTMLDesc.set.call(this, value);
}
return "nah";
}
});
})();
<h1>no user innerHTML</h1>
<script src='file.js'></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment