]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blob - javascript/options.js
convert to MV3
[chrome-ext/clearhistory-advance-fork.git] / javascript / options.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
2
3 /**
4 * @fileoverview Script for the options page.
5 * @author arunjit@google.com (Arunjit Singh)
6 */
7
8 window.addEventListener('load', init, false);
9
10 /**
11 * Initializes the i18n strings.
12 * Loads values from localStorage and applies them to the elements
13 */
14 function init() {
15 // Localize strings:
16 CONSTANTS.TIMES.forEach(function(time) {
17 var selector = 'input[name=time][value="{t}"], .opt-label#opt-{t}'
18 .supplant({t: time});
19 var elements = $(selector);
20 time = getUnitsForTime(time);
21 var message = (time.time === -1) ?
22 chrome.i18n.getMessage('optionsTimeAll') :
23 chrome.i18n.getMessage('optionsTime',
24 [time.time, time.units]);
25 elements[0].title = message;
26 elements[1].innerText = message;
27 });
28
29 $('[i18n-content]').forEach(function(ele) {
30 ele.innerText = chrome.i18n.getMessage(ele.getAttribute('i18n-content'));
31 });
32
33 // Bind all the callbacks
34 $('input[type=radio]').forEach(function(e) {
35 e.onclick = toggle;
36 });
37 $('input[type=checkbox]').forEach(function(e) {
38 e.onclick = setCheck;
39 });
40
41 // Load or set localStorage data
42 var settings = [
43 'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear', 'notify',
44 ];
45 chrome.storage.sync.get(settings, function(s) {
46 var timeStart = s.timeStart || CONSTANTS.YES;
47 var time = ~~(s.time) || (s.time = CONSTANTS.DEFAULT_TIME);
48 var showPrompt = s.prompt || (s.prompt = CONSTANTS.YES);
49 var clearCookies = s.cookies || CONSTANTS.NO;
50 var clearDownloads = s.downloads || CONSTANTS.NO;
51 var autoClear = s.autoclear || CONSTANTS.NO;
52 var notify = s.notify || CONSTANTS.YES;
53
54 $('input[name=timeStart][value="' + timeStart + '"]')[0].checked = true;
55 $('input[name=time][value="' + time + '"]')[0].checked = true;
56 $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
57 $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES);
58 $('input[name=downloads]')[0].checked = (clearDownloads === CONSTANTS.YES);
59 $('input[name=autoclear]')[0].checked = (autoClear === CONSTANTS.YES);
60 $('input[name=notify]')[0].checked = (notify === CONSTANTS.YES);
61 });
62 }
63
64 /**
65 * Toggles the value in localStorage for the element selected
66 * @this {HTMLInputElement} The element (radio button) that was clicked.
67 */
68 function toggle() {
69 var setting = {};
70 setting[this.name] = this.value;
71 chrome.storage.sync.set(setting);
72 optionSaved();
73 }
74
75 /**
76 * Sets the {@code localStorage.prompt} property when selected
77 * @this {HTMLInputElement} The element (checkbox) that was clicked.
78 */
79 function setCheck() {
80 var setting = {};
81 setting[this.name] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
82 chrome.storage.sync.set(setting);
83 optionSaved();
84 }
85
86 // For rapid changes/saves
87 var isSaving = null;
88 /**
89 * Updates the UI to indicate save completed
90 */
91 function optionSaved() {
92 var element = $('#optionsSaved')[0];
93 element.classList.add('show');
94 clearTimeout(isSaving);
95 isSaving = setTimeout(function() {
96 element.classList.remove('show');
97 }, 1000);
98 }