Greasy Fork

Greasy Fork is available in English.

隐藏搜索历史

隐藏搜索引擎下拉框的历史记录,目前仅适配Google

当前为 2021-04-27 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         隐藏搜索历史
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  隐藏搜索引擎下拉框的历史记录,目前仅适配Google
// @homeurl      https://github.com/dengyuhan/LardMonkeyScripts
// @homeurl      http://greasyfork.icu/zh-CN/scripts/392368
// @author       xiandanin
// @include      *://encrypted.google.*/search*
// @include      *://*.google*/
// @include      *://*.google*/search*
// @include      *://*.google*/webhp*
// @exclude      *://*.google*/sorry*
// @grant        none
// ==/UserScript==
(function () {
    'use strict'

    function findTagByParent (element, tagName) {
        let currentElement = element
        while (currentElement.tagName.toLowerCase() !== tagName.toLowerCase() && currentElement !== document.body) {
            currentElement = currentElement.parentNode
        }
        return currentElement !== element ? currentElement : null
    }

    function deleteFunc () {
        let deleteElements = Array.from(document.getElementsByClassName("sbai sbdb"))
        deleteElements.filter(element => {
            return element.style.display !== 'none'
        }).flatMap(element => {
            return findTagByParent(element, 'li')
        }).forEach(element => {
            console.info("删除搜索记录", element.querySelector("span").innerText)
            element.remove()
        })
    }

    let inputElements = document.getElementsByName("q")
    for (let i = 0; i < inputElements.length; i++) {
        inputElements[i].oninput = function () {
            setTimeout(deleteFunc, 150)
        }
    }

    let target = document.getElementsByClassName("UUbT9")[0]
    let observer = new MutationObserver(function (mutations, observer) {
        deleteFunc()
    })
    observer.observe(target, {
        characterData: true,
        childList: true,
        subtree: true
    })

})()