github-search-helper

在新标签页打开github搜索,explore和trending结果

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         github-search-helper
// @namespace    github.com/leychan
// @version      0.3
// @description  在新标签页打开github搜索,explore和trending结果
// @author       leychan
// @match        https://github.com/trending
// @match        https://github.com/explore
// @match        https://github.com/search*
// @icon         https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png
// @run-at document-end
// @grant        none
// @license       MIT
// ==/UserScript==

(function() {
    'use strict';

    // 修改所有 <a> 标签的 target 属性
    function setTargetBlank() {
        const allATags = document.getElementsByTagName('a');
        for (let i = 0; i < allATags.length; i++) {
            allATags[i].setAttribute('target', '_blank');
        }
    }

    // 初始化 MutationObserver
    const observer = new MutationObserver((mutationsList, observer) => {
        // 检查是否有新的节点被添加到 DOM 中
        mutationsList.forEach(mutation => {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                // 如果新增的节点中包含 <a> 标签,则修改其 target 属性
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === 1 && node.tagName === 'A') { // 检查是否是元素节点且是 <a> 标签
                        node.setAttribute('target', '_blank');
                    } else if (node.nodeType === 1 && node.querySelectorAll) { // 检查是否是元素节点且包含子节点
                        const aTags = node.querySelectorAll('a');
                        aTags.forEach(aTag => aTag.setAttribute('target', '_blank'));
                    }
                });
            }
        });
    });

    // 启动 MutationObserver
    observer.observe(document.body, { childList: true, subtree: true });

    // 初始修改页面中所有的 <a> 标签
    setTargetBlank();
})();