How can a user script map its own DOM node object to the page’s?
04:20 09 Apr 2026

User scripts are typically executed in a separate realm from the page on which they run, and the objects that represent DOM nodes in the user script are not the same objects that the actual page accesses. And that, for the most part, is a good thing: it prevents mutations to properties and prototypes performed by the page from affecting (and therefore potentially breaking) the user script.

Hello!

// in the page
document.getElementById('node').test = 42;
// in the user script
console.log(document === unsafeWindow.document);                //= false
console.log(document.isSameNode(unsafeWindow.document));        //= true

console.log(document.getElementById('node').test);              //= undefined
console.log(unsafeWindow.document.getElementById('node').test); //= 42

But occasionally, a user script needs to pierce through that sandbox and obtain the page’s DOM node object, because the one the user script normally uses lacks some extra properties that the page has installed on its own DOM node object.

How to get access to it?

javascript dom userscripts