Reputation: 44444
I am trying out a simple Google Chrome extension, in which I need to communicate between the options page and the background page for getting/setting the options.
I have tried chrome.extension.sendRequest(..) and chrome.extension.onRequest.addListener(..) but with no success!
Am I missing something? Or should I post my code?
Upvotes: 11
Views: 3896
Reputation: 40159
In you background.html, you could have something like this:
<html>
<script>
settings = {
get foo() {
return localStorage['foo'];
},
set foo(val) {
localStorage['foo'] = val;
}
}
</script>
</html>
Now in your options page, you can simply use chrome.extensions.getBackgroundPage. For example in options.html:
<html>
<head>
<script>
var bkg = chrome.extension.getBackgroundPage();
function saveOptions() {
bkg.settings.foo = 'bar';
}
function restoreOptions() {
document.getElementById('foo').value = bkg.settings.foo;
}
</script>
</head>
<body onload="restoreOptions()">
<form onsubmit="return false;">
<input id="foo" type="text" />
<button onclick="saveOptions();">Save</button>
</form>
</body>
</html>
Remember one thing, the dev guide is your best friend :) http://developer.chrome.com/extensions/devguide
Upvotes: 16
Reputation: 1
yeah, that's not gonna work:
<!--
- JavaScript and HTML must be in separate files: see our Content Security
- Policy http://developer.chrome.com/extensions/contentSecurityPolicy.html
-->
Upvotes: 0