0

I read Google Chrome Extensions Developer's Guide carefully, it told me to save options in localStorage, but how is content_scripts able to get access to these options?

Sample:

I want to write a script for a couple of domains, and this script should share some options on these domains.

content_scripts:

//Runs on several domains
(function(){
    var option=getOptions();
    //Get options which have been set in options.html
    if(option){
        doSome();
    }
})

option_page:

<html>
    <head>
        <title>My Test Extension Options</title>
    </head>
    <body>
        option: <input id="option" type="text" /><br />
        <input id="save" type="submit" /><br />
        <span id="tips">option saved</span>
        <script type="text/javascript">
            (function(){
                var input=document.getElementById('option');
                var save=document.getElementById('save');
                var tips=document.getElementById('tips');
                input.value=localStorage.option||'';
            // Here localStorage.option is what I want content_scripts to get.
                function hideTips(){
                    tips.style.visibility='hidden';
                }
                function saveHandler(){
                    localStorage.option=input.value||'';
                    tips.style.visibility='visible';
                    setTimeout(hideTips,1000);
                }
                hideTips();
                save.addEventListener('click',saveHandler);
            })();
        </script>
    </body>
</html>
OpenGG
  • 4,345
  • 2
  • 24
  • 31

1 Answers1

2

I would think you could use the chrome.extensions.* API to create a line of communication to a background page that is running under your extension ID, thus giving you local storage.

I think this is possible because the Content Script docs specify that the chrome.extensions* API's are available to content scripts. But I have never tried this.

You would then just have to send messages from the background page to the content script when a connection is made. You could even send one message with all the settings in a literal object.

Here is an example of creating two way communication I wrote about earlier. You could implement this or create a custom solution but I think this is how you would achieve what you are looking for.

Community
  • 1
  • 1
jjNford
  • 5,170
  • 7
  • 40
  • 64
  • Oh god, I have to write several files just to save some options, that's why I prefer greasemonkey on firefox. Thank you anyway. – OpenGG Mar 02 '12 at 23:57
  • @Rufus naw, this will be easy, just basic javascript. You can even copy the code that I gave a link too. Create some prototype methods to send your local storage as stringified object. Then in your content script just parse it out and boom you are ready, based on what I have given you it should only be a couple lines of code. – jjNford Mar 03 '12 at 00:02
  • Thank you, I know that, and I really appreciate your help. But honestly I just want to write some simple userscripts, and host them on userscripts.org . If a Chrome extension is so complicated to write, then it will not be a good idea to me. – OpenGG Mar 03 '12 at 01:13
  • Chrome extensions are easy to write considering how much power they give you for so little effort (I should know, Im not what youd call a genius ;) ). But in this case it is a little awkward because of the security stuff on content scripts. You should look at an extension for Chrome called TamperMonkey that will allow you to do things more the FF way, I think youd like it alot. And on getting settings, it can be done without a background page as I figured out today.... http://forum.valorsolo.com/viewtopic.php?f=36&t=375 ..have fun and dont give up on Chrome so quick :P – PAEz Mar 04 '12 at 10:58