您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Greasy Fork is available in English.
在新标签页打开视频链接,屏蔽主页Shorts,屏蔽精选, 功能在右下角悬浮球可手动开关
当前为
// ==UserScript== // @name YouTube 新标签页打开 // @namespace http://tampermonkey.net/ // @version 1.2 // @description 在新标签页打开视频链接,屏蔽主页Shorts,屏蔽精选, 功能在右下角悬浮球可手动开关 // @author YourName // @match *://www.youtube.com/* // @grant none // @license MIT // ==/UserScript== (function() { 'use strict'; // 配置开关变量,从 localStorage 初始化,默认均开启 var newTabEnabled = localStorage.getItem('yt_new_tab_enabled') !== null ? (localStorage.getItem('yt_new_tab_enabled') === 'true') : true; var shortsBlockingEnabled = localStorage.getItem('yt_shorts_blocking_enabled') !== null ? (localStorage.getItem('yt_shorts_blocking_enabled') === 'true') : true; // 添加精选屏蔽开关变量 var exploreBlockingEnabled = localStorage.getItem('yt_explore_blocking_enabled') !== null ? (localStorage.getItem('yt_explore_blocking_enabled') === 'true') : true; // 监听捕获所有点击事件 document.addEventListener('click', function(e) { // 如果不是鼠标左键点击,或者使用修饰键,则跳过 if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) { return; } // 获取触发事件的最近的 <a> 标签 var anchor = e.target.closest('a'); if (!anchor) { return; } try { var url = new URL(anchor.href, window.location.origin); // 判断是否为标准视频链接 (/watch?v=...) 且新标签页打开功能开启时 if (newTabEnabled && url.pathname === '/watch' && url.searchParams.has('v')) { e.preventDefault(); e.stopPropagation(); window.open(anchor.href, '_blank'); return; } // 判断是否为 Shorts 链接 (路径以 /shorts 开头) 且 Shorts 屏蔽功能开启时 if (shortsBlockingEnabled && url.pathname.startsWith('/shorts')) { e.preventDefault(); e.stopPropagation(); console.log("Shorts 被屏蔽。"); return; } } catch (error) { return; } }, true); // 创建贴边悬浮球及功能菜单 var controlContainer = document.createElement('div'); controlContainer.style.position = 'fixed'; controlContainer.style.bottom = '10px'; controlContainer.style.right = '10px'; controlContainer.style.zIndex = '9999'; controlContainer.style.display = 'flex'; controlContainer.style.flexDirection = 'column'; controlContainer.style.alignItems = 'center'; var menu = document.createElement('div'); menu.style.display = 'none'; menu.style.flexDirection = 'column'; menu.style.backgroundColor = 'white'; menu.style.border = '1px solid #ccc'; menu.style.borderRadius = '4px'; menu.style.padding = '10px'; menu.style.marginBottom = '5px'; menu.style.boxShadow = '0 2px 6px rgba(0,0,0,0.2)'; // 新标签页打开开关 var newTabDiv = document.createElement('div'); var newTabCheckbox = document.createElement('input'); newTabCheckbox.type = 'checkbox'; newTabCheckbox.checked = newTabEnabled; newTabCheckbox.id = 'newTabToggle'; var newTabLabel = document.createElement('label'); newTabLabel.htmlFor = 'newTabToggle'; newTabLabel.textContent = ' 新标签页打开'; newTabDiv.appendChild(newTabCheckbox); newTabDiv.appendChild(newTabLabel); // Shorts 屏蔽开关 var shortsDiv = document.createElement('div'); var shortsCheckbox = document.createElement('input'); shortsCheckbox.type = 'checkbox'; shortsCheckbox.checked = shortsBlockingEnabled; shortsCheckbox.id = 'shortsToggle'; var shortsLabel = document.createElement('label'); shortsLabel.htmlFor = 'shortsToggle'; shortsLabel.textContent = ' Shorts 屏蔽'; shortsDiv.appendChild(shortsCheckbox); shortsDiv.appendChild(shortsLabel); // 精选屏蔽开关 var exploreDiv = document.createElement('div'); var exploreCheckbox = document.createElement('input'); exploreCheckbox.type = 'checkbox'; exploreCheckbox.checked = exploreBlockingEnabled; exploreCheckbox.id = 'exploreToggle'; var exploreLabel = document.createElement('label'); exploreLabel.htmlFor = 'exploreToggle'; exploreLabel.textContent = ' 精选屏蔽'; exploreDiv.appendChild(exploreCheckbox); exploreDiv.appendChild(exploreLabel); menu.appendChild(newTabDiv); menu.appendChild(shortsDiv); menu.appendChild(exploreDiv); // 添加到菜单中 // 添加保存按钮 var saveButton = document.createElement('button'); saveButton.textContent = '保存'; // 设置按钮样式,可根据需要调整 saveButton.style.marginTop = '10px'; saveButton.style.padding = '5px 10px'; saveButton.style.border = 'none'; saveButton.style.backgroundColor = '#0073e6'; saveButton.style.color = '#fff'; saveButton.style.cursor = 'pointer'; saveButton.style.borderRadius = '4px'; menu.appendChild(saveButton); saveButton.addEventListener('click', function() { // 刷新页面使最新设置生效 location.reload(); }); newTabCheckbox.addEventListener('change', function() { newTabEnabled = newTabCheckbox.checked; localStorage.setItem('yt_new_tab_enabled', newTabEnabled); }); shortsCheckbox.addEventListener('change', function() { shortsBlockingEnabled = shortsCheckbox.checked; localStorage.setItem('yt_shorts_blocking_enabled', shortsBlockingEnabled); }); exploreCheckbox.addEventListener('change', function() { exploreBlockingEnabled = exploreCheckbox.checked; localStorage.setItem('yt_explore_blocking_enabled', exploreBlockingEnabled); }); // 悬浮球按钮 var ball = document.createElement('div'); ball.textContent = '⚙'; ball.style.fontSize = '24px'; ball.style.width = '40px'; ball.style.height = '40px'; ball.style.borderRadius = '50%'; ball.style.backgroundColor = '#0073e6'; ball.style.color = 'white'; ball.style.display = 'flex'; ball.style.justifyContent = 'center'; ball.style.alignItems = 'center'; ball.style.cursor = 'pointer'; ball.style.userSelect = 'none'; ball.addEventListener('click', function() { menu.style.display = (menu.style.display === 'none') ? 'flex' : 'none'; }); controlContainer.appendChild(menu); controlContainer.appendChild(ball); document.body.appendChild(controlContainer); // 修改移除功能部分 if (window.location.pathname === "/" && (shortsBlockingEnabled || exploreBlockingEnabled)) { function removeContent() { // 查找所有可能含 Shorts 的 shelf 容器 var shelves = document.querySelectorAll("ytd-rich-shelf-renderer, ytd-reel-shelf-renderer"); shelves.forEach(function(shelf) { // 尝试从标题中判断是否为 Shorts 部分 var titleElement = shelf.querySelector('h2'); if (titleElement) { var titleText = titleElement.textContent.trim().toLowerCase(); // 处理 Shorts if (shortsBlockingEnabled && titleText.includes('shorts')) { shelf.remove(); return; } // 处理精选 if (exploreBlockingEnabled && (titleText.includes('精选') || titleText.includes('explore'))) { shelf.remove(); return; } } // 备用检测:若 shelf 内包含链接地址以 "/shorts" 开头,则也移除 if (shortsBlockingEnabled && shelf.querySelector("a[href^='/shorts']")) { shelf.remove(); } }); } // 初次尝试删除内容 removeContent(); // 使用 MutationObserver 监控 DOM 变化,动态删除新增的内容 var observer = new MutationObserver(function(mutations) { removeContent(); }); observer.observe(document.body, { childList: true, subtree: true }); } })();