去除B站搜索框中的文字

删除B站(哔哩哔哩)搜索框中的文字。

// ==UserScript==
// @name         去除B站搜索框中的文字
// @namespace    http://tampermonkey.net/
// @version      0.3.0
// @description  删除B站(哔哩哔哩)搜索框中的文字。
// @author       grch12
// @match        *.bilibili.com/*
// @icon         none
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    function doSearch(text) {
        var link = document.createElement("a");
        link.href = "https://search.bilibili.com/all?keyword=" + encodeURIComponent(text);
        link.target = "_blank";
        link.click();
    }

    var observer = new MutationObserver(() => {
        var normalInput = document.querySelector(".nav-search-input") || document.querySelector(".nav-search-keyword");
        if (normalInput) {
            var isNewHp = normalInput.classList.contains("nav-search-input");
            observer.disconnect();
            normalInput.remove();
            var input = document.createElement("input");
            var form = document.querySelector("form#nav-searchform") || document.querySelector("form#nav_searchform");
            input.autocomplete = "off";
            input.placeholder = "搜索...";
            if (isNewHp) {
                input.classList.add("nav-search-input");
                input.style.height = "100%";
                input.style.borderRadius = "6px";
                input.style.margin = "0";
                input.style.paddingLeft = "8px";
                input.name = "keyword";
                document.querySelector(".nav-search-content").insertAdjacentElement("afterbegin", input);
            } else {
                input.classList.add("nav-search-keyword");
                form.insertAdjacentElement("afterbegin", input);
            }
            document.querySelector(".nav-search-btn").addEventListener("click", (e) => {
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();
                doSearch(input.value);
            }, true);
            form.addEventListener("submit", (e) => {
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();
                doSearch(input.value);
            }, true);
        }
    });
    observer.observe(document.body, { childList: true });
})()