Browser extension that demonstrates the Web Annotation Discovery mechanism: subscribe to people’s annotation collections/‘feeds’, to see their notes on the web; and create & publish annotations yourself.

web-annotation-discovery-we.../src/util/ notify.ts
50 lines
1.4 KiB

  1. const notifications = new Map();
  2. export default async function notify({
  3. type = 'basic',
  4. iconUrl = transparent1pixelPng,
  5. message = '',
  6. title = '',
  7. onClicked = () => {},
  8. ...otherProps
  9. }: Partial<
  10. browser.notifications.CreateNotificationOptions & {
  11. onClicked: () => void;
  12. }
  13. >) {
  14. const notificationId = await browser.notifications.create({
  15. type,
  16. iconUrl,
  17. title,
  18. message,
  19. ...otherProps,
  20. });
  21. notifications.set(notificationId, { onClicked });
  22. if (!browser.notifications.onClicked.hasListener(clickedListener)) {
  23. browser.notifications.onClicked.addListener(clickedListener);
  24. }
  25. if (!browser.notifications.onClosed.hasListener(closedListener)) {
  26. browser.notifications.onClosed.addListener(closedListener);
  27. }
  28. }
  29. function clickedListener(notificationId: string) {
  30. if (notifications.has(notificationId)) {
  31. notifications.get(notificationId).onClicked();
  32. }
  33. }
  34. function closedListener(notificationId: string) {
  35. if (notifications.has(notificationId)) {
  36. notifications.delete(notificationId);
  37. if (notifications.size === 0) {
  38. browser.notifications.onClosed.removeListener(clickedListener);
  39. browser.notifications.onClosed.removeListener(closedListener);
  40. }
  41. }
  42. }
  43. const transparent1pixelPng =
  44. 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQI12NgAAIAAAUAAeImBZsAAAAASUVORK5CYII=';