]>
git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blob - javascript/options.js
e5d53ec5de7f1b5f2d13ad31e717b045a3aa6ceb
1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Script for the options page.
5 * @author arunjit@google.com (Arunjit Singh)
8 window
.addEventListener('load', init
, false);
11 * Pours the data in template string.
12 * @param {Object} datObject The data object to be filled in template string.
13 * @return {string} The new string created from template string and filled
14 * with the given data.
16 String
.prototype.supplant = function(datObject
) {
17 return this.replace(/{([^{}]*)}/g,
18 function(match
, firstSubMatch
) {
19 var replace
= datObject
[firstSubMatch
];
20 return (typeof replace
=== 'string' || typeof replace
=== 'number') ?
27 * @param {string} selector Selector to execute.
28 * @param {HTMLElement=} context HTMLElement to query (optional).
29 * @return {Array.<HTMLElement>} Array of matched elements (non-live).
31 function $(selector
, context
) {
32 if (!(context
&& context
instanceof HTMLElement
)) {
35 return Array
.prototype.slice
.call(context
.querySelectorAll(selector
));
39 * Initializes the i18n strings.
40 * Loads values from localStorage and applies them to the elements
44 CONSTANTS
.TIMES
.forEach(function(time
) {
45 var selector
= 'input[name=time][value="{t}"], .opt-label#opt-{t}'
47 var elements
= $(selector
);
48 time
= getUnitsForTime(time
);
49 var message
= (time
.time
=== -1) ?
50 chrome
.i18n
.getMessage('optionsTimeAll') :
51 chrome
.i18n
.getMessage('optionsTime',
52 [time
.time
, time
.units
]);
53 elements
[0].title
= message
;
54 elements
[1].innerText
= message
;
57 $('[i18n-content]').forEach(function(ele
) {
58 ele
.innerText
= chrome
.i18n
.getMessage(ele
.getAttribute('i18n-content'));
61 // Bind all the callbacks
62 $('input[type=radio]').forEach(function(e
) {
65 $('input[type=checkbox]').forEach(function(e
) {
69 // Load or set localStorage data
71 'timeStart', 'time', 'prompt', 'cookies', 'downloads', 'autoclear', 'notify',
73 chrome
.storage
.sync
.get(settings
, function(s
) {
74 var timeStart
= s
.timeStart
|| CONSTANTS
.YES
;
75 var time
= ~~(s
.time
) || (s
.time
= CONSTANTS
.DEFAULT_TIME
);
76 var showPrompt
= s
.prompt
|| (s
.prompt
= CONSTANTS
.YES
);
77 var clearCookies
= s
.cookies
|| CONSTANTS
.NO
;
78 var clearDownloads
= s
.downloads
|| CONSTANTS
.NO
;
79 var autoClear
= s
.autoclear
|| CONSTANTS
.NO
;
80 var notify
= s
.notify
|| CONSTANTS
.YES
;
82 $('input[name=timeStart][value="' + timeStart
+ '"]')[0].checked
= true;
83 $('input[name=time][value="' + time
+ '"]')[0].checked
= true;
84 $('input[name=prompt]')[0].checked
= (showPrompt
=== CONSTANTS
.YES
);
85 $('input[name=cookies]')[0].checked
= (clearCookies
=== CONSTANTS
.YES
);
86 $('input[name=downloads]')[0].checked
= (clearDownloads
=== CONSTANTS
.YES
);
87 $('input[name=autoclear]')[0].checked
= (autoClear
=== CONSTANTS
.YES
);
88 $('input[name=notify]')[0].checked
= (notify
=== CONSTANTS
.YES
);
93 * Toggles the value in localStorage for the element selected
94 * @this {HTMLInputElement} The element (radio button) that was clicked.
98 setting
[this.name
] = this.value
;
99 chrome
.storage
.sync
.set(setting
);
104 * Sets the {@code localStorage.prompt} property when selected
105 * @this {HTMLInputElement} The element (checkbox) that was clicked.
107 function setCheck() {
109 setting
[this.name
] = this.checked
? CONSTANTS
.YES
: CONSTANTS
.NO
;
110 chrome
.storage
.sync
.set(setting
);
114 // For rapid changes/saves
117 * Updates the UI to indicate save completed
119 function optionSaved() {
120 var element
= $('#optionsSaved')[0];
121 element
.classList
.add('show');
122 clearTimeout(isSaving
);
123 isSaving
= setTimeout(function() {
124 element
.classList
.remove('show');