forked from jswanner/DontF-WithPaste
-
Notifications
You must be signed in to change notification settings - Fork 21
/
popup.js
54 lines (44 loc) · 1.68 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
(function() {
document.addEventListener('DOMContentLoaded', () => {
const add = document.querySelector('.add');
const cancel = document.querySelector('.cancel');
const container = document.querySelector('.container');
const form = document.querySelector('.form');
const options = document.querySelector('.options');
let rules = new DFWP.Rules();
form.addEventListener('submit', event => {
event.preventDefault();
DFWP.storage.set({ rules: rules.serialize() });
window.close();
});
cancel.addEventListener('click', event => {
window.close();
});
options.addEventListener('click', event => {
DFWP.browser.runtime.openOptionsPage();
});
DFWP.storage.get({ rules: [] }, ({ rules: values }) => {
rules = DFWP.Rules.deserialize(values);
DFWP.browser.tabs.query({active: true, windowId: DFWP.browser.windows.WINDOW_ID_CURRENT}, ([tab]) => {
const addHandler = () => {
const rule = new DFWP.Rule(new URL(tab.url).origin.replace(/\./g, '\\.'));
rules.add(rule);
new DFWP.RuleView(rule, rules).render(container, '#new');
};
add.addEventListener('click', addHandler);
const matching = rules.filter(rule => rule.test(tab.url));
matching.forEach(rule => new DFWP.RuleView(rule, rules).render(container, '#existing'));
if (!matching.length) {
addHandler();
}
const remove = document.querySelector('.delete');
if (remove) {
remove.addEventListener('click', event => {
DFWP.storage.set({ rules: rules.serialize() });
window.close();
});
}
});
});
});
})();