New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GM_addStyle cause window.Array become undefined #1735

Closed
ayanamist opened this Issue Apr 27, 2013 · 5 comments

Comments

Projects
None yet
3 participants
@ayanamist
Copy link

ayanamist commented Apr 27, 2013

Please see this script.
https://gist.github.com/ayanamist/5471650/8dd05392cb2c3c24af83bafd330f7b73c295a67e
Console output is undefined, and in fact it behaves as undefined too which affects my other scripts.
If i drop the line using GM_addStyle, everything returns normal.
In fact, not only Array become undefined, but also other things like Math.
I'm quite curious about why this could happen.

@ayanamist

This comment has been minimized.

Copy link
Author

ayanamist commented Apr 27, 2013

OK, now i know what happens. Only this can be used to access to global objects, window is not guaranteed and undocumented in MDN.

@cletusc

This comment has been minimized.

Copy link
Contributor

cletusc commented Apr 29, 2013

When using GM_* methods, the script is running in a sandbox which means window.ANYTHING will be undefined. When referencing Array, Math or any other JS object (as in, the ones available in JS natively, not the page defined variables), just use their name without window, so window.Array becomes Array, window.Math becomes Math and so on or switch over to this.Array, but I find it easier to understand without it. The only time you should actually use window is if you need page variables and in that case, you should use @grant none.

@arantius

This comment has been minimized.

Copy link
Collaborator

arantius commented Apr 29, 2013

the script is running in a sandbox which means window.ANYTHING will be undefined

Not strictly true. The sandbox global object (prototypically) inherits from the window object (and is indeed called this), and is also given a 'window' property to match. But I haven't yet had time to look into this and really understand what's going on.

@ayanamist

This comment has been minimized.

Copy link
Author

ayanamist commented May 2, 2013

@arantius So can you pass this as window into sandbox in order to guarantee the consistent behavior of window object?

@arantius

This comment has been minimized.

Copy link
Collaborator

arantius commented May 2, 2013

GM_addStyle is unrelated. My above comments are not correct because I didn't understand the whole problem.

// ==UserScript==
// @grant GM_log
// ==/UserScript==
console.log(window.Array);
console.log(Array);

This script has the same behavior. As soon as you @grant anything, you go into legacy security-wrapper-enabled mode. Its window object is missing lots of properties, and it has been for years. You'll notice that the second log works as expected however.

@arantius arantius closed this May 2, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment