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.