Google Search Plus

Google搜索增强

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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();
})();