I am trying to follow a code example for a newer style of creating parameter pages . I added the options.html and options.js file with the exact contents that they list on this page. I also added the options_ui section to my manifest.json (and removed the trailing comma after chrome_style: true ').
When I do this, I can open the options window for the extension, but the save and load functions do not work. I tried adding warnings to some console logs, and they don't seem to be executing either. I cannot find any errors or warnings anywhere, but I just cannot figure out how to execute JavaScript. Does anyone know what I need to do differently?
Edit: I am adding source files here to make it easier for people to scan through them and for posterity.
manifest.json
{
"manifest_version": 2,
"name": "My extension",
"description": "test extension",
"version": "1.0",
"options_ui": {
"page": "options.html",
"chrome_style": true
}
}
options.html
<!DOCTYPE html>
<html>
<head>
<title>My Test Extension Options</title>
<style>
body: { padding: 10px; }
</style>
</head>
<body>
Favorite color:
<select id="color">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="yellow">yellow</option>
</select>
<label>
<input type="checkbox" id="like">
I like colors.
</label>
<div id="status"></div>
<button id="save">Save</button>
<script src="options.js"></script>
</body>
</html>
options.js
function save_options() {
var color = document.getElementById('color').value;
var likesColor = document.getElementById('like').checked;
chrome.storage.sync.set({
favoriteColor: color,
likesColor: likesColor
}, function() {
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}
function restore_options() {
chrome.storage.sync.get({
favoriteColor: 'red',
likesColor: true
}, function(items) {
document.getElementById('color').value = items.favoriteColor;
document.getElementById('like').checked = items.likesColor;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);
source
share