In a Chrome Extension I am developing, I want to access a data structure created and maintained in a background page when the popup is clicked. Unfortunately, I am new to Javascript and Chrome extension development in general, can you tell me how to do that? Does this involve message passing between the popup and the background page? Thanks.
In a Chrome Extension I am developing, I want to access a data structure created and maintained in a background page when the popup is clicked. Unfortunately, I am new to Javascript and Chrome extension development in general, can you tell me how to do that? Does this involve message passing between the popup and the background page? Thanks.
Share Improve this question asked Jun 11, 2012 at 20:39 RaffoRaffo 1,6526 gold badges24 silver badges42 bronze badges1 Answer
Reset to default 9You can write three files like this to access a data structure in the background.html from your popup.html:
//in popup.html
<script type="text/javascript" src="mainscript.js"></script>
<!-- JavaScript and HTML must be in separate files for security. -->
//in mainscript.js
chrome.extension.getBackgroundPage().data = 'your data';
//in background.html
<script type="text/javascript">
var data;
</script>
and you need a manifest.json like this (maybe use browser_action instead of page_action):
....
,
"background_page": "background.html",
"page_action": {
"default_icon": "your_icon.ico",
"default_title": "Your title",
"default_popup": "popup.html"
},
....
edit: for message passing in chrome extensions see these functions
http://code.google./chrome/extensions/extension.html#method-sendRequest
http://code.google./chrome/extensions/extension.html#event-onRequest
and this useful description:
http://code.google./chrome/extensions/messaging.html