V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
sosme
V2EX  ›  分享创造

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

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

    ✅ 点击链接后,只在新标签打开,原页面不会跳转
    ✅ 支持 油管、知乎、百度等动态加载网站
    ✅ 阻止事件冒泡,绕过 油管 的 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  
       211 天前 via Android
    ctrl🤪
    sosme
        2
    sosme  
    OP
       211 天前
    @fgwmlhdkkkw 懒得每次都双手操作
    ageovb
        3
    ageovb  
       210 天前
    安装 SuperDrag 插件,拖动链接可以在新标签页打开
    washangshuang01
        4
    washangshuang01  
       210 天前
    用鼠标中键不是很方便吗
    g0blin
        5
    g0blin  
       210 天前
    鼠标滚轮键按下也可以
    aero99
        6
    aero99  
       209 天前
    试试先
    adsjcxeicxzs
        7
    adsjcxeicxzs  
       15 天前
    谢谢 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   ·   实用小工具   ·   2619 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:58 · PVG 19:58 · LAX 04:58 · JFK 07:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.