Advertisement
Guest User

invidious-links.user.js

a guest
Oct 27th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.19 KB | Software | 0 0
  1. // ==UserScript==
  2. // @name        Invidious Links
  3. // @namespace   Violentmonkey Scripts
  4. // @match       https://www.youtube.com/*
  5. // @grant       none
  6. // @version     1.0.1
  7. // @author      anon
  8. // @description Append Invidous Links next to a#video-title links on YouTube
  9. // ==/UserScript==
  10.  
  11. const SITE = "yewtu.be"
  12. const DELAY = 1000
  13.  
  14. /**
  15.  * Get the video_id of the url.
  16.  */
  17. function getv(url) {
  18.   const u = new URL(url)
  19.   if (u.pathname.match(/^\/shorts/)) {
  20.     const parts = u.pathname.split('/')
  21.     return parts[2]
  22.   } else {
  23.     return u.searchParams.get('v')
  24.   }
  25. }
  26.  
  27. function invidiousUrl(url, site) {
  28.   const v = getv(url)
  29.   return `https://${site}/watch?v=${v}`
  30. }
  31.  
  32. function createLink(url) {
  33.   const a = document.createElement('a')
  34.   a.href = url
  35.   a.classList = ['invidious']
  36.   a.innerHTML = ' ♂ '
  37.   return a
  38. }
  39.  
  40. function appendLink(vid) {
  41.   cl = [...vid.classList]
  42.   if (!cl.includes('appended')) {
  43.     const url = invidiousUrl(vid.href, SITE)
  44.     const link = createLink(url)
  45.     vid.after(link)
  46.     vid.classList.add('appended')
  47.   }
  48. }
  49.  
  50. const validNodeTypes = [1, 9, 10, 11]
  51. function processNode(node) {
  52.   if (!validNodeTypes.includes(node.nodeType)) return
  53.   try {
  54.     vids = node.querySelectorAll('a#video-title-link, a#video-title')
  55.     vids.forEach((vid) => {
  56.       appendLink(vid)
  57.     })
  58.   }
  59.   catch (e) {
  60.     console.warn(e, node)
  61.     // window.__node = node
  62.   }
  63. }
  64.  
  65. function detectAndRemovePopup(node) {
  66.   if (node.ELEMENT_NODE == 1 && node.tagName == 'TP-YT-PAPER-DIALOG') {
  67.     console.warn('POPUP DETECTED!')
  68.     setTimeout(() => { removePopup(node) }, DELAY)
  69.   }
  70. }
  71.  
  72. function removePopup(node) {
  73.   node.remove()
  74.   const vid = document.querySelector('video')
  75.   if (vid) {
  76.     vid.play()
  77.   } else {
  78.     console.warn('no video???')
  79.   }
  80. }
  81.  
  82. function delayedProcessNode(node) {
  83.   setTimeout(() => {
  84.     detectAndRemovePopup(node)
  85.     processNode(node)
  86.   }, DELAY)
  87. }
  88.  
  89. delayedProcessNode(document.body)
  90.  
  91. const observer = new MutationObserver((mutations) => {
  92.   mutations.forEach((mutation) => {
  93.     mutation.addedNodes.forEach(delayedProcessNode)
  94.   })
  95. })
  96.  
  97. observer.observe(document.body, {
  98.   childList: true,
  99.   subtree: true,
  100. })
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement