Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension: Access to variables of a background.js from a popup.js

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?

like image 963
dortonway Avatar asked Mar 22 '13 14:03

dortonway


1 Answers

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.

like image 116
Ryan Avatar answered Nov 07 '22 07:11

Ryan