Linux.do 新标签页打开

使linux.do网站的帖子链接在新标签页中打开

目前为 2025-03-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         Linux.do 新标签页打开
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  使linux.do网站的帖子链接在新标签页中打开
// @author       BIGFA
// @match        https://linux.do/*
// @grant        none
// @run-at       document-start
// @license      GPL-3.0
// @icon         data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0iY3VycmVudENvbG9yIj48cGF0aCBkPSJNMjAgM0g0QzMuNDQ3NzIgMyAzIDMuNDQ3NzIgMyA0VjIwQzMgMjAuNTUyMyAzLjQ0NzcyIDIxIDQgMjFIMjBDMjAuNTUyMyAyMSAyMSAyMC41NTIzIDIxIDIwVjRDMjEgMy40NDc3MiAyMC41NTIzIDMgMjAgM1pNNSAxOVY1SDE5VjE5SDVaTTE2IDhWMTZIMTRWMTEuNDE0Mkw4LjcwNzExIDE2LjcwNzFMNy4yOTI4OSAxNS4yOTI5TDEyLjU4NTggMTBIOFY4SDE2WiI+PC9wYXRoPjwvc3ZnPg==
// ==/UserScript==

(function() {
    'use strict';
    
    // 检查当前是否在主页
    function isHomePage() {
        // 主页的URL应该是 https://linux.do/ 或 https://linux.do
        return window.location.pathname === '/' || window.location.pathname === '';
    }
    
    // 主函数,用于修改链接
    function modifyLinks() {
        // 如果不是主页,则不执行任何操作
        if (!isHomePage()) {
            return;
        }
        
        // 选择所有帖子链接 - 更精确的选择器
        const postLinks = document.querySelectorAll('a[href^="/t/"], a[href^="/d/"], a.title[href], .topic-list-item a.title, .topic-list a.title, .latest-topic-list-item a.title');
        
        // 遍历所有链接并添加target="_blank"属性
        postLinks.forEach(link => {
            // 检查链接是否包含帖子URL模式
            if (link.href && (link.href.includes('/t/') || link.href.includes('/d/'))) {
                if (!link.hasAttribute('target') || link.getAttribute('target') !== '_blank') {
                    link.setAttribute('target', '_blank');
                    
                    // 添加rel="noopener"以提高安全性
                    if (!link.hasAttribute('rel') || !link.getAttribute('rel').includes('noopener')) {
                        const currentRel = link.getAttribute('rel') || '';
                        link.setAttribute('rel', currentRel ? currentRel + ' noopener' : 'noopener');
                    }
                    
                    // 防止点击事件被其他处理程序拦截
                    link.addEventListener('click', function(e) {
                        // 阻止默认行为和事件冒泡
                        e.stopPropagation();
                    }, true);
                }
            }
        });
    }
    
    // 页面加载完成后执行
    window.addEventListener('load', function() {
        modifyLinks();
        
        // 创建一个MutationObserver来监视DOM变化,处理动态加载的内容
        const observer = new MutationObserver(function(mutations) {
            modifyLinks();
        });
        
        // 开始观察document.body的所有子树变化
        observer.observe(document.body, { childList: true, subtree: true });
    });
    
    // 也在DOMContentLoaded时执行一次
    document.addEventListener('DOMContentLoaded', modifyLinks);
    
    // 定期执行以确保所有链接都被处理
    setInterval(modifyLinks, 2000);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址