-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent-script.js
58 lines (43 loc) · 1.71 KB
/
content-script.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
const BUTTON_ID = 'extension-open-unread';
const observer = new MutationObserver(() => {
const parent = document.getElementsByClassName('MarkAsReadButton')[0]?.parentElement;
if (parent && !parent.querySelector(`#${BUTTON_ID}`)) {
addButton(parent);
}
});
function addButton(parent) {
const button = document.createElement('button');
button.id = BUTTON_ID
button.classList.add(...parent.querySelector('button').classList);
button.innerHTML = 'Open unread';
button.onclick = open;
parent.insertBefore(button, parent.firstChild)
}
observer.observe(document.querySelector('body'), {
childList: true,
subtree: true
});
async function shouldConfirmManyTabs() {
const storage = await browser.storage.local.get("suppressConfirm");
return !storage.suppressConfirm;
}
async function shouldMarkRead() {
const storage = await browser.storage.local.get("markRead");
return storage.markRead;
}
async function open() {
const unread = document.getElementsByClassName('entry--unread');
if (unread.length >= 5 && await shouldConfirmManyTabs() && !confirm(`Are you sure you want to open ${unread.length} tabs`)) {
return;
}
await Promise.all([...unread].map(x => browser.runtime.sendMessage({
href: x.querySelector('.content > a').href
})))
if (await shouldMarkRead()) {
const markAsReadDropdownTrigger = document.getElementsByClassName('MarkAsReadButton')[0];
markAsReadDropdownTrigger.querySelector('button').click();
await new Promise(resolve => requestAnimationFrame(resolve));
const markAllAsReadItem = markAsReadDropdownTrigger.querySelector('li:nth-child(2)');
markAllAsReadItem.click();
}
}