views:

261

answers:

1

Hi All,

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?

+3  A: 

Hello,

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://code.google.com/chrome/extensions/devguide.html

Mohamed Mansour