Note that there are some explanatory texts on larger screens.

plurals
  1. POPass multiple variables to content script chrome
    primarykey
    data
    text
    <p>I'm writing my first chrome extension, and I just started this a few hours ago. Everything worked well when it was hard-coded. Essentially, I'm filling an 8-page form. Each page of the form corresponds to a separate content script. The content scripts used to look something like this: (finding the fields is a bit more difficult, since they're not standard, but I'm not worried about that..that part works)</p> <pre><code>var first_name = 'John'; var last_name = 'Doe'; ... ... document.getElementById('first_name').value = first_name; document.getElementById('last_name').value = last_name; </code></pre> <p>This worked fine (for me), but I want to distribute it to a few people, none of whom would/could use the extension from source code, editing the variables directly. So I made an 'Options' page, using this as an example: <a href="https://developer.chrome.com/trunk/extensions/options.html" rel="noreferrer">https://developer.chrome.com/trunk/extensions/options.html</a></p> <p>The options page is a mockup of the form to be filled, and each user can enter their defaults here. My save_options() function basically does (pseudocode):</p> <pre><code>foreach(fields as field) { localStorage[field] = document.getElement....(field); } </code></pre> <p>This works fine. It saves properly, displays the stored values, etc.</p> <p>The problem comes when I try to access the variables from the content script. From what I've read, this can't be done (directly), and there are various methods online that show how to do this. I want to do something like this (content script):</p> <pre><code>var first_name = localStorage["first_name"]; var last_name = localStorage["last_name"]; ... ... document.getElementById('first_name').value = first_name; document.getElementById('last_name').value = last_name; </code></pre> <p>My questions are:</p> <p>1) For those of you who have dealt with Chrome extensions/localstorage/chrome.storage/etc, is there a 'best' way to do this?</p> <p>2) What is the most efficient way to set all the variable names/values? I'd prefer a loop (like above) over setting each field with a separate request. I'm averaging 10 fields per page.</p> <p>(I could probably use a long, messy form of <a href="http://developer.chrome.com/extensions/messaging.html" rel="noreferrer">http://developer.chrome.com/extensions/messaging.html</a>, but I'm hoping for a more efficient/extensible/elegant solution)</p> <p>Any input is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload