forked from pastyface/chrome-refresh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
executable file
·152 lines (129 loc) · 3.62 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var options = { 'tab_stick': 'stick',
'default_delay': 'delay',
'countdown': 'countdown'
};
// Restores select box state to saved value from localStorage.
function restore_options() {
eachOption(restore_option);
displayAutos();
}
// Saves options to localStorage.
function save_options() {
eachOption(save_option);
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.innerHTML = 'Options Saved.';
setTimeout(function() {
status.innerHTML = '';
}, 750);
}
function save_option(storageKey, elementId){
var element = document.getElementById(elementId);
var type = element.constructor.name;
if(type === 'HTMLSelectElement'){
var value = element.children[element.selectedIndex].value;
}
else if(type === 'HTMLInputElement'){
if(element.type === 'checkbox'){
var value = $(element).attr('checked');
}
else {
var value = $(element).val();
}
}
if(value){
localStorage[storageKey] = value;
}
else {
delete localStorage[storageKey];
}
}
function restore_option(storageKey, elementId){
var value = localStorage[storageKey];
if (!value) {
return;
}
var element = document.getElementById(elementId);
var type = element.constructor.name;
if(type === 'HTMLSelectElement'){
for (var i = 0; i < element.children.length; i++) {
var child = element.children[i];
if (child.value == value) {
child.selected = 'true';
break;
}
}
}
else if(type === 'HTMLInputElement'){
if(element.type === 'checkbox'){
if(value){
$(element).attr('checked', 'checked');
}
else {
$(element).removeAttr('checked');
}
}
else {
$(element).val(value);
}
}
}
function eachOption(func){
var key;
for(key in options) {
if(typeof options[key] !== 'function') {
func(key, options[key]);
}
}
}
function displayAutos(){
var cr = chrome.extension.getBackgroundPage().cr;
var autos = cr.loadAllAutos();
var autoCount = 0;
var content = '<table class="table"><tr><th width="20"></th>' +
'<th>Title</th>' +
'<th>URL</th>' +
'<th align="center">Delay</th>' +
'</tr>';
for(key in autos) {
if(typeof autos[key] !== 'function') {
var props = autos[key];
var row = '<tr class="autos"><td><span class="glyphicon glyphicon-remove-circle remove clickable" id="deleteAuto_'+autoCount+'"></span>'+
'</td><td>' + props.title +
'</td><td><a href="'+props.url+'">' + props.url +'</a>'+
'</td><td align="center">' + props.timeout +
'</td></tr>';
content += row;
autoCount++;
}
}
content += '</table>';
if(autoCount > 0){
$('#autos').html(content);
}
else {
$('#autos').html('<em>none</em>');
}
autoCount = 0;
for(key in autos) {
if(typeof autos[key] !== 'function') {
var props = autos[key];
$('#deleteAuto_'+autoCount).click(function(){
deleteAuto(props.url);
});
autoCount++;
}
}
}
function deleteAuto(url){
var prompt = 'Stop automatically refreshing '+url;
if(confirm(prompt)){
var cr = chrome.extension.getBackgroundPage().cr;
cr.removeAutos(url);
displayAutos();
}
}
$(function() {
restore_options();
$('#save_options').click(save_options);
});