ChatWork Notification

ChatWork notification for Scriptish

  1. // ==UserScript==
  2. // @id chatwork_notification
  3. // @name ChatWork Notification
  4. // @version 1.0
  5. // @namespace http://efcl.info/
  6. // @author azu
  7. // @description ChatWork notification for Scriptish
  8. // @include https://www.chatwork.com/*
  9. // @run-at window-load
  10. // @icon https://www.chatwork.com/favicon.ico
  11. // ==/UserScript==
  12. (function(){
  13. var prevUnreadCount = 0;
  14. var roomList = document.getElementById("cw_roomlist_items");
  15. if (!roomList){
  16. console.log("チャット画面ではない?");
  17. return;
  18. }
  19. /*
  20. http://domes.lingua.heliohost.org/webapi/intro-domcore1.html
  21. https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/
  22. */
  23. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  24.  
  25. var openChatWork = function(){
  26. GM_openInTab(location.href, false, true);
  27. };
  28.  
  29. // https://github.com/ento/chatwork-userscripts
  30. function getUnreadCount(){
  31. return unsafeWindow.RL.unread_sum.contact;
  32. }
  33.  
  34. var observer = new MutationObserver(function(mutations){
  35. mutations.forEach(function(mutation){
  36. if (mutation.type === 'childList'){
  37. var unreadCount = getUnreadCount()
  38. // roomに新規メッセージがついた場合
  39. if (prevUnreadCount < unreadCount){
  40. GM_notification("new message", "ChatWork", null, openChatWork);
  41. }
  42. prevUnreadCount = unreadCount;
  43. }
  44. });
  45. });
  46.  
  47. observer.observe(roomList, {
  48. attributes : false,
  49. childList : true,
  50. characterData : false
  51. });
  52. })();