Access global scope in frame script

Holy hell doing Services.scriptloader.loadSubScript('...', this) is extremely bad. Because it is writing into the content frame message manager. Which is basically a shared scope among all addons. Wow very very bad. I wrote two addons using this technique, and they clashed hard with each other because in both addons I had a var named core so whichever one registered 2nd in that tab would override the first ones core.

Gosh this stinks so much how come we can’t get the global environment in the framescript.

The frame script environment is described here. In particular:

All the frame scripts running in a tab share this global. However, any top-level variables defined by a script are not stored on the global: instead, top-level variables are stored in a special per-script object that delegates to the per-tab global. This means you don’t have to worry about global variables you define conflicting with global variables defined by another frame script. You can still access the global directly via this.

So you can act like you have your own sandbox most of the time but if you deliberately access the frame manager itself then you’re in a shared environment. If you want to stay within your own sandbox you can load the subscript within a global object that you declare, and put everything “public” as a property of that namespace object.

I’m still not sure what your complaint is. What “global environment” do you want? Your latest message seems to be complaining that you got the global environment in the frame script and that was a big problem.

1 Like

8416 from IRC helped me phrase my question better. I am trying to not get the global, but “the top level variable scope”. In window and bootstrap the top level variable scope is same as global because the vars are bound to the global.

Thanks so much for sticking through this with me! Apparently there is a way to get the top level global scope with this but it is very bad for unloading framescripts, so I’m just going to not go that way and just work around it with setting a scope like this:

var myscope = {}

And then load everything into that.