|
| 1 | +// ==UserScript== |
| 2 | +// @name Disable Embedded YouTube Videos |
| 3 | +// @icon https://external-content.duckduckgo.com/ip3/youtube.com.ico |
| 4 | +// @version 0.1 |
| 5 | +// @downloadURL https://userscripts.codonaft.com/disable-embedded-youtube-videos.user.js |
| 6 | +// ==/UserScript== |
| 7 | + |
| 8 | +(_ => { |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const PROXY = 'https://wsrv.nl/?url='; |
| 12 | +// const PROXY = 'https://imgproxy.nosotros.app/_/feed_img/plain/'; |
| 13 | + |
| 14 | +if (performance.getEntriesByType('navigation')[0]?.responseStatus !== 200) return; |
| 15 | + |
| 16 | +const imageURL = url => { |
| 17 | + if (!PROXY) return url; |
| 18 | + return `${PROXY}${encodeURIComponent(url)}`; |
| 19 | +}; |
| 20 | + |
| 21 | +const subscribeOnChanges = (node, selector, f) => { |
| 22 | + const apply = (node, observer) => { |
| 23 | + if (node?.nodeType !== 1) return; |
| 24 | + |
| 25 | + let observeChildren = true; |
| 26 | + if (node?.matches?.(selector)) { |
| 27 | + try { |
| 28 | + observeChildren = f(node, observer); |
| 29 | + } catch (e) { |
| 30 | + err(e, node); |
| 31 | + if (e.name === 'SecurityError') { |
| 32 | + observer.disconnect(); |
| 33 | + return; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (observeChildren) { |
| 39 | + const children = node?.childNodes || []; |
| 40 | + children.forEach(i => apply(i, observer)); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + const observer = new MutationObserver(mutations => mutations.forEach(m => m.addedNodes.forEach(i => apply(i, observer)))); |
| 45 | + observer.observe(node, { childList: true, subtree: true }); |
| 46 | + node.querySelectorAll(selector).forEach(i => apply(i, observer)); |
| 47 | +}; |
| 48 | + |
| 49 | +subscribeOnChanges(document.body, 'iframe[src^="https://www.youtube.com/"], iframe[src^="https://www.youtube-nocookie.com/embed/"]', node => { |
| 50 | + const src = node.getAttribute('src'); |
| 51 | + const videoId = src.split('?v=')[1] || src.split('/embed/')[1]; |
| 52 | + if (videoId) { |
| 53 | + const image = document.createElement('img'); |
| 54 | + image.src = imageURL(`https://i.ytimg.com/vi_webp/${videoId}/maxresdefault.webp`); |
| 55 | + image.style.width = '100%'; |
| 56 | + image.style.height = 'auto'; |
| 57 | + image.style.display = 'block'; |
| 58 | + |
| 59 | + const link = document.createElement('a'); |
| 60 | + link.href = `https://www.youtube.com/watch?v=${videoId}`; |
| 61 | + link.appendChild(image); |
| 62 | + |
| 63 | + node.parentNode.replaceChild(link, node); |
| 64 | + } |
| 65 | + |
| 66 | + return false; |
| 67 | +}); |
| 68 | + |
| 69 | +const err = (e, node) => { |
| 70 | + console.log(node); |
| 71 | + console.error(e); |
| 72 | +}; |
| 73 | +})(); |
0 commit comments