No, there is no way to "exclude all extensions". That is, to ensure that no one else writes in the DOM except you.
You can make an attempt at it. Basically, the DOM is "written" using techniques like innerHTML
, appendChild
, insertAdjacentHTML
, etc.
You'd have to rewrite functions, implement getters and setters etc.
Implementing these changes for document.body
is not enough, as each element has the ability to make such changes, so you'd have to follow the prototype chain where you'll see facts like appendChild
being a member of Node
and innerHTML
a member of Element
and make the changes there.
Rewriting the appendChild
would be easy. For the sake of argument, I'll forbid the <p>
tag anywhere in the body
(rough example):
var old = Node.prototype.appendChild;
Node.prototype.appendChild = function (n){
try {
if (n.tagName.toLowerCase() == "p"){
return;
}
} catch(e) {
}
old.apply(this, [n]);
}
As already stated, if we'd just rewrite the document.body.appendChild
, anyone would be able to appendChild
a <p>
to a <div>
or what not - so we have to go to the root of it.
And after taking care of all of these stuff, who's to say that no one else comes along and rewrites what you've just written?
And on top of it all, there are other ways to change these values, like man-in-the-middle "attacks", where user (or attacker) will modify whatever comes from the server (all scripts) through a Proxy, before those ever reaching the browser - and the browser will blindly obey.
Or the user can extend an opensource browser like Chromium and implement whatever rendering logic they want, etc.
Bottom line: you have no control over code that reaches the client, so don't rely on that.