[Brick-Kill] User Notifs

Notifies when a user is online.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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