-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
146 lines (125 loc) · 3.09 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
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
//TODO: handle filter removal
var activeURL = null;
var activeHostname = null;
//helper function to catch events given a selector (avoid pulling in jquery)
function ev(selector, event, handler)
{
var elements = document.querySelectorAll(selector);
for (var i = 0; i < elements.length; ++i)
elements[i].addEventListener(event, handler);
}
function getHostname(url)
{
var parser = document.createElement('a');
parser.href = url;
return parser.hostname;
}
function parseSiteKey(key)
{
var match = key.match(/^site-(enable|filter)(-(.+))?$/);
if (match)
{
if (activeHostname == match[3])
return "site-" + match[1];
else
return null;
}
return key;
}
function applyOption(key, value)
{
key = parseSiteKey(key)
if (!key)
return;
var filter = key.match(/^filter-(.*)$/);
if (filter)
{
var lists = document.querySelectorAll('.filterlist');
for (var i = 0; i < lists.length; ++i)
{
var e = document.createElement('option');
e.text = filter[1];
e.value = filter[1];
lists[i].appendChild(e);
}
return;
}
var e = document.querySelector('*[name="' + key + '"]');
if (e)
{
if (e.type == 'checkbox')
e.checked = value;
else
e.value = value;
if (key.match(/^site-/))
{
if (value === null)
e.parentNode.className = e.parentNode.className.replace(/\b.override\b/,'');
else if (e.parentNode.className.indexOf('override') === -1)
e.parentNode.className += ' override';
}
}
}
function sendOption(key, value)
{
if (key.match(/^site-.*$/))
key = key + "-" + activeHostname;
mylogger.log('popup sets ' + key + '=' + value + ' - ' + encodeURI(activeURL));
//saving the option
if (value !== null)
{
var data = {};
data[key] = value;
mystorage.set(data);
}
else
mystorage.remove(key);
//send to all tabs
var data = {key:key, value:value};
mymessages.sendTabs(data);
//finally, make sure the option page is displaying the right thing
applyOption(key, value);
}
mymessages.listen(function(request){
var re = /^[a-z-]+$/;
if (re.test(request.key))
applyOption(request.key, request.value);
});
document.addEventListener('DOMContentLoaded', function(){
ev('.option', 'change', function(event){
var e = event.target;
if (e.type === 'checkbox')
sendOption(e.name, e.checked);
else
sendOption(e.name, e.value);
});
ev('#clear-overrides', 'click', function(e){
var elements = document.querySelectorAll("#site-specific input, #site-specific select");
for (var i = 0; i < elements.length; ++i)
sendOption(elements[i].name, null);
});
ev('#open-options', 'click', function(e){
openOptions();
e.preventDefault();
return false;
});
});
function onLoad()
{
getActiveTabURL(function(url) {
activeURL = url;
activeHostname = getHostname(url);
mystorage.all(function(items){
for (var key in items)
if (key.match(/^filter.*$/))
applyOption(key, items[key]);
var selects = document.getElementsByTagName('select');
for (var i = 0; i < selects.length; ++i)
selects[i].selectedIndex = -1;
for (var key in items)
if (!key.match(/^filter.*$/))
applyOption(key, items[key]);
});
});
}
window.onload = onLoad;