Youtube - Fix channel links in sidebar recommendations

Fixes the channel links for the "Up next" and recommendated videos below it on youtube.

נכון ליום 29-01-2021. ראה הגרסה האחרונה.

  1. // ==UserScript==
  2. // @name Youtube - Fix channel links in sidebar recommendations
  3. // @namespace 1N07
  4. // @version 0.5
  5. // @description Fixes the channel links for the "Up next" and recommendated videos below it on youtube.
  6. // @author 1N07
  7. // @match https://www.youtube.com/*
  8. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
  9. // @grant GM_registerMenuCommand
  10. // @grant GM_unregisterMenuCommand
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_openInTab
  14. // @grant GM_addStyle
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. var videoSectionOption;
  21. var videoSection = GM_getValue("videoSection", true);
  22. SetVidSecOption();
  23.  
  24. GM_addStyle(`
  25. ytd-compact-video-renderer .channel-link-blocker:hover ~ a #text.ytd-channel-name {
  26. text-decoration: underline;
  27. }
  28. .loading-channel-link, .loading-channel-link * {
  29. cursor: progress !important;
  30. }
  31.  
  32. .channel-link-blocker-parent
  33. {
  34. position: relative;
  35. }
  36.  
  37. .channel-link-blocker
  38. {
  39. display: inline-block;
  40. position: absolute;
  41. width: 100%;
  42. height: 25px;
  43. background-color: rgba(255, 25, 25, 0);
  44. top: 32px;
  45. left: 0;
  46. z-index: 2019;
  47. cursor: pointer;
  48. }
  49. `);
  50.  
  51. setInterval(AddListeners, 200); //fairly stupid way to do this, but hey, it works so this is how I'm doing it for now... -> switch to mutationobserver?
  52.  
  53. function AddListeners() {
  54. // My big brain high IQ plan for preventing the video from opening, since seems whatever I do some click event is caught by youtube:
  55. // Adding invisible divs on top of channel links so I can handle the clicks however I want. :DD
  56. $("ytd-compact-video-renderer .metadata.ytd-compact-video-renderer:not(.channel-link-blocker-parent) > a[href^='/watch'], ytd-compact-playlist-renderer .metadata.ytd-compact-playlist-renderer:not(.channel-link-blocker-parent) > a[href^='/watch']").each(function(){
  57. $(this).parent().addClass("channel-link-blocker-parent");
  58. $(this).parent().prepend(`<div class="channel-link-blocker" title="Open channel to current tab with LMB and to a new tab with MMB"></div>`);
  59. $(this).parent().find(".channel-link-blocker").prop("style", "top: " + $(this).parent().find("a[href^='/watch']:first > h3").height() + "px;");
  60. });
  61.  
  62. $(".channel-link-blocker").off(); //take out old listeners
  63. $(".channel-link-blocker").on("mousedown", function(e){ if(e.button==1) return false; }); //prevent MMB scrolling
  64. $(".channel-link-blocker").on("mouseup click", function(e){ //add new listeners
  65. e.preventDefault();
  66. e.stopPropagation();
  67. if(e && (e.which == 2 || e.which == 1)) {
  68. let elem = $(this);
  69. elem.addClass("loading-channel-link");
  70. let getUrl = $(this).next().prop("href") || "error"; //url of the video for the ajax
  71. if(getUrl != "error") {
  72. $.get(getUrl, function(data){
  73. let foundChannel = data.split('"/channel/')[1].split('"')[0] || "error"; //finding the channel code from the returned data
  74. if(foundChannel != "error") {
  75. if(e.which == 2)
  76. {
  77. GM_openInTab("https://www.youtube.com/channel/" + foundChannel + (videoSection ? "/videos" : ""));
  78. }
  79. else
  80. window.open("https://www.youtube.com/channel/" + foundChannel + (videoSection ? "/videos" : ""),"_self");
  81. }
  82. else {
  83. alert("parsing error");
  84. }
  85. }).fail(function(){
  86. alert("load error");
  87. }).always(function(){
  88. elem.removeClass("loading-channel-link");
  89. });
  90. }
  91. else {
  92. alert("getUrl error");
  93. }
  94. }
  95. });
  96. }
  97.  
  98. function SetVidSecOption() {
  99. GM_unregisterMenuCommand(videoSectionOption);
  100. videoSectionOption = GM_registerMenuCommand("Fix channel links- videos section (" + (videoSection ? "yes" : "no") + ") -click to change-", function(){
  101. videoSection = !videoSection;
  102. GM_setValue("videoSection", videoSection);
  103. SetVidSecOption();
  104. });
  105. }
  106. })();