]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blame - javascript/options.js
make cookie deletion optional
[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');
77dffdcd 33 $('#optionsCookies')[0].innerText = chrome.i18n.getMessage('optionsCookies');
a5409a6b
MF
34 $('#optionsSaved > b')[0].innerText = chrome.i18n.getMessage('optionsSaved');
35
36 // Bind all the callbacks
37 $('#opt-time input.opt-chk[type=radio]').forEach(function(e) {
38 e.onclick = toggle;
39 });
77dffdcd
MF
40 $('input.opt-chk[type=checkbox]').forEach(function(e) {
41 e.onclick = setCheck;
42 });
a5409a6b
MF
43 $('#opt-prompt input.opt-chk[type=checkbox]').forEach(function(e) {
44 e.onclick = setPrompt;
45 });
46
47 // Load or set localStorage data
48 var time = ~~(localStorage['time']) ||
49 (localStorage['time'] = CONSTANTS.DEFAULT_TIME);
50 var showPrompt = localStorage['prompt'] ||
51 (localStorage['prompt'] = CONSTANTS.YES);
77dffdcd 52 var clearCookies = localStorage['cookies'] || CONSTANTS.NO;
a5409a6b
MF
53
54 $('input[name=time][value="' + time + '"]')[0].checked = true;
55 $('input[name=prompt]')[0].checked = (showPrompt === CONSTANTS.YES);
77dffdcd 56 $('input[name=cookies]')[0].checked = (clearCookies === CONSTANTS.YES);
a5409a6b
MF
57}
58
59/**
60 * Toggles the value in localStorage for the element selected
61 * @this {HTMLInputElement} The element (radio button) that was clicked.
62 */
63function toggle() {
64 localStorage['time'] = this.value;
65 optionSaved();
66}
67
68/**
69 * Sets the {@code localStorage.prompt} property when selected
70 * @this {HTMLInputElement} The element (checkbox) that was clicked.
71 */
72function setPrompt() {
73 localStorage['prompt'] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
74 optionSaved();
75}
76
77dffdcd
MF
77function setCheck() {
78 localStorage[this.name] = this.checked ? CONSTANTS.YES : CONSTANTS.NO;
79 optionSaved();
80}
81
a5409a6b
MF
82// For rapid changes/saves
83var isSaving = null;
84/**
85 * Updates the UI to indicate save completed
86 */
87function optionSaved() {
88 var element = $('#optionsSaved')[0];
89 element.classList.add('show');
90 clearTimeout(isSaving);
91 isSaving = setTimeout(function() {
92 element.classList.remove('show');
93 }, 1000);
94}