javascript - Chrome 扩展程序全局变量

标签 javascript google-chrome google-chrome-extension

我查看了带有 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/

相关文章:

html - 没有 JS 的可点击 HTML/CSS 下拉菜单在 Firefox 中有效,但在 Chrome 中无效

eclipse - 如何在 Eclipse for Mac 中将 Chrome 设置为默认外部浏览器?

jQuery 触发器不更新 2 路绑定(bind)值

javascript - jQuery 隐藏 nextall 元素

javascript - 在javascript数组中插入项目并排序

javascript - 从 7 个输入条件中搜索以查看 json 文件

javascript - 在 Backbone.js 路由器中定义一个基本 url

javascript - Oracle 的 deployjava.js 在 Firefox/Chrome 中不再起作用。我还可以做些什么?

javascript - 内容脚本中的监听器

javascript - 如何使用 chrome 扩展程序保存页面