Chrome扩展程序全局变量

时间:2013-09-17 03:38:52

标签: javascript google-chrome google-chrome-extension

我查看了有关清单版本2的Chrome扩展程序的其他全局变量问题,但未找到任何内容。

假设我有2个文件:

// content.js

var myVariable = myVariable(someDiv);
var myVarWithGlobe = VarWithGlobe.fromVariable(myVariable);

// VarWithGlobe.js

var withGlobe = withGlobe || { };

withGlobe.WithGlobe = (function(global) {
    var myLocalVar = global.myVariable;
....

WithGlobe.fromVariable = WithGlobe;

它们都被添加到 web_accessible_resources,content_scripts 但我无法访问第二个文件中的global.myVariable,因为它未定义。

如果我不允许更改VarWithGlobe.js,我怎么能得到它?

3 个答案:

答案 0 :(得分:4)

内容脚本具有自己的执行上下文(甚至与它们正在执行的网页上下文不同)。唯一的方法是使用变量向/从后台页面传递消息。检查this answer代码示例

答案 1 :(得分:0)

首先包含全局的文件,它应该在那里。

答案 2 :(得分:-2)

在Chromium版本35.0.1916.114中,可能在所有最新的Chrome版本中,您可以在Chrome控制台上进行测试时存储全局变量。

例如,如果您对代码中的变量使用 console.log(),当您在浏览器控制台上看到它时,可以通过右键单击它来存储它/ strong>并选择"存储为全局变量"

我希望它可以帮到你!