ChatWork Notification

ChatWork notification for Scriptish

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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
    });
})();