ZhChat增强脚本

增强功能:自动登录

目前為 2023-03-25 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ZhChat增强脚本
// @namespace    http://tampermonkey.net/
// @version      2.2.114.514
// @description  增强功能:自动登录
// @author       UbisoComes (GreenDebug)
// @match        https://chat.zhangsoft.cf/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zhangsoft.cf
// @license      MIT
// @run-at       document-end
// @grant        unsafeWindow
// @grant none
// ==/UserScript==

(function () {

    const local = {
        set: (key, value) => localStorage.setItem(key, value),
        remove: (key) => localStorage.removeItem(key),
        get: (key) => localStorage.getItem(key),
        exist: (key) => localStorage.getItem(key) != undefined,

    };

    const dev = window.console;
    window.dev = dev;
    const doc = window.document;
    const $_ = (q) => doc.querySelectorAll(q);
    const $ = (d) => doc.querySelector(d);

    const SideBarUI = {

        add: (element) => {
            let side = $('#sidebar-content');
            side.insertBefore(element, side.children[1]);
        },
        remove: (filther) => {
            let side = $('#sidebar-content');
            for (let i in side.children) {
                let item = side.children[i];
                if (filther(item)) side.removeChild(item);
            }
        }
    };
    function hookLogin() {


        //hook
        let login = async (nick) => {
            local.set('my-nick', nick);
            setTimeout(() => { window.myNick = nick; }, 100);
            await getMurmur();
            var sendMurmur = encode(window.myMurmur).toString();
            send({ cmd: 'join', channel: window.myChannel, nick: nick, client: 'ZhangChatClient', murmur: sendMurmur });
        };
        let orgin = () => {

            var newNick = prompt('输入昵称:', local.get('my-nick') || '');

            login(newNick);
        };

        let autoLogin = () => {
            if (local.get('autoLogin') != 'true') return false;
            if (!local.exist('my-nick')) return false;
            let nick = local.get('my-nick');

            login(nick);

            return true;
        }
        if (!autoLogin()) orgin();
    }

    function autoLoginUI() {
        let p = doc.createElement('p'),
            input = doc.createElement('input'),
            label = doc.createElement('label');
        input.setAttribute('type', 'checkbox');
        input.setAttribute('id', 'autoLogin');
        input.onchange = (e) => {
            local.set('autoLogin', input.checked);
        };
        input.checked = local.get('autoLogin') == 'true';
        label.innerText = ' 自动登录';
        label.setAttribute('for', 'autoLogin');
        p.appendChild(input);
        p.appendChild(label);
        SideBarUI.add(p);
    }
    function initUI() {
        autoLoginUI();

    }
    function init() {
        try{
        initUI();}
        catch{}
        window._prompt = window.prompt;
        window.hook = hookLogin;

        window.prompt = (w, s = '') => {

            if (w.indexOf('请输入昵称') != -1) {
                window.hook();
                return;
            }
            return window._prompt(w, s);
        };
        setTimeout(() => {
            userInvite = function (nick) {
                var gotoChannel = prompt("设置一个去的频道(按取消随机)")

                send(gotoChannel ? { cmd: 'invite', nick: nick, to: gotoChannel } : { cmd: 'invite', nick: nick });

            }

        }, 1000);
    }

    init();
})();