views:

1162

answers:

4

I'm just fiddling around with user scripts in chrome right now, so please bear with my potential ignorance/idiocy.

In the page I'm writing a script for, there is a <script> element that declares a variable x. Does this mean that, in my user script, I can just access x from the global namespace?

For example, if the only line in my userscript is alert(x);, should that work as expected (assuming x is a String)? I understand chrome doesn't support unsafewindow, but for some reason I'm finding it impossible to figure out how to mimic the functionality. Is it even possible?

A: 

Why do you need unsafeWindow? It's not a recommended practice, even in Greasemonkey/Firefox world.

Ian
+2  A: 

contentWindow avalible in Chrome 3, but removed in Chrome 4. Only possible solution for Chrome 4:

location.href="javascript:(function(){ alert('Hello'); })()"
NV
A: 

ok heres an idea you can inject the script using the address bar...

javascript:var ElEm = document.createElement("script");ElEm.src='[path_to_script]';document.body.appendChild(ElEm);

then you can run whatever you want in the window with your javascript

Carter Cole
oh and if the variable is global for the page you should be able to read it with your javascript
Carter Cole
A: 

@NV, Excellent ! Thanks.

@Carter Cole, how can you put javascript code instead of adding an url with the same aproach ?

sputnick