Duck.ai chats search bar

Search bar in recent chats list

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name                Duck.ai chats search bar
// @namespace           https://greasyfork.org/users/821661
// @match               https://duckduckgo.com/*
// @grant               GM_addStyle
// @version             0.2.1
// @author              hdyzen
// @description         Search bar in recent chats list
// @license             GPL-3.0-only
// ==/UserScript==

const input = getInput();

function waitRecentChatList() {
    const observer = new MutationObserver(() => {
        const recentChatList = document.querySelector(".JmSIe7cXVoKWvdAQJ2iD");

        if (!location.search.includes("ia=chat") || !recentChatList || document.body.contains(input)) {
            return;
        }

        recentChatList.insertAdjacentElement("afterbegin", input);
    });

    observer.observe(document, { subtree: true, childList: true });
}
waitRecentChatList();

function filterChatsByText(searchBar, searchText) {
    let currentChatItem = searchBar.nextElementSibling;

    while (currentChatItem) {
        const title = currentChatItem.innerText.toLowerCase();
        const isMatch = title.includes(searchText);

        currentChatItem.setAttribute("match-search", isMatch.toString());

        currentChatItem = currentChatItem.nextElementSibling;
    }
}

function getInput() {
    const input = document.createElement("input");
    input.type = "text";
    input.placeholder = "Search";
    input.style =
        "width: 100%;font-size: 16px;padding-inline: 11px;height: 44px;border-radius: var(--default-border-radius);background-color: #333333;box-shadow: 0 1px 3px rgba(0,0,0,0.5);outline: none;position: sticky;top: 0;";
    input.addEventListener("input", (e) => {
        const searchText = e.target.value.toLowerCase();

        filterChatsByText(input, searchText);
    });

    return input;
}

GM_addStyle(`
.HTQSbbGLgf1_TYR3V7Ln:has([match-search]):not(:has(.JYOnH1YkhsxhT9yFGgea > [match-search="true"])), [match-search="false"] {
    display: none !important;
}
`);

/*
Search bar template

<input
    type="text"
    style="width: 100%;font-size: 16px;padding-inline: 11px;height: 44px;border-radius: var(--default-border-radius);background-color: #333333;box-shadow: 0 1px 3px rgba(0,0,0,0.5);outline: none;position: sticky;top: 0;"
    placeholder="Search"
>
*/