Bing添加-site

baidu和google暂不支持

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @license      GPL
// @name         Bing添加-site
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  baidu和google暂不支持
// @author       procrastination-user@github
// @match        *://www.bing.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=juejin.cn
// @grant        unsafeWindow
// ==/UserScript==

(function () {
    'use strict';
    const searchForm = document.querySelector('form[action="/search"]');
    if (searchForm) {
        searchForm.addEventListener('submit', interceptSearch);
    }
})();

function interceptSearch(event) {
    let currentURL = window.location.href;

    let myConf = getSEConf().find(obj => currentURL.indexOf(obj.url) > -1);
    if (myConf) {
        let input = document.getElementById(myConf.inputId)
        if (input) {
            const inputValue = input.value;

            const array = getWebSiteConf()
            const prefix = " -site:";
            const separator = " , ";

            // 使用 map() 方法添加前缀
            const arrayWithPrefix = array.map(item => prefix + item);
            // 使用 join() 方法将数组转换为字符串,并添加分隔符
            const resultString = arrayWithPrefix.join(separator);

            if (input.value.indexOf(resultString) < 0) {
                input.value = inputValue + resultString;
            }
        }
    }

    // 提交搜索表单
    const formElement = document.querySelector('form[action="/search"]');
    if (formElement) {
        formElement.submit();
    }
}

function getSEConf() {
    return [
        {name: 'bing', url: 'www.bing.com', inputId: 'sb_form_q'}
    ]
}

function getWebSiteConf() {
    return [
        'csdn.net',
        'baijiahao.baidu.com'
    ]
}