ChatWork Notification

ChatWork notification for Scriptish

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @id             chatwork_notification
// @name           ChatWork Notification
// @version        1.0
// @namespace      http://efcl.info/
// @author         azu
// @description    ChatWork notification for Scriptish
// @include        https://www.chatwork.com/*
// @run-at         window-load
// @icon           https://www.chatwork.com/favicon.ico
// ==/UserScript==
(function(){
    var prevUnreadCount = 0;
    var roomList = document.getElementById("cw_roomlist_items");
    if (!roomList){
        console.log("チャット画面ではない?");
        return;
    }
    /*
     http://domes.lingua.heliohost.org/webapi/intro-domcore1.html
     https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/
     */
    var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

    var openChatWork = function(){
        GM_openInTab(location.href, false, true);
    };

    // https://github.com/ento/chatwork-userscripts
    function getUnreadCount(){
        return unsafeWindow.RL.unread_sum.contact;
    }

    var observer = new MutationObserver(function(mutations){
        mutations.forEach(function(mutation){
            if (mutation.type === 'childList'){
                var unreadCount = getUnreadCount()
                // roomに新規メッセージがついた場合
                if (prevUnreadCount < unreadCount){
                    GM_notification("new message", "ChatWork", null, openChatWork);
                }
                prevUnreadCount = unreadCount;
            }
        });
    });

    observer.observe(roomList, {
        attributes : false,
        childList : true,
        characterData : false
    });
})();