Greasy Fork

Google搜索结果新标签页打开

让谷歌搜索结果在新标签页打开

// ==UserScript==
// @name         Google搜索结果新标签页打开
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  让谷歌搜索结果在新标签页打开
// @author       Your name
// @license      MIT
// @match        *://www.google.com/search*
// @match        *://www.google.com.*/search*
// @grant        none
// ==/UserScript==

(function () {
    "use strict";

    // 监听DOM变化,处理动态加载的内容
    const observer = new MutationObserver((mutations) => {
        processSearchResults();
    });

    // 开始观察文档变化
    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

    // 处理搜索结果链接
    function processSearchResults() {
        // 获取所有搜索结果链接
        const searchResults = document.querySelectorAll("a");

        searchResults.forEach((link) => {
            // 跳过已处理的链接
            if (link.dataset.processed) return;

            // 排除谷歌内部链接
            if (!link.href || link.href.includes("google.com/")) return;

            // 标记为已处理
            link.dataset.processed = "true";

            // 设置在新标签页打开
            link.setAttribute("target", "_blank");

            // 添加 rel 属性以提高安全性
            link.setAttribute("rel", "noopener noreferrer");
        });
    }

    // 初始执行一次
    processSearchResults();
})();