Hijack a variable using usercript for Chrome

I am trying to change a variable on a page using usercript. I know there is a variable in the source code

var smilies = false; 

In theory, I would have to change it like this:

 unsafeWindow.smilies = true; 

But that will not work. When I try to warn or register a variable on the console without hijacking, I get that it is undefined.

 alert(unsafeWindow.smilies); // undefined !!! 

EDIT: I use Chrome if it changes anything ...

http://code.google.com/chrome/extensions/content_scripts.html says:

Content scripts are executed in a special environment called Isolated World. They have access to the DOM of the page to which they are entered, but not to any JavaScript variables or functions created by this page. It looks at each script content as if there was no other JavaScript running on the page on which it is running.

This is about Chrome extensions, but I think this is also a story with Usercripts too?

Thanks, Rob W. Therefore, a working code for people who need it:

 var scriptText = "smilies = true;"; var rwscript = document.createElement("script"); rwscript.type = "text/javascript"; rwscript.textContent = scriptText; document.documentElement.appendChild(rwscript); rwscript.parentNode.removeChild(rwscript); 
+6
source share
1 answer

In Content Scripts (Chrome Extensions), there is a strict separation between the global page window object and the contents of the script global object.

Final script content code:

 // This function is going to be stringified, and injected in the page var code = function() { // window is identical to the page window, since this script is injected Object.defineProperty(window, 'smilies', { value: true }); // Or simply: window.smilies = true; }; var script = document.createElement('script'); script.textContent = '(' + code + ')()'; (document.head||document.documentElement).appendChild(script); script.parentNode.removeChild(script); 
+20
source

Source: https://habr.com/ru/post/915107/


All Articles