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

2025 年 2 月 6 日
 sosme
强制所有链接在新标签页打开

✅ 点击链接后,只在新标签打开,原页面不会跳转
✅ 支持 油管、知乎、百度等动态加载网站
✅ 阻止事件冒泡,绕过 油管 的 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(); // 监听整个页面点击事件
})();
3086 次点击
所在节点    分享创造
7 条回复
fgwmlhdkkkw
2025 年 2 月 6 日
ctrl🤪
sosme
2025 年 2 月 6 日
@fgwmlhdkkkw 懒得每次都双手操作
ageovb
2025 年 2 月 7 日
安装 SuperDrag 插件,拖动链接可以在新标签页打开
washangshuang01
2025 年 2 月 7 日
用鼠标中键不是很方便吗
g0blin
2025 年 2 月 7 日
鼠标滚轮键按下也可以
aero99
2025 年 2 月 8 日
试试先
adsjcxeicxzs
2025 年 8 月 21 日
谢谢 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);
})();

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.ih06.com/t/1109424

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX