Plug.DJ Chat Notifications

Shows desktop notification for new chat messages.

当前为 2015-05-10 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Plug.DJ Chat Notifications
// @icon         http://i.imgur.com/0LwVHyl.png
// @namespace    x4_plugdjcn
// @version      0.1
// @description  Shows desktop notification for new chat messages.
// @author       x4fab
// @license      CC0
// @match        https://plug.dj/*
// @grant        none
// ==/UserScript==

!function (){
    var windowOpen = window.open,
        opened;
    
    window.open = function (){
        opened = windowOpen.apply(window, arguments);
        return opened;
    };

    if (Notification.permission == 'default'){
        document.body.onclick = function (){
            Notification.requestPermission();
            document.body.onclick = null;
        };
    }

    var lastId;
    setInterval(function (){
        if (opened && opened.closed){
            opened = null;
        }
        
        var node = (opened ? opened.document : document).querySelector('#chat-messages .cm.message:not(.from-you):last-child .msg .text');
        if (!node){
            return;
        }

        var currentId = node.querySelector('.text').className;
        if (lastId != currentId){
            var focused = opened && opened.document.hasFocus() || document.hasFocus();
            
            if (!focused){
                var notification = new Notification(node.querySelector('.un').textContent, { 
                    body: node.querySelector('.text').innerHTML.replace(/^[\s\S]*<br>/, '').replace(/<.+?>/g, ''), 
                    icon: 'http://i.imgur.com/0LwVHyl.png' 
                });
                
                setTimeout(notification.close.bind(notification), 3e3);
            }
            
            lastId = currentId;
        } 
    }, 50);
}();