Chrome扩展程序:通过popup.js访问background.js的变量 [英] Chrome extension: Access to variables of a background.js from a popup.js

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

问题描述

如果我使用 chrome.extension.getBackgroundPage(),我可以像这样访问background.js的变量:

background.js:

  var transfer ='一些文字'; 

popup.js:

  chrome.extension.getBackgroundPage()。transfer 

这个说我只得到一个窗口对象(但也许'JavaScript'之前'窗口'意味着某事......)。如何访问背景变量?

解决方案

是的,窗口前的'javascript'字样意味着它返回的JavaScript文件页面)background.js



为了便于访问我的 popup.js 文件的顶部,我这样做:

  var background = chrome.extension.getBackgroundPage(); //在全局范围内为popup.js执行此操作

然后您可以执行此操作:

  background.transfer; 

由于您通常必须访问您的后台页面,因此这会让生活更轻松。 / p>

If I'm using chrome.extension.getBackgroundPage(), I can access variables of the background.js like this:

background.js:

var transfer = 'some text';

popup.js:

chrome.extension.getBackgroundPage().transfer

But this says I get only a window object (but maybe 'JavaScript' before 'window' means something...). How can I access background variables?

解决方案

Yes, the word 'javascript' before window does mean that its returning the javascript file(page) background.js

For ease of access at the top of my popup.js file I do this:

var background = chrome.extension.getBackgroundPage(); //do this in global scope for popup.js

then you can do this:

background.transfer;

Since you typically have to access your background page a lot, this just makes life easier all around.

这篇关于Chrome扩展程序:通过popup.js访问background.js的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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