Advertisement
Guest User

invidious-links.user.js

a guest
Oct 27th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | Source Code | 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
  7. // @author anon
  8. // @description Append Invidious Links next to a#video-title links on YouTube
  9. // ==/UserScript==
  10.  
  11. function getv(url) {
  12. const u = new URL(url)
  13. if (u.pathname.match(/^\/shorts/)) {
  14. const parts = u.pathname.split('/')
  15. return parts[2]
  16. } else {
  17. return u.searchParams.get('v')
  18. }
  19. }
  20.  
  21. function invidiousUrl(url, site="yewtu.be") {
  22. const v = getv(url)
  23. return `https://${site}/watch?v=${v}`
  24. }
  25.  
  26. function createLink(url) {
  27. const a = document.createElement('a')
  28. a.href = url
  29. a.classList = ['invidious']
  30. a.innerHTML = ' ♂ '
  31. return a
  32. }
  33.  
  34. function appendLink(vid) {
  35. cl = [...vid.classList]
  36. if (!cl.includes('appended')) {
  37. const url = invidiousUrl(vid.href)
  38. const link = createLink(url)
  39. vid.after(link)
  40. vid.classList.add('appended')
  41. }
  42. }
  43.  
  44. const validNodeTypes = [1, 9, 10, 11]
  45. function processNode(node) {
  46. if (!validNodeTypes.includes(node.nodeType)) return
  47. try {
  48. vids = node.querySelectorAll('a#video-title-link, a#video-title')
  49. vids.forEach((vid) => {
  50. appendLink(vid)
  51. })
  52. }
  53. catch (e) {
  54. console.warn(e, node)
  55. // window.__node = node
  56. }
  57. }
  58.  
  59. const DELAY = 1000
  60.  
  61. const delayedProcessNode = (node) => {
  62. setTimeout(() => {
  63. processNode(node)
  64. }, DELAY)
  65. }
  66.  
  67. delayedProcessNode(document.body)
  68.  
  69. const observer = new MutationObserver((mutations) => {
  70. mutations.forEach((mutation) => {
  71. mutation.addedNodes.forEach(delayedProcessNode)
  72. })
  73. })
  74.  
  75. observer.observe(document.body, {
  76. childList: true,
  77. subtree: true,
  78. })
  79.  
Tags: userscript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement