]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blame - javascript/options.js
add support for clearing download history
[chrome-ext/clearhistory-advance-fork.git] / javascript / options.js
CommitLineData
a5409a6b
MF
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
8window.addEventListener('load', init, false);
9
a5409a6b
MF
10/**
11 * Initializes the i18n strings.
12 * Loads values from localStorage and applies them to the elements
13 */
14function 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 $('#optionsTitle')[0].innerText = chrome.i18n.getMessage('optionsTitle');
30 $('#optionsHeader')[0].innerText = chrome.i18n.getMessage('optionsHeader');
31 $('#optionsPrompt')[0].innerText = chrome.i18n.getMessage('optionsPrompt');
32 $('#optionsTimeFor')[0].innerText = chrome.i18n.getMessage('optionsTimeFor');
cc8be426 33 $('#optionsTimeStart')[0].innerText = chrome.i18n.getMessage('optionsTimeStart');
77dffdcd 34 $('#optionsCookies')[0].innerText = chrome.i18n.getMessage('optionsCookies');
c8fabbc8 35 $('#optionsDownloads')[0].innerText = chrome.i18n.getMessage('optionsDownloads');
6565d921 36 $('#optionsAutoclear')[0].innerText = chrome.i18n.getMessage('optionsAutoclear');
a5409a6b
MF
37 $('#optionsSaved > b')[0].innerText = chrome.i18n.getMessage('optionsSaved');
38
39 // Bind all the callbacks
40 $('#opt-time input.opt-chk[type=radio]').forEach(function(e) {
41 e.onclick = toggle;
42 });
77dffdcd
MF
43 $('input.opt-chk[type=checkbox]').forEach(function(e) {
44 e.onclick = setCheck;
45 });
a5409a6b
MF
46
47 // Load or set localStorage data
03eef92c 48 var settings = [
c8fabbc8 49 'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear',
03eef92c
MF
50 ];
51 chrome.storage.sync.get(settings, function(s) {
52 var timeStart = s.timeStart || CONSTANTS.YES;
53 var time = ~~(s.time) || (s.time = CONSTANTS.DEFAULT_TIME);
54 var showPrompt = s.prompt || (s.prompt = CONSTANTS.YES);
55 var clearCookies = s.cookies || CONSTANTS.NO;
c8fabbc8 56 var clearDownloads = s.downloads || CONSTANTS.NO;
03eef92c 57 var autoClear = s.autoclear || CONSTANTS.NO;
a5409a6b 58
03eef92c
MF
59 $('input[name=timeStart]')[0].checked = (timeStart === CONSTANTS.YES);
60 $('input[name=time][value="' + time + '"]')[0].checked = true;
61 $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
62 $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES);
c8fabbc8 63 $('input[name=downloads]')[0].checked = (clearDownloads === CONSTANTS.YES);
03eef92c
MF
64 $('input[name=autoclear]')[0].checked = (autoClear === CONSTANTS.YES);
65 });
a5409a6b
MF
66}
67
68/**
69 * Toggles the value in localStorage for the element selected
70 * @this {HTMLInputElement} The element (radio button) that was clicked.
71 */
72function toggle() {
03eef92c 73 chrome.storage.sync.set({'time': this.value});
a5409a6b
MF
74 optionSaved();
75}
76
77/**
78 * Sets the {@code localStorage.prompt} property when selected
79 * @this {HTMLInputElement} The element (checkbox) that was clicked.
80 */
77dffdcd 81function setCheck() {
03eef92c
MF
82 var setting = {};
83 setting[this.name] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
84 chrome.storage.sync.set(setting);
77dffdcd
MF
85 optionSaved();
86}
87
a5409a6b
MF
88// For rapid changes/saves
89var isSaving = null;
90/**
91 * Updates the UI to indicate save completed
92 */
93function optionSaved() {
94 var element = $('#optionsSaved')[0];
95 element.classList.add('show');
96 clearTimeout(isSaving);
97 isSaving = setTimeout(function() {
98 element.classList.remove('show');
99 }, 1000);
100}