我查看了带有 list 版本 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怎么获取?
最佳答案
内容脚本有自己的执行上下文(甚至不同于它们在其中执行的网页上下文)。唯一的方法是使用变量向/从后台页面传递消息。检查this answer代码示例
关于javascript - Chrome 扩展程序全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18840929/