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

如何用JavaScript向Discord搜索栏插入文本并触发搜索?

问题:Tampermonkey脚本插入Discord搜索栏文本后交互异常

我正在开发一个Tampermonkey脚本,目标是将预定义字符串插入Discord搜索栏并触发搜索。目前文本已成功插入搜索栏,但存在以下问题:

  • 插入的文本无法交互,且未触发搜索,文本直接覆盖在搜索栏的提示文字上方
  • 手动使用搜索栏正常,但脚本执行后,在搜索栏内手动输入的文本会被隐藏,不过搜索结果能正确显示手动输入的内容(而非脚本插入的显示文本)

我曾尝试跳过搜索栏直接调用搜索函数,但未能找到对应的搜索函数。

我尝试的代码

function performSearch() {
    const searchString = 'Search String';
    const searchBar = document.querySelector('.search_ff5f90 .public-DraftEditor-content');

    if (searchBar) {
        searchBar.focus();
        searchBar.textContent = searchString;

        const inputEvent = new Event('input', { bubbles: true });
        searchBar.dispatchEvent(inputEvent);

        const enterEvent = new KeyboardEvent('keydown', {
            bubbles: true,
            cancelable: true,
            key: 'Enter',
            keyCode: 13,
        });
        searchBar.dispatchEvent(enterEvent);
    }
}

搜索栏不同状态的HTML

空搜索栏

<div class="search_ff5f90"><div class="search_a46bef"><div class="searchBar_a46bef"><div class="DraftEditor-root"><div class="public-DraftEditorPlaceholder-root"><div class="public-DraftEditorPlaceholder-inner" id="placeholder-8p48t" style="white-space: pre-wrap;">Search</div></div><div class="DraftEditor-editorContainer"><div aria-describedby="placeholder-8p48t" aria-expanded="false" aria-label="Search" autocorrect="off" class="notranslate public-DraftEditor-content" contenteditable="true" role="combobox" spellcheck="false" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;" aria-haspopup="listbox"><div data-contents="true"><div class="" data-block="true" data-editor="8p48t" data-offset-key="fkrf5-0-0"><div data-offset-key="fkrf5-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="fkrf5-0-0"><br data-text="true"></span></div></div></div></div></div></div><div class="icon_a46bef iconLayout_effbe2 small_effbe2" tabindex="-1" aria-hidden="true" aria-label="Clear search" role="button"><div class="iconContainer_effbe2"><svg class="icon_effbe2 visible_effbe2" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path fill="currentColor" fill-rule="evenodd" d="M15.62 17.03a9 9 0 1 1 1.41-1.41l4.68 4.67a1 1 0 0 1-1.42 1.42l-4.67-4.68ZM17 10a7 7 0 1 1-14 0 7 7 0 0 1 14 0Z" clip-rule="evenodd" class=""></path></svg><svg class="icon_effbe2" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path fill="currentColor" d="M17.3 18.7a1 1 0 0 0 1.4-1.4L13.42 12l5.3-5.3a1 1 0 0 0-1.42-1.4L12 10.58l-5.3-5.3a1 1 0 0 0-1.4 1.42L10.58 12l-5.3 5.3a1 1 0 1 0 1.42 1.4L12 13.42l5.3 5.3Z" class=""></path></svg></div></div></div></div></div>

手动添加示例字符串后的搜索栏

<div class="search_ff5f90"><div class="search_a46bef open_a46bef"><div class="searchBar_a46bef"><div class="DraftEditor-root"><div class="DraftEditor-editorContainer"><div aria-describedby="placeholder-8p48t" aria-expanded="false" aria-label="Search" autocorrect="off" class="notranslate public-DraftEditor-content" contenteditable="true" role="combobox" spellcheck="false" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;" aria-haspopup="listbox" aria-activedescendant="uid_10--1"><div data-contents="true"><div class="" data-block="true" data-editor="8p48t" data-offset-key="fkrf5-0-0"><div data-offset-key="fkrf5-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="fkrf5-0-0"><span data-text="true">Example String</span></span></div></div></div></div></div></div><div class="icon_a46bef iconLayout_effbe2 small_effbe2 clear_effbe2" tabindex="0" aria-hidden="false" aria-label="Clear search" role="button"><div class="iconContainer_effbe2"><svg class="icon_effbe2" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path fill="currentColor" fill-rule="evenodd" d="M15.62 17.03a9 9 0 1 1 1.41-1.41l4.68 4.67a1 1 0 0 1-1.42 1.42l-4.67-4.68ZM17 10a7 7 0 1 1-14 0 7 7 0 0 1 14 0Z" clip-rule="evenodd" class=""></path></svg><svg class="icon_effbe2 visible_effbe2" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path fill="currentColor" d="M17.3 18.7a1 1 0 0 0 1.4-1.4L13.42 12l5.3-5.3a1 1 0 0 0-1.42-1.4L12 10.58l-5.3-5.3a1 1 0 0 0-1.4 1.42L10.58 12l-5.3 5.3a1 1 0 1 0 1.42 1.4L12 13.42l5.3 5.3Z" class=""></path></svg></div></div></div></div></div>

脚本插入示例字符串后的搜索栏

<div class="search_ff5f90"><div class="search_a46bef"><div class="searchBar_a46bef"><div class="DraftEditor-root"><div class="public-DraftEditorPlaceholder-root"><div class="public-DraftEditorPlaceholder-inner" id="placeholder-anc41" style="white-space: pre-wrap;">Search</div></div><div class="DraftEditor-editorContainer"><div aria-describedby="placeholder-anc41" aria-expanded="false" aria-label="Search" autocorrect="off" class="notranslate public-DraftEditor-content" contenteditable="true" role="combobox" spellcheck="false" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;" aria-haspopup="listbox"><div data-contents="true"><div data-block="true" data-editor="ncf4zujpt0i" data-offset-key="ncf4zujpt0i-0-0"><div data-offset-key="ncf4zujpt0i-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="ncf4zujpt0i-0-0"><span data-text="true">G• example string</span></span></div></div></div></div></div></div><div class="icon_a46bef iconLayout_effbe2 small_effbe2" tabindex="-1" aria-hidden="true" aria-label="Clear search" role="button"><div class="iconContainer_effbe2"><svg class="icon_effbe2 visible_effbe2" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path fill="currentColor" fill-rule="evenodd" d="M15.62 17.03a9 9 0 1 1 1.41-1.41l4.68 4.67a1 1 0 0 1-1.42 1.42l-4.67-4.68ZM17 10a7 7 0 1 1-14 0 7 7 0 0 1 14 0Z" clip-rule="evenodd" class=""></path></svg><svg class="icon_effbe2" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path fill="currentColor" d="M17.3 18.7a1 1 0 0 0 1.4-1.4L13.42 12l5.3-5.3a1 1 0 0 0-1.42-1.4L12 10.58l-5.3-5.3a1 1 0 0 0-1.4 1.42L10.58 12l-5.3 5.3a1 1 0 1 0 1.42 1.4L12 13.42l5.3 5.3Z" class=""></path></svg></div></div></div></div></div>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 21:37:32