(options: {
url: string;
title: string;
text: string;
image?: string;
sticky?: boolean;
})
| 3 | |
| 4 | // Only works on the background Page |
| 5 | export async function sendNotification(options: { |
| 6 | url: string; |
| 7 | title: string; |
| 8 | text: string; |
| 9 | image?: string; |
| 10 | sticky?: boolean; |
| 11 | }) { |
| 12 | if (!options.image) options.image = defaultImg; |
| 13 | |
| 14 | con.m('Notification').log(options); |
| 15 | |
| 16 | const imgBlob = await getImageBlob(options.image); |
| 17 | const messageArray = { |
| 18 | type: 'basic' as const, |
| 19 | title: options.title, |
| 20 | message: options.text, |
| 21 | iconUrl: imgBlob, |
| 22 | contextMessage: 'MAL-Sync', |
| 23 | requireInteraction: options.sticky ?? false, |
| 24 | eventTime: Date.now(), |
| 25 | }; |
| 26 | try { |
| 27 | chrome.notifications.create(options.url, messageArray); |
| 28 | } catch (e) { |
| 29 | con.error(e); |
| 30 | // @ts-ignore |
| 31 | delete messageArray.requireInteraction; |
| 32 | chrome.notifications.create(options.url, messageArray); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | function getImageBlob(url, fallback = false): Promise<string> { |
| 37 | if (fallback) url = defaultImg; |
no test coverage detected