forked from Cimbali/CleanLinks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
100 lines (83 loc) · 2.95 KB
/
popup.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
/* ***** BEGIN LICENSE BLOCK *****
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/
*
* The Original Code is CleanLinks Mozilla Extension.
*
* The Initial Developer of the Original Code is
* Copyright (C)2012 Diego Casorran <[email protected]>
* All Rights Reserved.
*
* ***** END LICENSE BLOCK ***** */
function add_option(orig, clean)
{
var select = document.querySelector('select');
var option = document.createElement('option');
// value is -1 for title, otherwise id of elment in clean list
option.setAttribute('value', '' + (select.querySelectorAll('option').length - 1));
if (option.value == '-1') {
option.disabled = true;
}
var span = document.createElement('span');
span.append(document.createTextNode(orig))
span.setAttribute('class', 'original');
option.appendChild(span);
span = document.createElement('span');
span.append(document.createTextNode(clean))
span.setAttribute('class', 'cleaned');
option.appendChild(span);
select.appendChild(option);
}
function set_toggle_text()
{
if (prefValues.enabled) {
document.querySelector('#status').textContent = _('browser_enabled')
document.querySelector('#toggle').textContent = _('browser_disable')
} else {
document.querySelector('#status').textContent = _('browser_disabled')
document.querySelector('#toggle').textContent = _('browser_enable')
}
}
function populate_popup()
{
var list = document.querySelectorAll('[i18n_text]');
for (var n = 0; n < list.length; n++)
list[n].prepend(document.createTextNode(_(list[n].getAttribute('i18n_text'))));
document.querySelector('#title').prepend(document.createTextNode(title + ' v' + version));
document.querySelector('#copyright').appendChild(document.createTextNode('\u00A9 '+copyright));
var link = document.createElement('a');
link.setAttribute('href', homepage);
link.appendChild(document.createTextNode(homepage));
document.querySelector('#homepage').appendChild(link);
add_option(_('bootstrap_listheader_original'), _('bootstrap_listheader_cleaned'));
browser.runtime.sendMessage('get_cleaned_list').then(response =>
{
response.forEach(clean => add_option(clean.orig, clean.url));
});
set_toggle_text();
document.querySelector('#toggle').onclick = () =>
{
prefValues.enabled = !prefValues.enabled;
set_toggle_text();
browser.runtime.sendMessage('toggle');
}
document.querySelector('#whitelist').onclick = () =>
{
var select = document.querySelector('select');
var id = parseInt(select.value);
browser.runtime.sendMessage({whitelist: id}).then(() =>
// remove selected element, and renumber higher-ordered ones
select.querySelectorAll('option').forEach(opt =>
{
var opt_id = parseInt(opt.value);
if (opt_id == id)
opt.remove()
else if (opt_id > id)
opt.value = '' + (opt_id - 1);
})
);
}
}
loadOptions().then(() => populate_popup());