yui3

How to access a function defined in a previous YUI.use() call


[I'm a YUI newbie]

I'm writing a Chrome extension that needs to change the contents of a web page created using the YUI3 framework.

I've identified that the extension, which injects javascript that runs in the page after it is loaded, must call a function that was previously defined in a YUI.add() call.

The original YUI code that runs is something like this:

YUI.add("uuu", function (c) {
    ...
    c.theObject = niceStuff;
}
...
YUI().use("uuu", function (c) {
    c.theObject.doSomething();
}

Is it possible that after this code runs, I can access a function of c.theObject?

(I understand this might go against YUI3's nice sandbox mechanism, but it's what I need to get the job done here).


Solution

  • You might have problems because any time a YUI() instance is created, it builds you a new sandbox. With a few exceptions, YUI modules are completely boxed by their sandbox context. For example:

    YUI().use('node', function(Y1) {
        YUI().use('node', function(Y2) {
            assert(Y1.Node === Y2.Node) // fails!
        });
    });
    

    It's very possible that you may not be able to access the specific instance of theObject that you need, if it's never assigned to a variable outside the sandbox function scope. If any instance of theObject will do, you can just call into the YUI API and get your own version to play with.