Generate Youtube Mix Link

Adds a link to the youtube mix playlist for the current video after the video title

  1. // ==UserScript==
  2. // @name Generate Youtube Mix Link
  3. // @namespace https://gf.qytechs.cn/en/users/1473478
  4. // @version 0.1
  5. // @description Adds a link to the youtube mix playlist for the current video after the video title
  6. // @author exrook
  7. // @match https://www.youtube.com/watch*
  8. // @grant none
  9. // @license GPL3
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. function getVideoId() {
  16. const urlParams = new URLSearchParams(window.location.search);
  17. return urlParams.get('v');
  18. }
  19.  
  20. function createMixLink(videoId) {
  21. const mixLink = document.createElement('a');
  22. mixLink.href = `https://www.youtube.com/watch?v=${videoId}&list=RD${videoId}&start_radio=1`;
  23. mixLink.textContent = 'YT Mix';
  24. mixLink.style.padding = '0px 12px';
  25. mixLink.style.borderRadius = '18px';
  26. mixLink.style.background = 'rgb(39, 39, 39)';
  27. mixLink.style.color = '#fff';
  28. mixLink.style.textDecoration = 'none';
  29. mixLink.style.fontWeight = '500';
  30. mixLink.style.fontSize = '14px';
  31. mixLink.style.fontFamily = 'Roboto, Arial, sans-serif';
  32. mixLink.style.cursor = 'pointer';
  33. mixLink.style.marginLeft = '10px';
  34. mixLink.style.display = 'inline-flex';
  35. mixLink.style.alignItems = 'center';
  36. mixLink.style.justifyContent = 'center';
  37. mixLink.id = 'yt-mix-link'; // Unique ID for updating link
  38. return mixLink;
  39. }
  40.  
  41. function addLink() {
  42. const videoId = getVideoId();
  43. if (videoId) {
  44. const mixLink = createMixLink(videoId);
  45. const titleElement = document.querySelector('#title h1') || document.querySelector('h1.title');
  46. if (titleElement) {
  47. const existingLink = titleElement.querySelector('#yt-mix-link');
  48. if (existingLink) {
  49. existingLink.replaceWith(mixLink);
  50. } else {
  51. titleElement.appendChild(mixLink);
  52. }
  53. }
  54. }
  55. }
  56.  
  57.  
  58. // Use MutationObserver to add the link after title element is created
  59. function registerTitleObserver() {
  60. const observer = new MutationObserver((mutations) => {
  61. const titleElement = document.querySelector('#title h1');
  62. console.log("Hello");
  63. if (titleElement) {
  64. observer.disconnect();
  65. addLink();
  66. }
  67. });
  68. observer.observe(document.body, { subtree: true, childList: true });
  69. }
  70.  
  71. let lastUrl = location.href;
  72. // Trigger addLink function on url changes
  73. function updateUrl() {
  74. const currentUrl = location.href;
  75. if (currentUrl !== lastUrl) {
  76. lastUrl = currentUrl;
  77. addLink();
  78. }
  79. }
  80.  
  81. function registerNavigationObserver() {
  82. updateUrl(); // Don't miss changes while we were disconnected
  83. const observer = new MutationObserver(() => {
  84. updateUrl();
  85.  
  86. // completely unnecessary optimization, sleep 2 seconds after each trigger
  87. // because it bothers me that this gets called multiple times for second
  88. observer.disconnect();
  89. setTimeout(registerNavigationObserver, 2000);
  90. });
  91. observer.observe(document.body, { childList: true, subtree: true });
  92. }
  93.  
  94. registerTitleObserver()
  95. registerNavigationObserver();
  96. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址