Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

82
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!