如何在chrome扩展中的后台脚本之间传递一个变量值 [英] How to pass a variable value between background scripts in chrome extensions

查看:152
本文介绍了如何在chrome扩展中的后台脚本之间传递一个变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个谷歌浏览器扩展。我为我的后台JavaScript文件(example.js)中的一个变量设置了一个值。我需要访问或传递这个值到另一个backgound JavaScript文件(extjs.js)。我该怎么做?有没有全球变量的概念?我的浏览器控制台没有收到任何错误。



我的manifest.json

 <$ 
name:Example,
version:31.0.1650.57,
manifest_version:2,
background:{
scripts:[common.js,example.js,extjs.js]
},
content_scripts:[
{
matches:[< all_urls>],
js:[myscript.js]
}
]
}

我的example.js

  function trial()
{
/ ...某些功能../
var result = data;
}

我的extjs.js

 警报(结果); 

我知道我错过了一些东西。



问候,
Nikhil

解决方案

所有后台脚本共享相同的JS上下文,所以在其中一个脚本中声明的任何变量/函数都可用于所有其他脚本(加载顺序当然起作用)。



当指定一个或多个后台脚本时,Chrome会自动创建一个最小的HTML页面,并在主体中插入一些< script> 标签。例如。您自动生成的背景页面应该如下所示:

 < html> 
< head>< / head>
< body>
< script src =common.js>< / script>
< script src =example.js>< / script>
< script src =extjs.js>< / script>
< / body>
< / html>






你可以看看你的背景页面导航到 chrome:// extensions 并检查开发者模式复选框。然后,在每个扩展名下面都有标有背景页面的链接,您可以点击打开您的后台页面的DevTools控制台。



$ hr

UPDATE:



我只是注意到你正试图访问一个函数局部变量(在我们可以从全局上下文(这是不可能的)试用()函数)。
$ b

由于 var result trial()函数中定义,它不能在函数范围外访问 。 (即你无法从 example.js 中引用它。)



你需要改变您的代码如下所示:

example.js:

  var outer_result = 0; 
函数trial(){
var inner_result = 1; //< - 这是一个局部变量
outer_result = 2; //< - 这是一个全局变量
}
// inner_result`在函数

$ b之外是未定义的
$ b

extjs.js:

  console.log(outer_result) ; //<  - '0',因为`trial()`还没有被调用
console.log(inner_result); //< - 'undefined',因为它永远不会被定义

trial(); //< - 执行`trial()`函数

console.log(inner_result); //< - 'undefined',因为它是本地函数
console.log(outer_result); //< - '2',因为`trial()`修改了它


I am developing a google chrome extension. I have a value set for a variable in one of my background javascript files(example.js). I need to access or pass this value to another backgound javascript file(extjs.js). How do I do it? Is there a global variable concept? I am not getting any error in my browser console.

my manifest.json

{
"name": "Example",
"version": "31.0.1650.57",
"manifest_version": 2,
"background":{
"scripts":["common.js","example.js","extjs.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["myscript.js"]
}
]
}

my example.js

function trial()
{
/... some functionality../
var result = data;
}

my extjs.js

alert(result);

I know I am missing something.

Regards, Nikhil

解决方案

All background scripts share the same JS context, so any variable/function declared in one of the scripts is available to all the others (the order of loading plays a role of course).

When specifying one or more background scripts, Chrome automatically creates a minimal HTML page and inserts some <script> tags in the body. E.g. your automatically generated background page should look something like this:

<html>
    <head></head>
    <body>
        <script src="common.js"></script>
        <script src="example.js"></script>
        <script src="extjs.js"></script>
    </body>
</html>


You can take a look at your background page by navigating to chrome://extensions and checking the Developer mode checkbox. Then, under each extension there is link labelled "background page", which you can click to open a DevTools console of your background page.


UPDATE:

I just notice you are trying to access a function-local variable (defined in trial() function) from the global context (which is not possible).

Since var result is defined inside the trial() function, it is not accessible outside the scope of the function. (I.e. you won't be able to reference it from example.js either.)

You need to change your code like this:

example.js:

var outer_result = 0;
function trial() {
    var inner_result = 1;   // <-- this is a local variable
    outer_result = 2;       // <-- this is a global variable
}
// `inner_result` is undefined outside the function

extjs.js:

console.log(outer_result);   // <-- '0' since `trial()` is not invoked yet
console.log(inner_result);   // <-- 'undefined' since it is never defined

trial();   // <-- executing `trial()` function

console.log(inner_result);   // <-- 'undefined' since it is function-local
console.log(outer_result);   // <-- '2' since `trial()` modified it

这篇关于如何在chrome扩展中的后台脚本之间传递一个变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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