Chrome 扩展程序全局变量

Chrome extension global variable

本文关键字:全局变量 程序 扩展 Chrome      更新时间:2023-09-26

我在Chrome Extension上查看了清单ver 2的其他全局变量问题,但一无所获。

假设我有 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,我怎么能得到它?

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

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

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

希望对您有所帮助!