-
Notifications
You must be signed in to change notification settings - Fork 64
/
background.js
49 lines (42 loc) · 1.37 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
import DFWP, { Rules } from "./dfwp.js";
const { browser, storage } = DFWP;
let rules = new Rules();
const checkIfActive = async (tabId) => {
const tab = await browser.tabs.get(tabId);
const match = rules.some((r) => r.test(tab.url));
if (match) {
browser.action.setIcon({ path: 'clipboard-active-32.png' });
browser.action.setTitle({ title: "Don't F*** With Paste (active)" });
try {
await browser.tabs.sendMessage(tab.id, { active: true });
} catch { }
} else {
browser.action.setIcon({ path: 'clipboard-inactive-32.png' });
browser.action.setTitle({ title: "Don't F*** With Paste (inactive)" });
try {
await browser.tabs.sendMessage(tab.id, { active: false });
} catch { }
}
};
const fetchRules = (cb) => {
storage.get({ rules: [] }, ({ rules: values }) => {
rules = Rules.deserialize(values);
if (cb) { cb(); }
});
};
browser.runtime.onMessage.addListener(async ({ didLoad }) => {
if (didLoad) {
const [tab] = await browser.tabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT })
checkIfActive(tab.id);
}
});
browser.storage.onChanged.addListener(() => {
fetchRules(async () => {
const tabs = await browser.tabs.query({ active: true });
tabs.forEach(tab => checkIfActive(tab.id));
})
});
browser.tabs.onActivated.addListener(({ tabId }) => {
checkIfActive(tabId)
});
fetchRules();