UserScript Notification Framework

This script is intended to work with @require only. Exposes an instance of the Notifications class of this script to window.UserScript.Notifications

Version vom 19.01.2022. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/438798/1010206/UserScript%20Notification%20Framework.js

  1. // ==UserScript==
  2. // @namespace Xortrox/UserScripts/Notifications
  3. // @name UserScript Notification Framework
  4. // @version 0.3
  5. // @description This script is intended to work with @require only. Exposes an instance of the Notifications class of this script to window.UserScript.Notifications
  6. // @author Xortrox, Puls3
  7. // @match *
  8. // @esversion: 6
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. class Notifications {
  13.  
  14. /** Should always be awaited before you use notifications. */
  15. askPermission() {
  16. return this.hasPermission();
  17. }
  18.  
  19. notify(title, text, icon) {
  20. this.hasPermission().then(function (result) {
  21. if (result === true) {
  22. let popup = new window.Notification(title, { body: text, icon: icon });
  23. popup.onclick = function () {
  24. window.focus();
  25. }
  26. }
  27. });
  28. }
  29.  
  30. hasPermission() {
  31. return new Promise(function (resolve) {
  32. if ('Notification' in window) {
  33. if (window.Notification.permission === 'granted') {
  34. resolve(true);
  35. } else {
  36. window.Notification.requestPermission().then(function (permission) {
  37. if (permission === 'granted') {
  38. resolve(true);
  39. } else {
  40. resolve(false);
  41. }
  42. });
  43. }
  44. } else {
  45. resolve(true);
  46. }
  47. });
  48. }
  49. }
  50.  
  51. if (!window.UserScript) {
  52. window.UserScript = {};
  53. }
  54. window.UserScript.Notifications = new Notifications();