智能新标签页打开

自动在新标签页打开链接,但保持导航类链接在当前页面跳转

// ==UserScript==
// @name         智能新标签页打开
// @name:en      Smart Tab Opener
// @name:zh-CN   智能新标签页打开
// @version      1.17
// @description  自动在新标签页打开链接,但保持导航类链接在当前页面跳转
// @description:en  Automatically open links in new tabs, but keep navigation class links on the current page
// @description:zh-CN  自动在新标签页打开链接,但保持导航类链接在当前页面跳转
// @author       dal
// @match        *://*/*
// @grant        none
// @license      MIT
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAd/SURBVGhDzZhbbBRVGMfnTLsI2wYFBPFFSqIICglBCIgGgqSh7S5KuzO7cusUMERjDIQEH3ygafDFROMDiSbEcA0gXUhAKSoXKwZaeuFBqWggsFsIBCiWiIVedneO3zczZ5g9O2d2e0E48O3/7Dnn/32/M7M7O1MJGqmurpadCsHU7nNrJN7zuLyZA7kneVK8Q5Lk8XntTu5J+PF+eRc3KC+WNYUPB5rDNwMt4TPBpshCHLfC08srhDFvG519QZKMZBBMs3rVerUQoGPB5ggNtpgBG+krO6usgWlPLzfurJs7gEeSnLxlLZF3nPCOTeiBpnCNqqp5Iq9jnK+bEwA/zidxW5PhBfgID28HbqxZ3Tl/uzbczcsUginrZwXIdgTS5ry8S5qWjAk2hztd4UHhTFD4iJ0IHFk2iveiQjB19v8feFQIeXFL+A0A7XCDD7ayTUR+LztZMYH3WsrXFQLw415J+uVddFp5GY70JTd4exPN4evBU+EZvJf17bxcIc+jZ2lGkoF4y8+sGAdHusENnp0Z2OS90sZQKe/l6w4IgFszIG/xTysKAPKgG7yh5tlJBJvC7/FeVk9YmCkE0wyAofDOr56fD6BfusGzwLlAc2Sz6DIr3AQEUyHAUHkDrZF1AJnk4e0wP1K7ucusmaympka3lIISVJyDQMVm9Nkc6gX1gj9FfGN8fh9J9Mmyb5iuo8o0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d4ZvCHYtbw3PYeivSvJZmHizsAOg5Ht7YQFzrUxqUEYIkmcmyAODHEaBPckc+HmxdOpmttyLDCyqqK5GKmLafh2dRcWnlIudiQZKs8KCGZ369NjzYpGwA+H2Bs+rm4jPl49h6K4Rehz6six34Dqx3gzfPirZXZLaT5A7wKLzGGZgMoC7wxscoWR6vmiFKBsE0G8Cj8kIHLsGh9qpGF3hqXV4vqJfVpz2SDAZgsF6zU3FZCwngmTYsvb72WUGStAKiwo/Ia3ZwQShWeVoAb50Z7VooVlXh+HGzE3KFcj16aXMD8bKO8fN8vio+CQbOkTziF91aYKNU/4sQOaonpRa4LfibJuFhKx8etiyVepOp3kL/+de2Pt+T7daCzVnar3sqnMMO7tBO0rY6psBt4D6Al93gM5o1hzdgafc2KXqXykSbtm1CnReAAzIDmilbj0ZoaV77rxKW6lO3TTwA9T+AR7vUgOFNHQXnZO+5xRdHm6tygkd1jnvCozp3bCd5ZVvRNwClQNwzLG7NC97aOGih1Jd63VqNLQPAWddSe56tRyM0Vy87A06zodN2FH1HelLT4WGhzvQ7Wg7wRjloOpHx7zfYXAH4umyerUcjNKHXmGUdSzO+9RVXKufBFegQ3DP1elxm035DrDXHrVxpebl6OV1tRN5+JVHalNEVcS0El9vPALpWiVWeAMhfQjEIUICvN97HtWOwblOwda2febn8QwLPlF/QnySP3Zs5kHuSJ8U7JEkek1ci/wEgX/qb/2WYeQAAAABJRU5ErkJggg==
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';
    
    const NAV_RELATED_ELEMENTS = [
        'nav',
        'menu',
        'dropdown',
        'header',
        'select',
        'navigat',
        'section',
        'button',
    ];

    const DO_NOTHING_ELEMENTS = [
        'input',
        'button'
    ];
    
    // 导航相关的类名关键字
    const NAV_RELATED_CLASS_KEYWORDS = [
        'nav',
        'menu',
        'dropdown',
        'header',
        'select',
        'navigat',
        'breadcrumb',
        'section',
        'chapter',
        'tree',
        'n-tab-links',
        'channel',
        'side-bar',
        'sidebar',
        'wrapper',
        'outline',
        'folder'
    ];

    document.addEventListener('click', function(e) {
        console.log('点击事件触发:', e.target);
        
        // 查找被点击元素的最近的 A 标签父元素
        let target = e.target;
        if (DO_NOTHING_ELEMENTS.includes(target.tagName.toLowerCase())){
            console.log("什么也不做!");
            return;
        }
        while (target && target.tagName !== 'A') {
            target = target.parentElement;
        }
        
        // 如果找到了链接元素
        if (target && target.tagName === 'A') {
            let href = target.href;
            if (!href || href === '' || href.indexOf("#") != -1 ||
                href.startsWith('javascript')) {
                console.log('锚点跳转', target.href);
                return;
            }
            e.preventDefault();
            e.stopImmediatePropagation();
            console.log('找到链接元素:', target);
            console.log('链接href:', target.href);
            
            let isNavLink = false;
            
            // 检查父元素
            let parent = target;
            for (let depth = 0; depth < 5; depth++) {
                if (!parent) break;
                
                console.log(`检查第 ${depth + 1} 层父元素:`, parent.tagName, 
                    parent.classList ? Array.from(parent.classList) : '无类名',
                );
                
                // 检查标签名
                if (NAV_RELATED_ELEMENTS.includes(parent.tagName.toLowerCase())) {
                    console.log(`与导航相关的标签名:`, parent.tagName);
                    isNavLink = true;
                    break;
                }
                
                // 检查类名
                if (parent.classList && parent.classList.length > 0) {
                    if (NAV_RELATED_CLASS_KEYWORDS.some(navClass => 
                        Array.from(parent.classList).some(cls => 
                            cls.toLowerCase().includes(navClass.toLowerCase())
                        )
                    )) {
                        console.log(`与导航相关的类名:`, 
                            parent.classList ? Array.from(parent.classList) : '无类名',
                        );
                        isNavLink = true;
                        break;
                    }
                }
                
                // 检查链接本身的 ID
                if (parent.id) {
                    if (NAV_RELATED_CLASS_KEYWORDS.some(keyword => 
                        parent.id.toLowerCase().includes(keyword.toLowerCase())
                    )) {
                        console.log('与导航相关的的ID:', parent.id);
                        isNavLink = true;
                        break;
                    }
                }
                
                parent = parent.parentElement;
            }
            
            const url = target.href;
            if (url && !url.startsWith('javascript:')) {
                // 如果是导航链接
                if (isNavLink) {
                    console.log('这是一个导航链接');
                    // 检查是否设置为新标签页打开
                    if (target.target === '_blank') {
                        console.log('原本是新标签页打开,改为当前页面打开');
                        window.location.href = url;
                    } else {
                        window.location.href = url;
                        console.log('保持默认打开方式');
                    }
                } else {
                    // 非导航链接,在新标签页打开
                    console.log('非导航链接,在新标签页打开');
                    window.open(url, '_blank');
                }
            }
        }
    }, true);
})();