You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

用于Arqade评论粘贴内容的Userscript始终报‘未找到textarea’错误

问题排查与修复方案

核心错误点及对应修复

1. paste函数未接收参数,硬编码消息内容

你调用paste(MINECRAFT_MESSAGE)和paste(REGISTER_MESSAGE)时传递了不同消息,但paste函数没有定义参数,最后还固定插入MINECRAFT_MESSAGE,既会导致消息无法正确切换,也会影响后续逻辑。
修复:
修改paste函数定义,添加消息参数并替换硬编码内容:

function paste(message) {
    // ... 原有代码
    document.execCommand("insertText", false, message);
    // ... 原有代码
}

2. 评论输入框选择器错误(报错直接原因)

当前Arqade的评论输入框已不再是原生textarea,而是使用可编辑的div元素,你用querySelector("textarea")自然找不到对应元素。
修复:
替换为当前站点评论输入框的正确选择器:

// 替换原textarea查找代码
let inputElement = targetForm.querySelector(".js-comment-text-input");

3. 评论表单选择器过时

原选择器[id^='add-comment-'][data-placeholdertext]不符合当前Stack Exchange站点的DOM结构,现在评论表单容器的类名是.js-add-comment-form。
修复:
修改评论表单的查询代码:

const commentForms = document.querySelectorAll(".js-add-comment-form");

4. 多余的字符删除逻辑

代码中setTimeout里的字符删除操作毫无意义,会删掉刚插入的消息最后一个字符,直接删除这段代码即可。

5. 按键计数未重置

连续按v超过3次时,pressCount会持续累加,导致后续按键无法触发预期功能,需要在触发操作后重置计数:
修复:
在触发双击/三击逻辑后添加重置代码:

if (pressCount === 2) {
    GM_setClipboard(MINECRAFT_MESSAGE);
    paste(MINECRAFT_MESSAGE);
    pressCount = 0; // 重置计数
} else if (pressCount === 3) {
    GM_setClipboard(REGISTER_MESSAGE);
    paste(REGISTER_MESSAGE);
    pressCount = 0; // 重置计数
}

完整修复后的代码

// ==UserScript==
// @name         Easy message pasting
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Don't you dislike having to type out long canned messages, or having to find, copy, and paste them? That's annoying. Let a userscript do it for you.
// @author       Otaku
// @match        https://gaming.stackexchange.com/*
// @grant        GM_setClipboard
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    const MINECRAFT_MESSAGE = "On Minecraft commands questions, please see our [guide on what to do before asking a Minecraft commands question](https://gaming.meta.stackexchange.com/q/13507/4797), especially the part about “what you’ve tried so far”, and edit in the requested information to your post.";
    const REGISTER_MESSAGE = "Consider registering your account. This gives you certain benefits, such as being able to vote, not needing to worry about losing your account when your cookies are wiped or you use a different computer, and being able to gain reputation. For more info, see [Why should I register my account?](https://meta.stackexchange.com/questions/44557/why-should-i-register-my-account)."

    let lastPressTime = 0;
    let pressCount = 0;

    document.addEventListener("keydown", function(event) {
        if (event.key.toLowerCase() === "v") {
            const currentTime = Date.now();

            if (currentTime - lastPressTime < 1000) {
                pressCount++;
            } else {
                pressCount = 1;
            }

            lastPressTime = currentTime;

            if (pressCount === 2) {
                GM_setClipboard(MINECRAFT_MESSAGE);
                paste(MINECRAFT_MESSAGE);
                pressCount = 0;
            } else if (pressCount === 3) {
                GM_setClipboard(REGISTER_MESSAGE);
                paste(REGISTER_MESSAGE);
                pressCount = 0;
            }
        }
    });

    function paste(message) {
        // 查找评论表单
        const commentForms = document.querySelectorAll(".js-add-comment-form");

        if (!commentForms.length) {
            console.warn("No matching comment form found.");
            return;
        }

        // 取最新打开的评论表单(最后一个)
        let targetForm = commentForms[commentForms.length - 1];

        // 查找评论输入框
        let inputElement = targetForm.querySelector(".js-comment-text-input");
        if (!inputElement) {
            console.warn("No comment input found inside the target form.");
            return;
        }

        // 插入内容
        inputElement.focus();
        document.execCommand("insertText", false, message);

        // 触发输入事件更新UI
        inputElement.dispatchEvent(new Event('input', { bubbles: true }));
    }
})();

内容的提问来源于stack exchange,提问作者voided

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 01:24:50