🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/10 22:45:28
🔄 下次更新时间:2026/01/10 23:45:28
手动刷新缓存

YouTube Shorts Blaster

A userscript to automatically detect and remove YouTube page elements containing shorts outside of the "/shorts" page itself.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         YouTube Shorts Blaster
// @description  A userscript to automatically detect and remove YouTube page elements containing shorts outside of the "/shorts" page itself.
// @namespace    drez3000
// @author       drez3000
// @copyright    2024, drez3000 (https://github.com/drez3000)
// @license      MIT
// @match        *://*.youtube.com/*
// @exclude      *://accounts.youtube.com/*
// @grant        none
// @version      0.2.0
// ==/UserScript==

;(() => {
	'use strict'

	const MAX_DOM_LOOKUP_DEPTH = 22
	const MAX_CHECK_FREQUENCY_MS = 350

	function oncePageLoaded(callback) {
		return new Promise((res) => {
			const resolve = () => res(callback())
			if (document.readyState !== 'loading') {
				// Document is already ready, call the callback immediately
				resolve()
			} else {
				// Document is not ready yet, wait for the DOMContentLoaded event
				document.addEventListener('DOMContentLoaded', resolve)
			}
		})
	}

	function flatNodesOf(
		node,
		{ minDepth = 0, maxDepth = Number.POSITIVE_INFINITY, includeShadowRoot = true } = {},
	) {
		const nodes = []
		const stack = [{ node, depth: 0 }]
		while (stack.length > 0) {
			const { node: currentNode, depth } = stack.pop()

			if (depth >= minDepth && depth <= maxDepth) {
				nodes.push({ node: currentNode, depth })
			}

			// Add children to the stack with increased depth
			for (let i = currentNode.childNodes.length - 1; i >= 0; i--) {
				stack.push({ node: currentNode.childNodes[i], depth: depth + 1 })
			}
			if (includeShadowRoot && currentNode.shadowRoot) {
				stack.push({ node: currentNode.shadowRoot, depth: depth + 1 })
			}
		}
		return nodes.sort((a, b) => a.depth - b.depth).map((item) => item.node)
	}

	function isMain(node) {
		const tagName = node?.tagName || ''
		return tagName.match(/^(html|main|body|content|article)$/i)
	}

	function isShortsElement(node) {
		const tagName = node?.tagName || ''
		const headers = [...node.querySelectorAll('h1,h2,h3,h4,h5,h6')]
		return (
			tagName.match(/reel/i) ||
			(node?.attributes && node.attributes['is-shorts'] !== undefined) ||
			(headers.length === 1 && headers.at(0).innerText.match(/^shorts$/i))
		)
	}

	function selectYoutubeShortsThumbnails() {
		return [...document.querySelectorAll('#contents > ytd-video-renderer')].filter(
			(node) =>
				[...node.querySelectorAll('a')].filter((a) => a?.href?.match('/shorts')).length > 0,
		)
	}

	function selectYoutubeShortsSections() {
		return flatNodesOf(document, { maxDepth: MAX_DOM_LOOKUP_DEPTH }).filter(
			(node) =>
				typeof node?.querySelectorAll === 'function' &&
				node?.attributes !== undefined &&
				!isMain(node) &&
				isShortsElement(node),
		)
	}

	function removeYoutubeShorts() {
		return [...selectYoutubeShortsThumbnails(), ...selectYoutubeShortsSections()].map(
			(node) => {
				node?.remove && node.remove()
				return node
			},
		)
	}

	function check() {
		if (
			document.location.href.match(/youtube\..*\/shorts/i) ||
			document.location.href.match(/youtube\..*\/history/i) ||
			document.location.href.match(/youtube\..*\/playlist/i) ||
			document.location.href.match(/youtube\..*\/account/i)
		) {
			return
		}
		const removed = removeYoutubeShorts()
		if (removed.length) {
			console.info(`[YOUTUBE SHORTS BLASTER] Removed ${removed.length} elements:`, removed)
		}
	}

	function main() {
		oncePageLoaded(check).then(() => setInterval(check, MAX_CHECK_FREQUENCY_MS))
	}

	main()
})()