Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
What is the proper way of updating values using chrome.storage API?

The code below is not doing what I expect:

chrome.runtime.onStartup.addListener(() => {
  chrome.storage.local.set({
    foo: {},
  });
});

chrome.tabs.onUpdated.addListener(async () => {
  const foo = await chrome.storage.local.get("foo");
  foo[Math.floor(Math.random() * 10).toString()] = "data";
  chrome.storage.local.set({
    foo: foo,
  });
});

It results in this data:

{
    "2": "data",
    "foo": {
        "5": "data",
        "foo": {
            "3": "data",
            "foo": {
                "2": "data",
//...
// (the rest omitted deliberately for not occupying too much space)

I don't get it, does this result makes sense? Probably I'm thinking the wrong way about it, but I can't find out what's my misunderstanding. If I'm setting foo which is a key already available in the object, shouldn't it be updated (and not get nested)? My end goal is a structure like this(please forget about the Math.random, that was just for the example):

{
  foo: {
    "AB4": "data1",
    "5C2": "data2"
  }
}

and I want it to turn into something like this after update:

{
  foo: {
    "AB4": "data1",
    "5C2": "data2",
    "DA1": "data3"
  }
}
about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda