Open YouTube videos in new tab with intelligent filtering
当前为
// ==UserScript==
// @name YouTube New Tab Opener
// @name:zh-CN YouTube新标签页打开器
// @namespace your_namespace
// @version 1.0.1
// @description Open YouTube videos in new tab with intelligent filtering
// @description:zh-cn YouTube视频新标签页打开
// @author YourName
// @license MIT
// @match *://*.youtube.com/*
// @icon https://www.youtube.com/favicon.ico
// @grant none
// @supportURL https://github.com/yourname/repo/issues
// ==/UserScript==
(function() {
'use strict';
// 更精确的视频链接匹配
function isVideoLink(href) {
return /(\/watch\?v=|\/shorts\/|^https:\/\/www\.youtube\.com\/embed\/)/.test(href);
}
// 优化排除规则
function shouldIgnoreLink(element) {
// 排除播放器标题、频道页、评论区
return element.closest('.ytp-title-link, #channel-header, ytd-comment-renderer');
}
function handleClick(event) {
let target = event.target;
let linkElement = null;
// 动态查找最近的视频链接(最多10层)
for (let i = 0; i < 10 && target; i++) {
if (target.tagName === 'A' && target.href && isVideoLink(target.href)) {
linkElement = target;
break;
}
target = target.parentElement;
}
if (linkElement && !shouldIgnoreLink(linkElement)) {
event.preventDefault();
event.stopPropagation();
window.open(linkElement.href, '_blank');
// Shorts页面特殊处理
if (linkElement.href.includes('/shorts/')) {
window.location.replace("about:blank");
}
}
}
// 使用事件捕获并动态绑定
document.addEventListener('click', handleClick, true);
})();