chrome.storage.local.get iterate simultaneously through keys and elements










0















First of all, sorry if the original question isn't clear enough,
I was struggling to define the exact problem I'm having.
I am making a Chrome extension which features a list of blank input boxes. I would like to save the value assigned in those input boxes using the chrome.storage.set method, and retrieve said values into their original input boxes when the popup is reopened.



So far, I have managed to store locally the values of the boxes using a loop, assigning each value a key depending of its order of iteration.



HTML



<input type="text" class="random" value="">
<input type="text" class="random" value="">
<input type="text" class="random" value="">
<button id="4">save</button>


JS



document.addEventListener('DOMContentLoaded', function() 
document.getElementById("4").addEventListener("click", save);
);
function save()
var id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
var inputValue= id[i].value;
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.set([key]: inputValue);
alert(key)




The problem comes when I try to retrieve each value and return it to its original input field. My solution was to create another loop which iterates through the input fields while retrieving the corresponding keys, but can't seem to make it work.



window.onload = () => 
const id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.get([key], (data) =>
if (data.key)
id[i].value = data.key;

);




How should I define the variables properly? Is there any other work around to achieve the same result?










share|improve this question






















  • Is the amount of input fields variable, or do you know their list in advance?

    – Xan
    Nov 14 '18 at 15:55











  • @Xan yes, the extension has a function to add/remove input boxes if needed

    – Akiro
    Nov 14 '18 at 16:01











  • BTW, the problem with your code is pretty much this: stackoverflow.com/questions/750486/…

    – Xan
    Nov 14 '18 at 16:02











  • You can get/set all items in just one API call, by the way.

    – wOxxOm
    Nov 14 '18 at 17:19











  • @wOxxom that would be helpful, how would you do that?

    – Akiro
    Nov 14 '18 at 18:40















0















First of all, sorry if the original question isn't clear enough,
I was struggling to define the exact problem I'm having.
I am making a Chrome extension which features a list of blank input boxes. I would like to save the value assigned in those input boxes using the chrome.storage.set method, and retrieve said values into their original input boxes when the popup is reopened.



So far, I have managed to store locally the values of the boxes using a loop, assigning each value a key depending of its order of iteration.



HTML



<input type="text" class="random" value="">
<input type="text" class="random" value="">
<input type="text" class="random" value="">
<button id="4">save</button>


JS



document.addEventListener('DOMContentLoaded', function() 
document.getElementById("4").addEventListener("click", save);
);
function save()
var id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
var inputValue= id[i].value;
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.set([key]: inputValue);
alert(key)




The problem comes when I try to retrieve each value and return it to its original input field. My solution was to create another loop which iterates through the input fields while retrieving the corresponding keys, but can't seem to make it work.



window.onload = () => 
const id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.get([key], (data) =>
if (data.key)
id[i].value = data.key;

);




How should I define the variables properly? Is there any other work around to achieve the same result?










share|improve this question






















  • Is the amount of input fields variable, or do you know their list in advance?

    – Xan
    Nov 14 '18 at 15:55











  • @Xan yes, the extension has a function to add/remove input boxes if needed

    – Akiro
    Nov 14 '18 at 16:01











  • BTW, the problem with your code is pretty much this: stackoverflow.com/questions/750486/…

    – Xan
    Nov 14 '18 at 16:02











  • You can get/set all items in just one API call, by the way.

    – wOxxOm
    Nov 14 '18 at 17:19











  • @wOxxom that would be helpful, how would you do that?

    – Akiro
    Nov 14 '18 at 18:40













0












0








0








First of all, sorry if the original question isn't clear enough,
I was struggling to define the exact problem I'm having.
I am making a Chrome extension which features a list of blank input boxes. I would like to save the value assigned in those input boxes using the chrome.storage.set method, and retrieve said values into their original input boxes when the popup is reopened.



So far, I have managed to store locally the values of the boxes using a loop, assigning each value a key depending of its order of iteration.



HTML



<input type="text" class="random" value="">
<input type="text" class="random" value="">
<input type="text" class="random" value="">
<button id="4">save</button>


JS



document.addEventListener('DOMContentLoaded', function() 
document.getElementById("4").addEventListener("click", save);
);
function save()
var id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
var inputValue= id[i].value;
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.set([key]: inputValue);
alert(key)




The problem comes when I try to retrieve each value and return it to its original input field. My solution was to create another loop which iterates through the input fields while retrieving the corresponding keys, but can't seem to make it work.



window.onload = () => 
const id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.get([key], (data) =>
if (data.key)
id[i].value = data.key;

);




How should I define the variables properly? Is there any other work around to achieve the same result?










share|improve this question














First of all, sorry if the original question isn't clear enough,
I was struggling to define the exact problem I'm having.
I am making a Chrome extension which features a list of blank input boxes. I would like to save the value assigned in those input boxes using the chrome.storage.set method, and retrieve said values into their original input boxes when the popup is reopened.



So far, I have managed to store locally the values of the boxes using a loop, assigning each value a key depending of its order of iteration.



HTML



<input type="text" class="random" value="">
<input type="text" class="random" value="">
<input type="text" class="random" value="">
<button id="4">save</button>


JS



document.addEventListener('DOMContentLoaded', function() 
document.getElementById("4").addEventListener("click", save);
);
function save()
var id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
var inputValue= id[i].value;
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.set([key]: inputValue);
alert(key)




The problem comes when I try to retrieve each value and return it to its original input field. My solution was to create another loop which iterates through the input fields while retrieving the corresponding keys, but can't seem to make it work.



window.onload = () => 
const id = document.querySelectorAll("input[type='text']");
for (i = 0; i < id.length; i++)
if (id.length > 0)
var key = "key"+i;
chrome.storage.local.get([key], (data) =>
if (data.key)
id[i].value = data.key;

);




How should I define the variables properly? Is there any other work around to achieve the same result?







loops google-chrome-extension local-storage storage






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 15:45









AkiroAkiro

81




81












  • Is the amount of input fields variable, or do you know their list in advance?

    – Xan
    Nov 14 '18 at 15:55











  • @Xan yes, the extension has a function to add/remove input boxes if needed

    – Akiro
    Nov 14 '18 at 16:01











  • BTW, the problem with your code is pretty much this: stackoverflow.com/questions/750486/…

    – Xan
    Nov 14 '18 at 16:02











  • You can get/set all items in just one API call, by the way.

    – wOxxOm
    Nov 14 '18 at 17:19











  • @wOxxom that would be helpful, how would you do that?

    – Akiro
    Nov 14 '18 at 18:40

















  • Is the amount of input fields variable, or do you know their list in advance?

    – Xan
    Nov 14 '18 at 15:55











  • @Xan yes, the extension has a function to add/remove input boxes if needed

    – Akiro
    Nov 14 '18 at 16:01











  • BTW, the problem with your code is pretty much this: stackoverflow.com/questions/750486/…

    – Xan
    Nov 14 '18 at 16:02











  • You can get/set all items in just one API call, by the way.

    – wOxxOm
    Nov 14 '18 at 17:19











  • @wOxxom that would be helpful, how would you do that?

    – Akiro
    Nov 14 '18 at 18:40
















Is the amount of input fields variable, or do you know their list in advance?

– Xan
Nov 14 '18 at 15:55





Is the amount of input fields variable, or do you know their list in advance?

– Xan
Nov 14 '18 at 15:55













@Xan yes, the extension has a function to add/remove input boxes if needed

– Akiro
Nov 14 '18 at 16:01





@Xan yes, the extension has a function to add/remove input boxes if needed

– Akiro
Nov 14 '18 at 16:01













BTW, the problem with your code is pretty much this: stackoverflow.com/questions/750486/…

– Xan
Nov 14 '18 at 16:02





BTW, the problem with your code is pretty much this: stackoverflow.com/questions/750486/…

– Xan
Nov 14 '18 at 16:02













You can get/set all items in just one API call, by the way.

– wOxxOm
Nov 14 '18 at 17:19





You can get/set all items in just one API call, by the way.

– wOxxOm
Nov 14 '18 at 17:19













@wOxxom that would be helpful, how would you do that?

– Akiro
Nov 14 '18 at 18:40





@wOxxom that would be helpful, how would you do that?

– Akiro
Nov 14 '18 at 18:40












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303924%2fchrome-storage-local-get-iterate-simultaneously-through-keys-and-elements%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303924%2fchrome-storage-local-get-iterate-simultaneously-through-keys-and-elements%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党