forked from jswanner/DontF-WithPaste
-
Notifications
You must be signed in to change notification settings - Fork 21
/
background.js
65 lines (57 loc) · 1.9 KB
/
background.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
(function() {
const browser = DFWP.browser;
let rules = new DFWP.Rules();
const checkIfActive = (tabId) => {
browser.tabs.get(tabId, tab => {
const match = rules.some((r) => r.test(tab.url));
if (match) {
browser.browserAction.setIcon({ path: 'clipboard-active-32.png' });
browser.browserAction.setTitle({ title: "Don't Fuck With Paste (active)" });
browser.tabs.sendMessage(tab.id, { active: true });
} else {
browser.browserAction.setIcon({ path: 'clipboard-inactive-32.png' });
browser.browserAction.setTitle({ title: "Don't Fuck With Paste (inactive)" });
browser.tabs.sendMessage(tab.id, { active: false });
}
});
};
const fetchRules = (cb) => {
DFWP.storage.get({ rules: [] }, ({ rules: values }) => {
rules = DFWP.Rules.deserialize(values);
if (cb) { cb(); }
});
};
browser.runtime.onInstalled.addListener(({ previousVersion, reason }) => {
if (reason === 'update' && previousVersion == '1.1') {
DFWP.storage.get({ include: '' }, ({ include }) => {
fetchRules(() => {
include.split('\n').forEach(value => {
if (value !== '.*') {
const rule = new DFWP.Rule(value);
rules.add(rule);
}
});
DFWP.storage.set({ rules: rules.serialize() });
});
});
}
});
browser.runtime.onMessage.addListener(({ didLoad }) => {
if (didLoad) {
browser.tabs.query({active: true, windowId: browser.windows.WINDOW_ID_CURRENT}, ([tab]) => {
checkIfActive(tab.id);
});
}
});
browser.storage.onChanged.addListener(() => {
fetchRules(() => {
browser.tabs.query({active: true}, tabs => {
tabs.forEach(tab => {
checkIfActive(tab.id);
});
});
})
});
browser.tabs.onActivated.addListener(({ tabId }) => checkIfActive(tabId));
fetchRules();
})();