diff --git a/background.html b/background.html index 52d2b0b..d298792 100644 --- a/background.html +++ b/background.html @@ -7,9 +7,10 @@
Status:
diff --git a/lib/options.js b/lib/options.js index 8c6bea5..e19ba7c 100644 --- a/lib/options.js +++ b/lib/options.js @@ -21,6 +21,36 @@ var OptionsManager = function() { this.opts = null; }; +OptionsManager.prototype.types = { + "get": { + "text": function(elem) { + return elem.value; + }, + "password": function(elem) { + return elem.value; + }, + "checkbox": function(elem) { + return elem.checked; + } + }, + "set": { + "text": function(elem, val) { + elem.value = val; + }, + "password": function(elem, val) { + elem.value = val; + }, + "checkbox": function(elem, val) { + console.log(val); + if (!val) { + elem.removeAttribute('checked'); + } else { + elem.setAttribute('checked'); + } + } + } +}; + OptionsManager.prototype.init = function() { var saveOpts = function(elem) { return function() { @@ -55,7 +85,7 @@ OptionsManager.prototype.save = function() { if (this.opts !== null) { for (i = this.opts.length - 1; i >= 0; --i) { - data[this.opts[i].id] = this.encrypt(this.opts[i].value); + data[this.opts[i].id] = this.getElemValue(this.opts[i]); } this.storage.set(data, notif(this.status)); } @@ -69,7 +99,7 @@ OptionsManager.prototype.restore = function() { for (i in items) { el = document.getElementById(i); if (el !== null) { - el.value = elem.decrypt(items[i]); + elem.setElemValue(el, items[i]); } } }; @@ -78,6 +108,25 @@ OptionsManager.prototype.restore = function() { this.storage.get(null, rst(this)); }; +OptionsManager.prototype.getElemValue = function(elem) { + var val = null, + type = elem.getAttribute('type'); + + if (typeof this.types.get[type] !== "undefined") { + val = this.types.get[type](elem); + } + + return this.encrypt(val); +}; + +OptionsManager.prototype.setElemValue = function(elem, val) { + var type = elem.getAttribute('type'); + + if (typeof this.types.set[type] !== "undefined") { + this.types.set[type](elem, this.decrypt(val)); + } +}; + OptionsManager.prototype.encrypt = function(value) { return value; };