如何访问先前 YUI.use() 调用中定义的函数

发布于 2024-11-16 18:10:59 字数 444 浏览 1 评论 0原文

[我是 YUI 新手]

我正在编写一个 Chrome 扩展程序,需要更改使用 YUI3 框架创建的网页的内容。

我已经确定,加载加载后在页面中运行的 JavaScript 的扩展必须调用先前在 YUI.add() 调用中定义的函数。

运行的原始 YUI 代码是这样的:

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

是否有可能在这段代码运行后,我可以访问 c.theObject 的函数?

(我知道这可能违背 YUI3 良好的沙箱机制,但是这就是我在这里完成工作所需要的)。

[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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2024-11-23 18:10:59

您可能会遇到问题,因为每当创建 YUI() 实例时,它都会为您构建一个新的沙箱。除了少数例外,YUI 模块完全由其沙箱上下文包围。例如:

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

如果从未将其分配给沙箱函数范围之外的变量,则您很可能无法访问所需的 theObject特定实例。如果 theObject 的任何实例都可以,您只需调用 YUI API 并获取您自己的版本即可使用。

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.

家住魔仙堡 2024-11-23 18:10:59

一种方法是在“使用”YUI() 实例后捕获它。像这样:

YUI().add("uuu", function (c) {
    c.theObject = 'foo';
})

var yInstance = YUI().use("uuu", function (c) {
    c.theObject = 'booyaa';
})

yInstance.use('uuu',function(c){
console.log(c.theObject)
})
    // booyaa

One way to do it is to capture the YUI() instance after you 'use' it. Like this:

YUI().add("uuu", function (c) {
    c.theObject = 'foo';
})

var yInstance = YUI().use("uuu", function (c) {
    c.theObject = 'booyaa';
})

yInstance.use('uuu',function(c){
console.log(c.theObject)
})
    // booyaa
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文