在浏览器地址栏直接向 ChatGLM 提问

ChatGLM automatically generates questions based on URL search.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         在浏览器地址栏直接向 ChatGLM 提问
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  ChatGLM automatically generates questions based on URL search.
// @author       Your Name
// @match        https://chatglm.cn/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';
    if (document.location.origin === 'https://chatglm.cn' && document.location.pathname === '/main/alltoolsdetail') {
        const url = new URL(document.location.href)
        const keywords = url.searchParams.get("wd")

        if (keywords) {
            const timer = setInterval(() => {
                const textarea = document.getElementsByTagName("textarea")[0]
                if (!textarea) {
                    return
                }
                {
                    clearInterval(timer)

                    // 调用函数移除名为"wd"的查询参数
                    removeSearchParam('wd');
                }
                textarea.value = keywords
                const event = new Event('input', {
                    bubbles: true,
                    cancelable: true,
                });
                textarea.dispatchEvent(event);
                // 创建一个回车键的事件
                const enterKeyEvent = new KeyboardEvent('keydown', {
                    'keyCode': 13, // 13是回车键的键码
                    'key': 'Enter',
                    'bubbles': true,
                    'cancelable': true
                });

                // 派发事件到textarea元素
                textarea.dispatchEvent(enterKeyEvent);

                // 通常,你还需要触发'keypress'和'keyup'事件,但'keydown'通常是触发实际行为所必需的
                const enterKeyPressEvent = new KeyboardEvent('keypress', {
                    'keyCode': 13,
                    'key': 'Enter',
                    'bubbles': true,
                    'cancelable': true
                });
                textarea.dispatchEvent(enterKeyPressEvent);

                const enterKeyUpEvent = new KeyboardEvent('keyup', {
                    'keyCode': 13,
                    'key': 'Enter',
                    'bubbles': true,
                    'cancelable': true
                });
                textarea.dispatchEvent(enterKeyUpEvent);
            })
        }

    }

    // 函数用于移除URL中的特定查询参数
    function removeSearchParam(paramName) {
        // 获取当前URL的查询字符串部分
        const searchParams = new URLSearchParams(window.location.search);

        // 移除名为paramName的查询参数
        searchParams.delete(paramName);

        // 构造新的URL
        const newUrl = window.location.pathname + (searchParams.toString() ? '?' + searchParams.toString() : '');

        // 使用history.pushState()更新URL,不会重新加载页面
        window.history.pushState({ path: newUrl }, '', newUrl);
    }



})();