]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blobdiff - javascript/options.js
convert to MV3
[chrome-ext/clearhistory-advance-fork.git] / javascript / options.js
index 682347bff6c00e2e6d68980bba947938c3030673..5f81fea950eeca9e347e52c3ca531e231a1e388f 100644 (file)
@@ -5,8 +5,39 @@
  * @author arunjit@google.com (Arunjit Singh)
  */
 
+import {CONSTANTS} from './background.js';
+import {getUnitsForTime} from './util.js';
+
 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
@@ -26,28 +57,39 @@ function init() {
     elements[1].innerText = message;
   });
 
-  $('#optionsTitle')[0].innerText = chrome.i18n.getMessage('optionsTitle');
-  $('#optionsHeader')[0].innerText = chrome.i18n.getMessage('optionsHeader');
-  $('#optionsPrompt')[0].innerText = chrome.i18n.getMessage('optionsPrompt');
-  $('#optionsTimeFor')[0].innerText = chrome.i18n.getMessage('optionsTimeFor');
-  $('#optionsSaved > b')[0].innerText = chrome.i18n.getMessage('optionsSaved');
+  $('[i18n-content]').forEach(function(ele) {
+    ele.innerText = chrome.i18n.getMessage(ele.getAttribute('i18n-content'));
+  });
 
   // Bind all the callbacks
-  $('#opt-time input.opt-chk[type=radio]').forEach(function(e) {
+  $('input[type=radio]').forEach(function(e) {
     e.onclick = toggle;
   });
-  $('#opt-prompt input.opt-chk[type=checkbox]').forEach(function(e) {
-    e.onclick = setPrompt;
+  $('input[type=checkbox]').forEach(function(e) {
+    e.onclick = setCheck;
   });
 
   // Load or set localStorage data
-  var time = ~~(localStorage['time']) ||
-             (localStorage['time'] = CONSTANTS.DEFAULT_TIME);
-  var showPrompt = localStorage['prompt'] ||
-                   (localStorage['prompt'] = CONSTANTS.YES);
+  var settings = [
+    'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear', 'notify',
+  ];
+  chrome.storage.sync.get(settings, function(s) {
+    var timeStart = s.timeStart || CONSTANTS.YES;
+    var time = ~~(s.time) || (s.time = CONSTANTS.DEFAULT_TIME);
+    var showPrompt = s.prompt || (s.prompt = CONSTANTS.YES);
+    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=time][value="' + time + '"]')[0].checked = true;
-  $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
+    $('input[name=timeStart][value="' + timeStart + '"]')[0].checked = true;
+    $('input[name=time][value="' + time + '"]')[0].checked = true;
+    $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
+    $('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);
+  });
 }
 
 /**
@@ -55,7 +97,9 @@ function init() {
  * @this {HTMLInputElement} The element (radio button) that was clicked.
  */
 function toggle() {
-  localStorage['time'] = this.value;
+  var setting = {};
+  setting[this.name] = this.value;
+  chrome.storage.sync.set(setting);
   optionSaved();
 }
 
@@ -63,8 +107,10 @@ function toggle() {
  * Sets the {@code localStorage.prompt} property when selected
  * @this {HTMLInputElement} The element (checkbox) that was clicked.
  */
-function setPrompt() {
-  localStorage['prompt'] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
+function setCheck() {
+  var setting = {};
+  setting[this.name] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
+  chrome.storage.sync.set(setting);
   optionSaved();
 }