[Brick-Kill] User Notifs

Notifies when a user is online.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// discord.gg/JjszyaD63A

// ==UserScript==
// @name         [Brick-Kill] User Notifs
// @version      1.03
// @description  Notifies when a user is online.
// @author       Spacekiller
// @match        *://www.brick-hill.com/*
// @icon         https://www.brick-hill.com/favicon.ico
// @license      MIT
// @namespace    bhusernotif
// @grant        GM_xmlhttpRequest
// @grant        GM_notification
// @grant        GM.setValue
// @grant        GM.getValue
// @connect      brick-hill.com
// @connect      api.brick-hill.com
// ==/UserScript==

(function () {
    'use strict';

    /*-    SETTINGS    -*/

    const userIds = [ // List of user's IDs you want notifications for being online. Defaulted to admins.
        59,
        4787,
        7175,
        51918,
        64562,
        184808
    ];

    /*-                -*/

    const userProfileUrlTemplate = 'https://api.brick-hill.com/v1/user/profile?id=';
    const userStatusUrlTemplate = 'https://www.brick-hill.com/user/';
    const userStatus = {};

    async function initializeUserStatus() {
        for (const userId of userIds) {
            userStatus[userId] = await GM.getValue(userId, {
                online: false,
                notified: false
            });
        }
    }

    function updateUserStatus(userId, status) {
        userStatus[userId] = status;
        GM.setValue(userId, status);
    }

    function checkUserStatus(userId) {
        const userProfileUrl = `${userProfileUrlTemplate}${userId}`;
        const userStatusUrl = `${userStatusUrlTemplate}${userId}`;

        GM_xmlhttpRequest({
            method: 'GET',
            url: userProfileUrl,
            onload: function (response) {
                const userProfile = JSON.parse(response.responseText);
                const username = userProfile.username;

                GM_xmlhttpRequest({
                    method: 'GET',
                    url: userStatusUrl,
                    onload: function (response) {
                        const parser = new DOMParser();
                        const doc = parser.parseFromString(response.responseText, 'text/html');
                        const statusDot = doc.querySelector('.status-dot');
                        const isOnline = statusDot && statusDot.classList.contains('online');

                        if (userStatus[userId].online !== isOnline) {
                            const status = {
                                online: isOnline,
                                notified: true
                            };
                            updateUserStatus(userId, status);

                            GM_notification({
                                title: `User ${isOnline ? 'Online' : 'Offline'}`,
                                text: `${username} is ${isOnline ? 'online' : 'offline'}`,
                                timeout: 5000,
                                onclick: function () {
                                    window.open(userStatusUrl);
                                },
                            });
                        }
                    }
                });
            }
        });
    }

    async function checkAllUsers() {
        for (const userId of userIds) {
            checkUserStatus(userId);
        }
    }

    (async function () {
        await initializeUserStatus();
        setInterval(checkAllUsers, 5000);
    })();
})();