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

235
Visualizações
Getting Chrome Storage Value from Function (Chrome Extension)

First Post, so forgive me for any general practices that I haven't followed.

Also very new to Chrome extension and Javascript so apologies for my noob question

I have the following code but getting the console is say undefined

    function getFromStorage(key) {
    chrome.storage.sync.get(key, function (r) {
        const returnVal = r[key]
        console.log(returnVal)
        return returnVal
    })
}

chrome.contextMenus.onClicked.addListener((selectionText) => {
    var Val = getFromStorage("value")
    console.log(Val)
});

But when I run the chrome.storage.sync.get in the listener, it works fine. I have a few values from storage that I want to grab so thought a function would be better.

btw, using Manifest V3

Thank you in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that your getFromStorage doesn't return anything. It just calls an asynchronous API, then the callback runs and returns a value into the internals of the chrome API, which simply ignores this value.

Since chrome.storage returns a Promise in modern Chrome you can just await it directly.
Note that the function should be declared as async to allow using await inside.

async function getFromStorage(key) {
  return (await chrome.storage.sync.get(key))[key];
}

chrome.contextMenus.onClicked.addListener(async info => {
  let Val = await getFromStorage('value');
  console.log(Val);
});
about 4 years ago · Juan Pablo Isaza Relatório
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