For a simple Chrome extension I've made that finds and replaces a text string I'm trying to add an option to add an asterisk after the replaced text. I have a bloolean variable showAsterisk that gets set in options.js from the options.html dialogue. I can see from the console that the value is being set properly, however it is not being retrieved with chrome.storage.sync.get. Not sure what I'm doing wrong. Here is the relevant code:
manifest.json
"permissions": ["storage"],
"options_ui": {
"page": "options.html",
"chrome_style": true
},
options.html
<h1>Options</h1>
<p><input type="checkbox" id="showAsterisk"> Add an asterisk after each instance of replaced text.</p>
<button id="save">Save</button>
<script src="options.js"></script>
options.js
function save_options() {
var showAsterisk = document.getElementById('showAsterisk').checked;
chrome.storage.sync.set({ showAsterisk: showAsterisk }, function() {
window.close();
});
}
function restore_options() {
chrome.storage.sync.get({ showAsterisk: false }, function(items) {
document.getElementById('showAsterisk').checked = items.showAsterisk;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);
content.js
var elements = document.getElementsByTagName("*");
var showAsterisk = false;
chrome.storage.sync.get("showAsterisk", function(items) {
if(typeof items.showAsterisk !== "undefined") showAsterisk = items.showAsterisk;
})
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
if (showAsterisk === true) {
var replacedText = text.replace(/Moda Center/gi, "Rose Garden*");
}
if (showAsterisk === false) {
var replacedText = text.replace(/Moda Center/gi, "Rose Garden");
}
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}