智谱清言(自动聚焦到输入框)Focus on Input Box on Window Focus

用来使智谱清言网页端的输入框自动聚焦

// ==UserScript==
// @name         智谱清言(自动聚焦到输入框)Focus on Input Box on Window Focus
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  用来使智谱清言网页端的输入框自动聚焦
// @author       XYZzz
// @match        https://chatglm.cn/*
// @icon        https://chatglm.cn/img/icons/favicon-32x32.png
// @grant        none
// @license      GPL-3.0
// ==/UserScript==

(function () {
    'use strict';
    // console.log(1)
    var clickCount = 0;
    var lastClickTime = 0;
    // 确定两次点击之间的最大间隔时间(毫秒)
    var maxInterval = 500;

    // 确保页面加载完成后执行
    window.addEventListener('load', function () {
        // 选择页面中的输入框
        var inputBox = document.querySelector('textarea[slot="reference"]');
        // console.log(inputBox)
        // 如果输入框存在,添加窗口焦点事件监听器
        // console.log(2)
        if (inputBox) {
            inputBox.focus();
            window.addEventListener('focus', function () {
                // 当窗口获得焦点时,将焦点聚焦到输入框
                inputBox.focus();
                setTimeout(()=>{
                    inputBox.focus();
                }, 200)
                // console.log(3)
            });


            // 监听键盘按下事件
            document.addEventListener('keydown', function (event) {
                // console.log(123321)
                // console.log(event)

                // 检查是否按下了F键
                if (event.key === 'f') {
                    // console.log(10001203)
                    var currentTime = new Date().getTime();

                    // 如果两次点击之间的时间间隔小于设定值,则增加点击计数
                    if (currentTime - lastClickTime < maxInterval) {
                        clickCount++;
                    } else {
                        clickCount = 1;
                    }

                    lastClickTime = currentTime;

                    // 如果点击次数达到2次,则聚焦到输入框
                    if (clickCount === 2) {
                        // debugger
                        setTimeout(()=>{
                            inputBox.focus();
                        }, 150)
                        clickCount = 0; // 重置点击计数
                    }
                }
            });
        }
    }, false);
})();