【问题标题】:Chrome extension: Access to variables of a background.js from a popup.jsChrome 扩展:从 popup.js 访问 background.js 的变量
【发布时间】:2013-03-12 10:37:22
【问题描述】:
如果我使用chrome.extension.getBackgroundPage(),我可以像这样访问 background.js 的变量:
background.js:
var transfer = 'some text';
popup.js:
chrome.extension.getBackgroundPage().transfer
但是this 说我只得到一个窗口对象(但也许“窗口”之前的“JavaScript”意味着什么......)。如何访问后台变量?
【问题讨论】:
标签:
javascript
google-chrome-extension
【解决方案1】:
是的,window 前面的“javascript”这个词确实意味着它返回了 javascript 文件(页面)background.js
为了便于访问我的 popup.js 文件顶部,我这样做:
var background = chrome.extension.getBackgroundPage(); //do this in global scope for popup.js
那么你可以这样做:
background.transfer;
由于您通常需要经常访问背景页面,这只会让生活变得更轻松。