Youtube - Fix channel links in sidebar recommendations

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

目前為 2023-11-30 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Youtube - Fix channel links in sidebar recommendations
// @namespace    1N07
// @version      0.5.3
// @description  Fixes the channel links for the "Up next" and recommended videos below it on youtube.
// @author       1N07
// @match        https://www.youtube.com/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_openInTab
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    var videoSectionOption;
    var videoSection = GM_getValue("videoSection", true);
    SetVidSecOption();

    GM_addStyle(`
		ytd-compact-video-renderer .channel-link-blocker:hover ~ a #text.ytd-channel-name {
			text-decoration: underline;
		}
		.loading-channel-link, .loading-channel-link * {
			cursor: progress !important;
		}

		.channel-link-blocker-parent
		{
			position: relative;
		}

		.channel-link-blocker
		{
			display: inline-block;
			position: absolute;
			width: 100%;
			height: 25px;
			background-color: rgba(255, 25, 25, 0);
			top: 32px;
			left: 0;
			z-index: 2019;
			cursor: pointer;
		}
	`);

    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?

    function AddListeners() {
        // My big brain high IQ plan for preventing the video from opening, since seems whatever I do some click event is caught by youtube:
        // Adding invisible divs on top of channel links so I can handle the clicks however I want. :DD
        $(`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(){
            $(this).parent().addClass("channel-link-blocker-parent");
            $(this).parent().prepend(`<div class="channel-link-blocker" title="Open channel to current tab with LMB and to a new tab with MMB"></div>`);

            //blind attempt at a fix for sometimes getting the wrong positioning. Delay checking the value, as perhaps the height hasn't been computed yet?
            let storedThis = $(this);
            setTimeout(function() { storedThis.parent().find(".channel-link-blocker").prop("style", "top: " + storedThis.parent().find("a[href^='/watch']:first > h3").height() + "px;"); }, 250);
        });

        $(".channel-link-blocker").off(); //take out old listeners
        $(".channel-link-blocker").on("mousedown", function(e){ if(e.button==1) return false; }); //prevent MMB scrolling
        $(".channel-link-blocker").contextmenu(function() { return false; }); //prevent context menu
        $(".channel-link-blocker").on("mouseup click", function(e){ //add new listeners
            e.preventDefault();
            e.stopPropagation();
            if(e && (e.which > 0)) {
                let elem = $(this);
                elem.addClass("loading-channel-link");
                let getUrl = $(this).next().prop("href") || "error"; //url of the video for the ajax
                if(getUrl != "error") {
                    $.get(getUrl, function(data){
                        let foundChannel = data.split(/\/channel\/(?=.{24}\/videos)/)[1].split('/')[0] || "error"; //finding the channel code from the returned data
                        if(foundChannel != "error") {
                            if(e.which > 1)
                                GM_openInTab("https://www.youtube.com/channel/" + foundChannel + (videoSection ? "/videos" : ""));
                            else
                                window.open("https://www.youtube.com/channel/" + foundChannel + (videoSection ? "/videos" : ""),"_self");
                        }
                        else {
                            alert("parsing error");
                        }
                    }).fail(function(){
                        alert("load error");
                    }).always(function(){
                        elem.removeClass("loading-channel-link");
                    });
                }
                else {
                    alert("getUrl error");
                }
            }
        });
    }

    function SetVidSecOption() {
        GM_unregisterMenuCommand(videoSectionOption);
        videoSectionOption = GM_registerMenuCommand("Fix channel links- videos section (" + (videoSection ? "yes" : "no") + ") -click to change-", function(){
            videoSection = !videoSection;
            GM_setValue("videoSection", videoSection);
            SetVidSecOption();
        });
    }
})();