Chrome扩展程序全局变量 [英] Chrome extension global variable

查看:1142
本文介绍了Chrome扩展程序全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过Chrome扩展的其他全局变量问题,但是没有发现任何内容。

假设我有2个文件:$ / $> $ / $> b
$ b

  // 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 ,但我可以'如果我不允许更改VarWithGlobe.js,那么如何获取它?

$ t在global.myVariable中访问第二个文件,因为它是未定义的。

b $ b

解决方案

内容脚本有自己的执行上下文(它甚至不同于它们执行的网页上下文)。唯一的方法是将消息传递给/从背景页面与变量。检查这个答案代码示例


I've looked at other global variable questions on Chrome Extension with manifest ver 2, but found nothing.

Assume I have 2 files:

// content.js

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

and

// VarWithGlobe.js

var withGlobe = withGlobe || { };

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

WithGlobe.fromVariable = WithGlobe;

both of them are added to web_accessible_resources, content_scripts but I can't access global.myVariable in second file since it is undefined.

How can I get it if I'm not allowed to change VarWithGlobe.js?

解决方案

Content scripts have own execution context (it is even different from the webpage context they are executing in). The only way is to pass messages to/from background page with variable. Check this answer for code samples

这篇关于Chrome扩展程序全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆