Google Search Plus

Google搜索增强

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google Search Plus
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Google搜索增强
// @author       ChenXu
// @license      MIT
// @include      *://www.google.com*/search*
// @include      *://scholar.google.com*/scholar*
// @grant        none
// ==/UserScript==

(function () {
    "use strict";

    const googleSearchPattern = /^https?:\/\/www\.google\.[^\/]+\/search\?/;
    const scholarSearchPattern = /^https?:\/\/scholar\.google\.[^\/]+\/scholar\?/;

    function checkAndAddFilterParam() {
        const currentUrl = window.location.href;

        if (googleSearchPattern.test(currentUrl)) {
            const url = new URL(currentUrl);
            if (!url.searchParams.has('filter')) {
                url.searchParams.set('filter', '0');
                if (url.href !== currentUrl) {
                    window.location.replace(url.href);
                }
            }
        }
    }

    const observer = new MutationObserver((mutations) => {
        processSearchResults();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

    function processSearchResults() {
        const searchResults = document.querySelectorAll("a");

        searchResults.forEach((url) => {
            if (url.dataset.processed) return;
            if (!url.href || isGoogleSearchLink(url.href)) return;
            url.dataset.processed = "true";
            url.setAttribute("target", "_blank");
            url.setAttribute("rel", "noopener noreferrer");
        });
    }

    function isGoogleSearchLink(href) {
        return googleSearchPattern.test(href) || scholarSearchPattern.test(href);
    }

    checkAndAddFilterParam();
    processSearchResults();
})();