-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathbackground.js
More file actions
152 lines (123 loc) · 3.78 KB
/
Copy pathbackground.js
File metadata and controls
152 lines (123 loc) · 3.78 KB
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
147
148
149
150
151
152
import browser from 'webextension-polyfill';
import delay from 'delay';
import optionsStorage from './options-storage.js';
import localStore from './lib/local-store.js';
import {openTab} from './lib/tabs-service.js';
import {queryPermission} from './lib/permissions-service.js';
import {getNotificationCount, getTabUrl} from './lib/api.js';
import {renderCount, renderError, renderWarning} from './lib/badge.js';
import {checkNotifications, openNotification} from './lib/notifications-service.js';
import {isChrome, isNotificationTargetPage} from './util.js';
async function scheduleNextAlarm(interval) {
const intervalSetting = await localStore.get('interval') || 60;
const intervalValue = interval || 60;
if (intervalSetting !== intervalValue) {
localStore.set('interval', intervalValue);
}
// Delay less than 1 minute will cause a warning
const delayInMinutes = Math.max(Math.ceil(intervalValue / 60), 1);
browser.alarms.clearAll();
browser.alarms.create('update', {delayInMinutes});
}
async function handleLastModified(newLastModified) {
const lastModified = await localStore.get('lastModified') || new Date(0).toUTCString();
// Something has changed since we last accessed, display any new notifications
if (newLastModified !== lastModified) {
const {showDesktopNotif, playNotifSound} = await optionsStorage.getAll();
if (showDesktopNotif === true || playNotifSound === true) {
await checkNotifications(lastModified);
}
await localStore.set('lastModified', newLastModified);
}
}
async function updateNotificationCount() {
const response = await getNotificationCount();
const {count, interval, lastModified} = response;
renderCount(count);
scheduleNextAlarm(interval);
handleLastModified(lastModified);
}
function handleError(error) {
scheduleNextAlarm();
renderError(error);
}
function handleOfflineStatus() {
scheduleNextAlarm();
renderWarning('offline');
}
async function update() {
if (navigator.onLine) {
try {
await updateNotificationCount();
} catch (error) {
handleError(error);
}
} else {
handleOfflineStatus();
}
}
async function handleBrowserActionClick() {
await openTab(await getTabUrl());
}
function handleInstalled(details) {
if (details.reason === 'install') {
browser.runtime.openOptionsPage();
}
}
async function onMessage(message) {
if (message.action === 'update') {
await addHandlers();
await update();
}
}
async function onTabUpdated(tabId, changeInfo, tab) {
if (changeInfo.status !== 'complete') {
return;
}
if (await isNotificationTargetPage(tab.url)) {
await delay(1000);
await update();
}
}
function onNotificationClick(id) {
openNotification(id);
}
async function createOffscreenDocument() {
if (await browser.offscreen.hasDocument()) {
return;
}
await browser.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['AUDIO_PLAYBACK'],
justification: 'To play an audio chime indicating notifications'
});
}
async function addHandlers() {
const {updateCountOnNavigation} = await optionsStorage.getAll();
if (await queryPermission('notifications')) {
browser.notifications.onClicked.addListener(onNotificationClick);
}
if (await queryPermission('tabs')) {
if (updateCountOnNavigation) {
browser.tabs.onUpdated.addListener(onTabUpdated);
} else {
browser.tabs.onUpdated.removeListener(onTabUpdated);
}
}
}
async function init() {
browser.alarms.onAlarm.addListener(update);
scheduleNextAlarm();
browser.runtime.onMessage.addListener(onMessage);
browser.runtime.onInstalled.addListener(handleInstalled);
// Chrome specific API
if (isChrome(navigator.userAgent)) {
browser.permissions.onAdded.addListener(addHandlers);
}
browser.action.onClicked.addListener(handleBrowserActionClick);
window.addEventListener('online', update);
await createOffscreenDocument();
addHandlers();
update();
}
init();