[油猴脚本] 强制所有链接在新标签页打开 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
sosme
V2EX    分享创造

[油猴脚本] 强制所有链接在新标签页打开

  •  
  •   sosme 254 天前 1847 次点击
    这是一个创建于 254 天前的主题,其中的信息可能已经有所发展或是发生改变。
    强制所有链接在新标签页打开

    点击链接后,只在新标签打开,原页面不会跳转
    支持 油管、知乎、百度等动态加载网站
    阻止事件冒泡,绕过 油管 的 Javascript 处理
    轻量高效,不会影响网站其他功能

    // ==UserScript==
    // @name 强制所有链接在新标签页打开
    // @namespace http://tampermonkey.net/
    // @version 1.0
    // @description 让所有链接都在新标签页打开,同时原页面不会变化
    // @match *://*/*
    // @grant none
    // @license MIT
    // ==/UserScript==

    (function() {
    'use strict';

    function openInNewTab(event) {
    const link = event.target.closest('a'); // 找到点击的链接
    if (link && link.href && !link.hasAttribute('target')) {
    event.preventDefault(); // 阻止默认行为
    event.stopPropagation(); // 阻止事件冒泡,避免内部 JS 触发跳转
    setTimeout(() => {
    window.open(link.href, '_blank'); // 在新标签页打开链接
    }, 50); // 延迟执行,确保兼容
    }
    }

    function observeLinks() {
    document.addEventListener('click', openInNewTab, true);
    }

    observeLinks(); // 监听整个页面点击事件
    })();
    7 条回复    2025-08-21 15:25:17 +08:00
    fgwmlhdkkkw
        1
    fgwmlhdkkkw  
       254 天前 via Android
    ctrl
    sosme
        2
    sosme  
    OP
       254 天前
    @fgwmlhdkkkw 懒得每次都双手操作
    ageovb
        3
    ageovb  
       253 天前
    安装 SuperDrag 插件,拖动链接可以在新标签页打开
    washangshuang01
        4
    washangshuang01  
       253 天前
    用鼠标中键不是很方便吗
    g0blin
        5
    g0blin  
       253 天前
    鼠标滚轮键按下也可以
    aero99
        6
    aero99  
       252 天前
    试试先
    adsjcxeicxzs
        7
    adsjcxeicxzs  
       58 天前
    谢谢 op, 使用中发现一个问题, 一些 pop 不需要跳转, 比如 v2 的感谢按钮, 所以更新了一版

    // ==UserScript==
    // @name Force All Links to Open in New Tab
    // @namespace http://tampermonkey.net/
    // @version 1.0
    // @description Make all links open in a new tab, while keeping the original page unchanged
    // @match *://*/*
    // @grant none
    // @license MIT
    // ==/UserScript==

    (function () {
    'use strict';

    function isPopupOrInteractiveLink(link) {
    const url = link.href;

    // Check if link has any event handlers (interactive functionality)
    if (link.onclick || link.getAttribute('onclick') ||
    link.getAttribute('onmousedown') || link.getAttribute('onmouseup')) {
    return true;
    }

    // Check for data attributes that indicate Javascript functionality
    if (link.hasAttribute('data-toggle') || link.hasAttribute('data-target') ||
    link.hasAttribute('data-dismiss') || link.hasAttribute('data-action') ||
    link.hasAttribute('data-modal') || link.hasAttribute('data-popup')) {
    return true;
    }

    // Check for CSS classes that typically indicate interactive elements
    const interactiveClasses = ['modal-trigger', 'popup-trigger', 'lightbox-trigger',
    'dialog-trigger', 'overlay-trigger', 'accordion-trigger',
    'tab-trigger', 'dropdown-trigger', 'tooltip-trigger'];
    if (interactiveClasses.some(cls => link.classList.contains(cls))) {
    return true;
    }

    // Check for hash-only links that are used for Javascript interactions
    if (url.endsWith('#') || url.endsWith('#;') || url.includes('#!') ||
    url === window.location.href + '#' || url === '#') {
    return true;
    }

    // Check for Javascript: URLs
    if (url.startsWith('Javascript:')) {
    return true;
    }

    // Check for void(0) patterns
    if (/void\(0\)/i.test(url)) {
    return true;
    }

    // Check for same-page anchors (internal page navigation)
    if (url.includes('#') && url.split('#')[0] === window.location.href.split('#')[0]) {
    return true;
    }

    // Common popup URL patterns
    const popupPatterns = [
    /popup/i, /modal/i, /overlay/i, /lightbox/i, /dialog/i, /window\.open/i,
    /fancybox/i, /colorbox/i, /thickbox/i, /magnific/i, /photoswipe/i,
    /gallery/i, /slideshow/i, /carousel/i, /accordion/i, /tabs?/i,
    /dropdown/i, /tooltip/i, /share/i, /social/i
    ];

    return popupPatterns.some(pattern => pattern.test(url));
    }

    function openInNewTab(event) {
    const link = event.target.closest('a'); // Find the clicked link
    if (link && link.href && !link.hasAttribute('target')) {
    // Check if this is a popup or interactive link
    if (isPopupOrInteractiveLink(link)) {
    // Let popup/interactive links behave normally (don't interfere)
    return;
    }

    event.preventDefault(); // Block the default behavior
    event.stopPropagation(); // Prevent bubbling so internal JS won't trigger navigation
    setTimeout(() => {
    window.open(link.href, '_blank'); // Open link in a new tab
    }, 50); // Small delay for compatibility
    }
    }

    // Attach the click listener to the whole document
    document.addEventListener('click', openInNewTab, true);
    })();
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2529 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 10:54 PVG 18:54 LAX 03:54 JFK 06:54
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86