Greasy Fork

Greasy Fork is available in English.

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

ChatGLM automatically generates questions based on URL search.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
    }



})();