Plug.DJ Chat Notifications

Shows desktop notification for new chat messages

  1. // ==UserScript==
  2. // @name Plug.DJ Chat Notifications
  3. // @icon http://i.imgur.com/0LwVHyl.png
  4. // @namespace x4_plugdjcn
  5. // @version 0.2.1
  6. // @description Shows desktop notification for new chat messages
  7. // @author x4fab
  8. // @license CC0
  9. // @match https://plug.dj/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. !function (){
  14. var windowOpen = window.open,
  15. opened;
  16. window.open = function (){
  17. opened = windowOpen.apply(window, arguments);
  18. return opened;
  19. };
  20.  
  21. if (Notification.permission == 'default'){
  22. document.body.onclick = function (){
  23. Notification.requestPermission();
  24. document.body.onclick = null;
  25. };
  26. }
  27.  
  28. var lastId;
  29. setInterval(function (){
  30. if (opened && opened.closed){
  31. opened = null;
  32. }
  33. var node = (opened ? opened.document : document).querySelector('#chat-messages .cm.message:not(.from-you):last-child .msg');
  34. if (!node){
  35. return;
  36. }
  37.  
  38. var currentId = node.querySelector('.text').className;
  39. if (lastId != currentId){
  40. var focused = opened && opened.document.hasFocus() || document.hasFocus();
  41. if (!focused){
  42. var notification = new Notification(node.querySelector('.un').textContent, {
  43. body: node.querySelector('.text').innerHTML.replace(/^[\s\S]*<br>/, '').replace(/<.+?>/g, ''),
  44. icon: 'http://i.imgur.com/0LwVHyl.png'
  45. });
  46. setTimeout(notification.close.bind(notification), 3e3);
  47. }
  48. lastId = currentId;
  49. }
  50. }, 50);
  51. }();