Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
Storing default options in a Chrome extension

I'm getting an error when trying to access stored option variables for the first time after installation, both in content scripts and in a service worker: Uncaught (in promise) undefined

Here is the code that manages user options:

function save_options() {
  var mykeySetting = document.getElementById('mycheckbox').checked;
  chrome.storage.sync.set({
    mykey: mykeySetting
  }, function() {
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'Options saved.';
    setTimeout(function() {
      status.textContent = '';
    }, 750);
  });
}


function restore_options() {
  chrome.storage.sync.get({
    mykey: true
  }, function(items) {
    document.getElementById('mycheckbox').checked = items.mykey;
  });
}

document.addEventListener('onInstalled', restore_options);
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('onInstalled', save_options);
document.getElementById('save').addEventListener('click', save_options);
<!DOCTYPE html>
<html>

<head>
  <title>My add-on</title>
</head>
<link rel="stylesheet" href="options.css" />

<body>
  <label>
  <input type="checkbox" id="mycheckbox">
  Option 1
</label>

  <div id="status"></div>

  <br>

  <button id="save">Save</button>

  <script src="options.js"></script>
</body>

</html>

After I hit save button in the options dialogue the errors go away, even if I reload the extension. In other words, the errors appear only after loading the extension for the first time, till hitting the save button.

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda