Youtube - Fix channel links in sidebar recommendations

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

目前为 2024-01-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         Youtube - Fix channel links in sidebar recommendations
// @namespace    1N07
// @version      0.6.2
// @description  Fixes the channel links for the "Up next" and recommended videos below it on youtube.
// @author       1N07
// @license      unlicense
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @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_addStyle
// @compatible   firefox Compatible with: Tampermonkey, Violentmonkey
// @compatible   firefox Not compatible with: Greasemonkey, FireMonkey
// @compatible   chrome Latest version untested, but likely works with at least Tampermonkey
// @compatible   opera Latest version untested, but likely works with at least Tampermonkey
// @compatible   edge Latest version untested, but likely works with at least Tampermonkey
// @compatible   safari Latest version untested, but likely works with at least Tampermonkey
// ==/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;
		}
		.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;
		}
	`);

    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(`<a class="channel-link-blocker" href="#"></a>`);

            //no idea what the longBylineText.runs array can contain other than the object we want, so throwing a find method in there so that if there are other random objects in the array, we hopefully get the right one (also tons of optional chaining so that if something goes wrong we gracefully get falsy as the final value)
            let channelHandle = $(this).closest("ytd-compact-video-renderer")?.[0]?.polymerController?.data?.longBylineText?.runs?.find(el => el.navigationEndpoint?.browseEndpoint?.canonicalBaseUrl?.startsWith("/@"))?.navigationEndpoint.browseEndpoint.canonicalBaseUrl;

            if(channelHandle?.length) {
                $(this).parent().find(".channel-link-blocker").prop("href", channelHandle + (videoSection ? "/videos" : ""));
            } else {
                console.log("Failed to get channel url");
                $(this).parent().find(".channel-link-blocker").on("click", (e) => {
                    e.preventDefault();
                    e.stopPropagation();
                    alert("'Youtube - Fix channel links in sidebar recommendations' failed to get the channel link for this video for some reason. If this happens consistently, please report it at greasyfork.");
                });
            }

            //blocker position adjustment
            $(this).parent().find(".channel-link-blocker").prop("style", "top: " + $(this).parent().find("a[href^='/watch']:first > h3").height() + "px;");
            //above adjustment appears to randomly fail. Attempted fix by delaying adjustment as perhaps the height hasn't been computed yet?
            //$(this) doesn't work from inside the seTimeout, so storing it here
            let storedThis = $(this);
            setTimeout(function() { storedThis.parent().find(".channel-link-blocker").prop("style", "top: " + storedThis.parent().find("a[href^='/watch']:first > h3").height() + "px;"); }, 1000);
        });
    }

    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();
        });
    }
})();

QingJ © 2025

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