]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blobdiff - javascript/options.js
options: move unique functions in here
[chrome-ext/clearhistory-advance-fork.git] / javascript / options.js
index bd3a2275c1b341372d9bc3f7f7d0b61556d7fb42..e5d53ec5de7f1b5f2d13ad31e717b045a3aa6ceb 100644 (file)
@@ -7,6 +7,34 @@
 
 window.addEventListener('load', init, false);
 
+/**
+ * Pours the data in template string.
+ * @param {Object} datObject The data object to be filled in template string.
+ * @return {string} The new string created from template string and filled
+ *     with the given data.
+ */
+String.prototype.supplant = function(datObject) {
+  return this.replace(/{([^{}]*)}/g,
+    function(match, firstSubMatch) {
+      var replace = datObject[firstSubMatch];
+      return (typeof replace === 'string' || typeof replace === 'number') ?
+          replace : match;
+    });
+};
+
+/**
+ * Queries the DOM.
+ * @param {string} selector Selector to execute.
+ * @param {HTMLElement=} context HTMLElement to query (optional).
+ * @return {Array.<HTMLElement>} Array of matched elements (non-live).
+ */
+function $(selector, context) {
+  if (!(context && context instanceof HTMLElement)) {
+    context = document;
+  }
+  return Array.prototype.slice.call(context.querySelectorAll(selector));
+}
+
 /**
  * Initializes the i18n strings.
  * Loads values from localStorage and applies them to the elements
@@ -40,7 +68,7 @@ function init() {
 
   // Load or set localStorage data
   var settings = [
-    'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear',
+    'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear', 'notify',
   ];
   chrome.storage.sync.get(settings, function(s) {
     var timeStart = s.timeStart || CONSTANTS.YES;
@@ -49,6 +77,7 @@ function init() {
     var clearCookies = s.cookies || CONSTANTS.NO;
     var clearDownloads = s.downloads || CONSTANTS.NO;
     var autoClear = s.autoclear || CONSTANTS.NO;
+    var notify = s.notify || CONSTANTS.YES;
 
     $('input[name=timeStart][value="' + timeStart + '"]')[0].checked = true;
     $('input[name=time][value="' + time + '"]')[0].checked = true;
@@ -56,6 +85,7 @@ function init() {
     $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES);
     $('input[name=downloads]')[0].checked = (clearDownloads === CONSTANTS.YES);
     $('input[name=autoclear]')[0].checked = (autoClear === CONSTANTS.YES);
+    $('input[name=notify]')[0].checked = (notify === CONSTANTS.YES);
   });
 }