4

我在清单版本 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,我怎么能得到它?

4

2 回答 2

5

内容脚本有自己的执行上下文(它甚至与它们正在执行的网页上下文不同)。唯一的方法是使用变量将消息传递到/从背景页面传递。检查此答案以获取代码示例

于 2013-09-17T15:45:05.823 回答
-3

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

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

我希望它可以帮助你!

于 2014-05-23T12:47:29.040 回答