Parent: [226275] (diff)

Child: [145f17] (diff)

Download this file

options.js    207 lines (188 with data), 6.8 kB

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* RecollWebext - WebExtension - Options page
*
* A lot of code was copied from or inspired by the Save Page WE
* extension.
*
* Copyright (C) 2017 jfd@recoll.org
* Copyright (C) 2016-2017 DW-dev
*
* Distributed under the GNU General Public License version 2
* See LICENCE.txt file and http://www.gnu.org/licenses/
*/
"use strict";
/* Listener for options page load */
document.addEventListener("DOMContentLoaded", onLoadPage, false);
var urlRules = {
inc: [],
exc: []
};
function onLoadPage(event)
{
/* Load options from local storage */
console.log("ONLOADPAGE");
chrome.storage.local.get(null,
function(object) {
var i, t;
var checkboxes = ["options-autosave",
"options-showsubmenu",
"options-httpsalso",
"options-nomatch-dosave",
"options-conflict-dosave"];
for (i = 0; i < checkboxes.length; i++) {
document.getElementById(checkboxes[i]).checked =
object[checkboxes[i]];
}
var keys = ["options-url-include", "options-url-exclude"];
var sks = ["inc", "exc"];
for (t = 0; t < 2; t++) {
var key = keys[t];
var sk = sks[t];
if (key in object) {
for (i = 0; i < object[key].length; i++) {
urlRules[sk].push(object[key][i]);
}
}
}
updateRulesTables();
});
document.getElementById("include-button-add").addEventListener(
"click", onClickIncludeAdd, false);
document.getElementById("include-button-delete").addEventListener(
"click", onClickIncludeDelete, false);
document.getElementById("exclude-button-add").addEventListener(
"click", onClickExcludeAdd, false);
document.getElementById("exclude-button-delete").addEventListener(
"click", onClickExcludeDelete, false);
document.getElementById("options-autosave").addEventListener(
"click", onClickAutosave, false);
document.getElementById("options-save-button").addEventListener(
"click", onClickSave, false);
document.removeEventListener("DOMContentLoaded", onLoadPage, false);
}
function updateRulesTables()
{
var i, t;
var caption = "URL include rules";
var keys = ["options-url-include", "options-url-exclude"];
var sks = ["inc", "exc"];
for (t = 0; t < 2; t++) {
var key = keys[t];
var sk = sks[t];
var html = '<caption>' + caption + '</caption>\n' +
'<tr><th>' +
'<input id="ckb-' + sk + '-all" type="checkbox"/>' +
'</th><th>Name</th><th>Pattern</th>' +
'<th>PatternType</th></tr>';
for (i = 0; i < urlRules[sk].length; i++) {
html += '<tr><td>'+
'<input id="ckb-' + sk + '-' + i + '" type="checkbox"/>' +
'</td>';
html += '<td>' + urlRules[sk][i][0] + '</td>';
html += '<td>' + urlRules[sk][i][1] + '</td>';
html += '<td>' + urlRules[sk][i][2] + '</td></tr>';
}
if (urlRules[sk].length == 0) {
html += '<tr><td>'+
'<input id="ckb-' + sk + '-0" type="checkbox"/></td>';
html += '<td> </td><td> </td><td> </td></tr>';
}
console.log("Elt for key "+key + " is " + document.getElementById(key));
console.log("html is " + html);
document.getElementById(key).innerHTML = html;
caption = "URL exclude rules";
}
document.getElementById("ckb-inc-all").addEventListener(
"click", onClickIncludeSelectAll, false);
document.getElementById("ckb-exc-all").addEventListener(
"click", onClickExcludeSelectAll, false);
}
function onClickRuleAdd(key, sk)
{
var name = document.getElementById(key + "-input-name").value;
var val = document.getElementById(key + "-input-value").value;
var tp = document.getElementById(key + "-select-type").value;
console.log('onClickRuleAdd: urlRules['+ sk +'].push(['+ name + ', ' + val +
', ' + tp + '])')
urlRules[sk].push([name, val, tp]);
/* We have a positive rule: it makes no sense to have save by default */
document.getElementById("options-nomatch-dosave").checked = false;
onClickSave();
}
function onClickRuleDelete(sk)
{
var i;
var newlist = [];
for (i = 0; i < urlRules[sk].length; i++) {
var id = 'ckb-' + sk + '-' + i;
if (! document.getElementById(id).checked) {
newlist.push(urlRules[sk][i]);
}
}
urlRules[sk] = newlist;
onClickSave();
}
function onClickRuleSelectAll(sk)
{
var i;
var ck = document.getElementById('ckb-' + sk + '-all').checked;
for (i = 0; i < urlRules[sk].length; i++) {
document.getElementById('ckb-' + sk + '-' + i).checked = ck;
}
}
function onClickIncludeAdd(event)
{
onClickRuleAdd('include', 'inc');
}
function onClickIncludeDelete(event)
{
onClickRuleDelete('inc');
}
function onClickIncludeSelectAll(event)
{
onClickRuleSelectAll('inc');
}
function onClickExcludeAdd(event)
{
onClickRuleAdd('exclude', 'exc');
}
function onClickExcludeDelete(event)
{
onClickRuleDelete('exc');
}
function onClickExcludeSelectAll(event)
{
onClickRuleSelectAll('exc');
}
/* Enable or Disable options */
function onClickAutosave(event)
{
document.getElementById("options-httpsalso").disabled =
!document.getElementById("options-autosave").checked;
}
/* Save options */
function onClickSave(event)
{
var checkboxnames = ["options-showsubmenu",
"options-autosave",
"options-httpsalso",
"options-nomatch-dosave",
"options-conflict-dosave"
];
var i, t;
var opts = {};
for (i = 0; i < checkboxnames.length; i++) {
opts[checkboxnames[i]] =
document.getElementById(checkboxnames[i]).checked;
}
opts["options-url-include"] = urlRules["inc"];
opts["options-url-exclude"] = urlRules["exc"];
chrome.storage.local.set(opts);
/* Display saved status for short period */
document.getElementById("options-save-status").style.setProperty(
"visibility", "visible", "");
setTimeout(function() {
document.getElementById("options-save-status").style.setProperty(
"visibility", "hidden", "");
}, 1000);
}