Plug.DJ Chat Notifications

Shows desktop notification for new chat messages

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Plug.DJ Chat Notifications
// @icon         http://i.imgur.com/0LwVHyl.png
// @namespace    x4_plugdjcn
// @version      0.2.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');
        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);
}();