google百度搜索屏蔽CSDN

自动在搜索条件后面增加 -csdn, 以此屏蔽csdn网站信息。只适用于google和百度的搜索页面

< Feedback on google百度搜索屏蔽CSDN

Review: Good - script works

§
Posted: 2024-07-05
Edited: 2024-07-05

2024-07-05 更新 search input 查询。

(function() {
    'use strict';
    const className = {
        "baidu":"form[action='/s'] s_ipt",
        "google": "form[action='/search'] textarea.gLFyf"
    };

    const key = window.location.href.indexOf("://www.baidu.com") !== -1 ? "baidu" : "google";
    const wordInput = document.querySelector(className[key]);
    if (wordInput) {
        wordInput.addEventListener("keydown", function(e) {
            if (e.key = 'Enter' && this.value.length > 0 && this.value.indexOf("-csdn") === -1) {
                this.value += " -csdn";
            }
        });
        wordInput.addEventListener("blur", function() {
            if (this.value.length > 0 && this.value.indexOf("-csdn") === -1) {
                this.value += " -csdn";
            }
        });
        wordInput.addEventListener("focus", function() {
            const index = this.value.indexOf(" -csdn");
            if (index !== -1) {
                this.value = this.value.substring(0, index);
            }
        });
        window.onbeforeunload = function() {
            const index = wordInput.value.indexOf(" -csdn");
            if (index !== -1) {
                wordInput.value = wordInput.value.substring(0, index);
            }
        }
    }
})();

Post reply

Sign in to post a reply.