B++

Remove the random recommendations, bottom bar, sidebar, microphone, and search optimization from the Bing search page. Remove the website logo and switch to a dual-column search result layout. Automatically redirect to the correct Baidu Tieba page. 移除必应搜索页面莫名其妙的推荐、底部栏、侧边栏、麦克风、优化搜索等,去除网页logo,改成双列搜索结果,百度贴吧自动正确跳转

// ==UserScript==
// @name         B++
// @name:zh-CN   B艹:必应搜索页面大修
// @name:en      B++: Bing Search Page Overhaul
// @namespace    Bing Plus Plus
// @version      v2.236067977
// @description:zh-CN 移除必应搜索页面大量元素,去除网页 logo,改成双列瀑布流结果,百度贴吧自动正确跳转,自动连续到下一页
// @description:en  Remove a large number of elements on the Bing search page, remove the webpage logo, change to a two-column waterfall layout for the results, ensure Baidu Tieba automatically redirects correctly, and automatically continue to the next page.
// @author       Yog-Sothoth
// @match        https://*.bing.com/search*
// @grant        GM_addStyle
// @license      MIT
// @description Remove the random recommendations, bottom bar, sidebar, microphone, and search optimization from the Bing search page. Remove the website logo and switch to a dual-column search result layout. Automatically redirect to the correct Baidu Tieba page. 移除必应搜索页面莫名其妙的推荐、底部栏、侧边栏、麦克风、优化搜索等,去除网页logo,改成双列搜索结果,百度贴吧自动正确跳转
// ==/UserScript==

(function () {
    'use strict';

    /**
     * 删除指定选择器匹配的所有元素。
     */
    function removeElement(selector) {
        const elements = document.querySelectorAll(selector);
        elements.forEach(element => element.remove());
    }

    /**
     * 修正 Bing 搜索结果中贴吧链接的 URL,
     * 将 "jump2.bdimg" 或 "jump.bdimg" 替换为 "tieba.baidu"。
     */
    function replace() {
        let as = document.querySelectorAll('#b_content .b_algo h2 a');
        let as2 = document.querySelectorAll('#b_content .b_algo .b_tpcn .tilk');

        for (let i = 0; i < as.length; i++) {
            let url = as[i].getAttribute('href');
            let new_url = url.replace(/jump2\.bdimg|jump\.bdimg/, 'tieba.baidu');
            as[i].setAttribute('href', new_url);
            as2[i].setAttribute('href', new_url);
        }
    }

    /**
     * 应用双列紧凑瀑布流布局的 CSS 样式。
     */
    const css = `
        #b_context { display: none; } /* 隐藏 Bing 侧边栏 */
        #b_content { padding: 30px 15px !important; } /* 调整搜索内容区域边距 */
        #b_results { display: flex; flex-wrap: wrap; width: 100% !important; } /* 结果列表流式布局 */
        #b_results > li { width: 40%; margin-right: 50px; } /* 搜索结果项宽度与间距 */
        .b_pag, .b_ans { width: 100% !important; } /* 分页和答案区域全宽 */
        #b_results .ContentItem { display: inline-flex; flex-wrap: wrap; width: 40%; } /* 文章项布局 */
        #b_results .MainContent_Sub_Left_MainContent { max-width: 100% !important; } /* 主内容最大化适应 */
    `;
    GM_addStyle(css);

    /**
     * 需要移除的页面元素选择器列表。
     */
    const elementsToRemove = [
        '.b_ans', '.b_ans .b_mop', '.b_vidAns', '.b_rc_gb_sub.b_rc_gb_sub_section',
        '.b_rc_gb_scroll', '.b_msg', '.b_footer', '.b_phead_sh_link',
        '.b_sh_btn-io', '#id_mobile', '[aria-label="更多结果"]', '.b_algoRCAggreFC',
        '.b_factrow b_twofr', '[id^="mic_"]', '[class="tpic"]',
        '[class="b_vlist2col b_deep"]', '[class="b_deep b_moreLink "]',
        '.b_algo b_vtl_deeplinks', '[class="tab-head HeroTab"]',
        '[class="tab-menu tab-flex"]', '[class="b_deepdesk"]',
        '[class^="b_algo b_algoBorder b_rc_gb_template b_rc_gb_template_bg_"]',
        '[class="sc_rf"]', '[class="b_algospacing"]', '#b_pole',
        '.b_caption.b_rich', '.b_ad.b_adMiddle', '.b_ad.b_adBottom',
        '.b_imagePair.wide_wideAlgo .inner', '.b_imagePair.wide_wideAlgo[rel="dns-prefetch"]'
    ];

    /**
     * 清理大多数大的块状结果(模板类内容)。
     */
    function updateClassForBAlgoElements() {
        const bContent = document.getElementById('b_results');
        if (bContent) {
            Array.from(bContent.children).forEach(element => {
                if (element.classList.contains('b_algo') && element.classList.contains('b_rc_gb_template')) {
                    element.classList.remove(...[...element.classList].filter(cls => cls.startsWith('b_rc_gb_template_bg_')));
                    element.classList.add('b_algo');
                }
            });
        }
    }

    /**
     * 简化 Bing 搜索 URL,保留 "q" 和 "first" 参数。
     */
    (function cleanCurrentUrl() {
        const urlObj = new URL(window.location.href);
        const params = urlObj.searchParams;
        const keepParams = new Set(['q', 'first']);

        for (const key of [...params.keys()]) {
            if (!keepParams.has(key)) {
                params.delete(key);
            }
        }

        const newUrl = urlObj.origin + urlObj.pathname + urlObj.search;
        window.history.replaceState(null, '', newUrl);
    })();

    /**
     * 自动连续加载下一页搜索结果。
     */
    function processBingSearchPage() {
        const urlParams = new URLSearchParams(window.location.search);
        let first = parseInt(urlParams.get('first'), 10) || 1;
        const query = urlParams.get('q');
        const resultsContainer = document.getElementById('b_results');

        if (first === 1) fetchResults(5);

        let isFetching = false;

        window.addEventListener('scroll', () => {
            if ((window.innerHeight + window.scrollY) >= (document.body.offsetHeight - 300)) {
                if (!isFetching) {
                    isFetching = true;
                    first += 10;
                    fetchResults(first).then(() => {
                        isFetching = false;
                    });
                }
            }
        });

        function fetchResults(pageFirst) {
            return fetch(`https://www4.bing.com/search?q=${encodeURIComponent(query)}&first=${pageFirst}`)
                .then(response => response.text())
                .then(data => {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(data, 'text/html');
                    const newResultsContainer = doc.getElementById('b_results');
                    if (!newResultsContainer) return;

                    const newResults = newResultsContainer.querySelectorAll('.b_algo');
                    if (newResults.length === 0) return;

                    const currentResults = resultsContainer.querySelectorAll('.b_algo');
                    const lastAlgo = currentResults[currentResults.length - 1];

                    newResults.forEach(result => {
                        const cloned = result.cloneNode(true);
                        cloned.style.opacity = '0';
                        cloned.style.transition = 'opacity 0.5s ease';

                        if (lastAlgo) {
                            lastAlgo.insertAdjacentElement('afterend', cloned);
                        } else {
                            resultsContainer.appendChild(cloned);
                        }

                        requestAnimationFrame(() => {
                            cloned.style.opacity = '1';
                        });
                    });
                })
                .catch(error => console.error('Error fetching results:', error));
        }
    }

    /**
     * 移除宽图布局的 class。
     */
    function cleanWideWideAlgo() {
        const captionCards = document.querySelectorAll('.captionMediaCard');
        captionCards.forEach(card => {
            const wideElements = card.querySelectorAll('.b_imagePair.wide_wideAlgo');
            wideElements.forEach(elem => {
                elem.classList.remove('wide_wideAlgo');
            });
        });
    }

    /**
     * 自动将 Bing 的 "www." 或 "cn." 前缀替换为 "www4.",避免白屏问题。
     */
    function redirectTowww4IfNeeded() {
        const urlObj = new URL(window.location.href);
        if (/^(www\.|cn\.)/.test(urlObj.hostname)) {
            window.location.href = `https://www4.${urlObj.hostname.replace(/^(www\.|cn\.)/, '')}${urlObj.pathname}${urlObj.search}`;
        }
    }

    // ===== 初始化调用 =====
    redirectTowww4IfNeeded();
    updateClassForBAlgoElements();
    elementsToRemove.forEach(removeElement);
    replace();
    processBingSearchPage();
    cleanWideWideAlgo();

    /**
     * 监听 DOM 变化,每次变化时重新删除指定页面元素。
     */
    const observer = new MutationObserver(mutations => {
        elementsToRemove.forEach(removeElement);
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === 1) { // 元素节点
                    cleanWideWideAlgo(node);
                }
            });
        });
    });
    observer.observe(document.body, { childList: true, subtree: true });

    /**
     * 监听页面 pushState 变化,及时重新修正贴吧跳转链接。
     */
    const _pushState = window.history.pushState;
    window.history.pushState = function () {
        replace();
        console.log('History changed');
        return _pushState.apply(this, arguments);
    };
})();