You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

html5_notifier.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * html5_notifier
  3. * Shows a desktop notification every time a new (recent) mail comes in
  4. *
  5. * @version 0.5.0 - 19.12.2013
  6. * @author Tilman Stremlau <tilman@stremlau.net>
  7. * @website stremlau.net/html5_notifier
  8. * @licence GNU GPL
  9. *
  10. **/
  11. function rcmail_show_notification(message)
  12. {
  13. if (use_notifications)
  14. {
  15. if ("Notification" in window) {
  16. var notification = new Notification(rcmail.gettext('notification_title', 'html5_notifier').replace('[from]', message.from), {
  17. icon: './plugins/html5_notifier/images/new_mail.png',
  18. body: message.subject
  19. });
  20. notification.onclick = function() {
  21. if(message.opentype == '1') {
  22. rcmail.open_window('?_task=mail&_action=show&_uid='+message.uid);
  23. } else {
  24. window.open('?_task=mail&_action=show&_extwin=1&_uid='+message.uid);
  25. }
  26. }
  27. if (parseInt(message.duration) > 0)
  28. {
  29. setTimeout(function(){ notification.close(); }, (parseInt(message.duration)*1000));
  30. }
  31. }
  32. }
  33. }
  34. function rcmail_browser_notifications()
  35. {
  36. if ("Notification" in window && Notification.permission) {
  37. if (Notification.permission === "granted") {
  38. rcmail.display_message(rcmail.gettext('ok_notifications', 'html5_notifier'), 'notice');
  39. }
  40. else {
  41. Notification.requestPermission(rcmail_check_notifications);
  42. }
  43. }
  44. else if (window.webkitNotifications) {
  45. if (window.webkitNotifications.checkPermission() == 0)
  46. {
  47. rcmail.display_message(rcmail.gettext('ok_notifications', 'html5_notifier'), 'notice');
  48. }
  49. else
  50. {
  51. window.webkitNotifications.requestPermission(rcmail_check_notifications);
  52. }
  53. }
  54. else
  55. {
  56. rcmail.display_message(rcmail.gettext('no_notifications', 'html5_notifier'), 'error');
  57. }
  58. }
  59. function rcmail_browser_notifications_test() {
  60. if (use_notifications)
  61. {
  62. rcmail.display_message(rcmail.gettext('check_ok', 'html5_notifier'), 'notice');
  63. var message = new Object();
  64. message.duration = 8;
  65. message.uid = 0;
  66. message.subject = 'It Works!';
  67. message.from = 'TESTMAN';
  68. message.opentype = $('select[name=_html5_notifier_popuptype]').val();
  69. rcmail_show_notification(message);
  70. }
  71. else
  72. {
  73. if ("Notification" in window && Notification.permission) {
  74. if (Notification.permission == 'denied') {
  75. rcmail.display_message(rcmail.gettext('check_fail_blocked', 'html5_notifier'), 'error');
  76. return false;
  77. }
  78. }
  79. else if (window.webkitNotifications)
  80. {
  81. if (window.webkitNotifications.checkPermission() == 2)
  82. {
  83. rcmail.display_message(rcmail.gettext('check_fail_blocked', 'html5_notifier'), 'error');
  84. return false;
  85. }
  86. }
  87. rcmail.display_message(rcmail.gettext('check_fail', 'html5_notifier'), 'error');
  88. }
  89. }
  90. function rcmail_browser_notifications_colorate() {
  91. if ("Notification" in window && Notification.permission) {
  92. var broco = $('#rcmfd_html5_notifier_browser_conf');
  93. if (broco)
  94. {
  95. switch (Notification.permission)
  96. {
  97. case 'granted': broco.css('color', 'green'); break;
  98. case 'default': broco.css('color', 'orange'); break;
  99. case 'denied': broco.css('color', 'red'); break;
  100. }
  101. }
  102. }
  103. else if (window.webkitNotifications)
  104. {
  105. var broco = $('#rcmfd_html5_notifier_browser_conf');
  106. if (broco)
  107. {
  108. switch (window.webkitNotifications.checkPermission())
  109. {
  110. case 0: broco.css('color', 'green'); break;
  111. case 1: broco.css('color', 'orange'); break;
  112. case 2: broco.css('color', 'red'); break;
  113. }
  114. }
  115. }
  116. }
  117. var use_notifications = false;
  118. var rcmail_check_notifications = function(e)
  119. {
  120. if ("Notification" in window && Notification.permission) {
  121. if (Notification.permission === "granted") {
  122. use_notifications = true;
  123. }
  124. }
  125. else if (window.webkitNotifications)
  126. {
  127. if (window.webkitNotifications.checkPermission() == 0) {
  128. use_notifications = true;
  129. }
  130. }
  131. rcmail_browser_notifications_colorate();
  132. }
  133. if (window.rcmail)
  134. {
  135. rcmail.addEventListener('plugin.showNotification', rcmail_show_notification);
  136. rcmail.addEventListener('init', rcmail_check_notifications);
  137. }