Greasy Fork is available in English.

Discussions » Demandes de création de scripts

Request: Make "Videoes" the default tab on Youtube-channels

§
Posté le: 27/11/2018

Request: Make "Videoes" the default tab on Youtube-channels

When you visit a Youtube-channel the HOME tab is the default, but I would love a script which changed the default tab to VIDEOS:



Imgur

woxxomMod
§
Posté le: 27/11/2018
Édité le: 27/11/2018

This works for me in Tampermonkey:

// ==UserScript==
// @name          YT channel shows Videos
// @match         https://www.youtube.com/*
// @run-at        document-start
// @license       MIT License
// @grant         none
// ==/UserScript==

(() => {
  const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com\/(user|channel)\/[^/]+)(\/?$|\/featured)/;
  if (RX_CHANNEL_HOME.test(location.href)) {
    location.href = RegExp.$1 + '/videos';
    return;
  }
  addEventListener('mousedown', event => {
    const a = event.target.closest('a');
    if (a && RX_CHANNEL_HOME.test(a.href)) {
      a.href = RegExp.$1 + '/videos';
      try { a.data.commandMetadata.webCommandMetadata.url = a.href; } catch (e) {}
    }
  }, true);
})();
§
Posté le: 28/11/2018

Thank you!

It also works using Violentmonkey.

§
Posté le: 09/03/2019

Nice, still works !

§
Posté le: 28/11/2020
Édité le: 06/01/2021

This didn't work for me anymore so I just wanted to leave my updated version here for anyone else who might stumble upon this (as this thread was my top result on Google):

// ==UserScript==
// @name          Youtube channel default tab
// @match         https://www.youtube.com/*
// @run-at        document-start
// @license       MIT License
// @grant         none
// ==/UserScript==

(() => {
  const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)(\/(user|channel|c)\/[^/]+)(\/?$|\/featured)/;
  const DEFAULT_TAB_HREF = "/videos";
  // the byte/ascii sequence '0x12 0x06 v i d e o s' encoded with base64 and uri component encoding seems to correspond to the videos tab
  const DEFAULT_TAB_ENDPOINT_PARAMS = encodeURIComponent(btoa(String.fromCharCode(0x12, 0x06) + "videos"));

  if (RX_CHANNEL_HOME.test(location.href)) {
    // this will get invoked when a youtube channel link is reached from a non-youtube origin page
    // where we didn't rewrite the link
    location.href = RegExp.$2 + DEFAULT_TAB_HREF;
    return;
  }

  addEventListener('mousedown', event => {
    const a = event.target.closest('a');
    if (a && RX_CHANNEL_HOME.test(a.href)) {
      // a channel link was clicked so it has to be rewritten before the actual navigation happens

      // this makes sure the redirect above in line 15-20 is not needed as long as the link clicked is on a youtube page
      // e.g. when opening a channel link in a new tab
      a.href = RegExp.$2 + DEFAULT_TAB_HREF;

      // without this the url in the browsers navigation bar will show the wrong url but the videos tab is still being loaded
      try { a.data.commandMetadata.webCommandMetadata.url = RegExp.$2 + DEFAULT_TAB_HREF; } catch (e) {}

      // this makes sure that the videos tab is the one actually being loaded
      try { a.data.browseEndpoint.params = DEFAULT_TAB_ENDPOINT_PARAMS; } catch (e) {}
    }
  }, true);
})();

Basically the only relevant change is line 35 where a.data.browseEndpoint.params now has to be set as well. Not sure how those params are built so this might break in the future. I found the current value by navigating to a channels video tab and looking at document.getElementsByTagName("yt-navigation-manager")[0].currentEndpoint_.

Edit 2021-01-06: Added /c/channel_name URLs to the regex.

§
Posté le: 13/12/2020

This didn't work for me anymore so I just wanted to leave my updated version here for anyone else who might stumble upon this (as this thread was my top result on Google):

...

Basically the only relevant change is line 35 where a.data.browseEndpoint.params now has to be set as well. Not sure how those params are built so this might break in the future. I found the current value by navigating to a channels video tab and looking at document.getElementsByTagName("yt-navigation-manager")[0].currentEndpoint_.

Can't thank you enough Schuwi.

§
Posté le: 19/12/2020
Édité le: 19/12/2020

Thank you Schuwi!

Another thing to add is |c after |channel because I found some channel URL's have /c/channel_name.

Replace line 10 with: const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)(\/(user|channel|c)\/[^/]+)(\/?$|\/featured)/;

§
Posté le: 06/01/2021
Édité le: 06/01/2021

Thanks flowscript :)
I just noticed as well when I found a channel that had such a link.

I edited it into my original comment for anyone doing just a quick copy-and-paste.

Edit: Also: glad I could help :)

§
Posté le: 16/05/2021

I looked into this again because I thought it stopped working (turns out it what just a fluke, it worked again without changing anything).

Thought I'd add some more info for anyone looking into this in the future when it will inevitably break at some point:
Currently when clicking a channel link (or any video, playlist, etc for that matter) the browser will execute a POST request to https://www.youtube.com/youtubei/v1/browse?key=WHATEVER.
The response is a JSON document that describes the content of the page you are navigating to. When navigating to a channel page this JSON describes the multiple tabs (Home, Videos, Playlists, etc) including their browseEndpoints.

For example:

{
  (...),
  "contents": {
    "twoColumnBrowseResultsRenderer": {
      "tabs": [
        (...),
        {
          "tabRenderer": {
            "endpoint": {
              (...),
              "browseEndpoint": {
                "browseId": "UCTeLqJq1mXUX5WWoNXLmOIA",
                "params": "EgZ2aWRlb3M%3D",
                "canonicalBaseUrl": "/c/RobertsSpaceInd"
              }
            },
            "title": "Videos",
            (...)
          }
        },
        (...)
      ],
      (...)
    }
  },
  (...)
}
§
Posté le: 10/11/2021

This didn't work for me anymore so I just wanted to leave my updated version here for anyone else who might stumble upon this (as this thread was my top result on Google):

// ==UserScript==
// @name          Youtube channel default tab
// @match         https://www.youtube.com/*
// @run-at        document-start
// @license       MIT License
// @grant         none
// ==/UserScript==

(() => {
  const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)(\/(user|channel|c)\/[^/]+)(\/?$|\/featured)/;
  const DEFAULT_TAB_HREF = "/videos";
  // the byte/ascii sequence '0x12 0x06 v i d e o s' encoded with base64 and uri component encoding seems to correspond to the videos tab
  const DEFAULT_TAB_ENDPOINT_PARAMS = encodeURIComponent(btoa(String.fromCharCode(0x12, 0x06) + "videos"));

  if (RX_CHANNEL_HOME.test(location.href)) {
    // this will get invoked when a youtube channel link is reached from a non-youtube origin page
    // where we didn't rewrite the link
    location.href = RegExp.$2 + DEFAULT_TAB_HREF;
    return;
  }

  addEventListener('mousedown', event => {
    const a = event.target.closest('a');
    if (a && RX_CHANNEL_HOME.test(a.href)) {
      // a channel link was clicked so it has to be rewritten before the actual navigation happens

      // this makes sure the redirect above in line 15-20 is not needed as long as the link clicked is on a youtube page
      // e.g. when opening a channel link in a new tab
      a.href = RegExp.$2 + DEFAULT_TAB_HREF;

      // without this the url in the browsers navigation bar will show the wrong url but the videos tab is still being loaded
      try { a.data.commandMetadata.webCommandMetadata.url = RegExp.$2 + DEFAULT_TAB_HREF; } catch (e) {}

      // this makes sure that the videos tab is the one actually being loaded
      try { a.data.browseEndpoint.params = DEFAULT_TAB_ENDPOINT_PARAMS; } catch (e) {}
    }
  }, true);
})();

Basically the only relevant change is line 35 where a.data.browseEndpoint.params now has to be set as well. Not sure how those params are built so this might break in the future. I found the current value by navigating to a channels video tab and looking at document.getElementsByTagName("yt-navigation-manager")[0].currentEndpoint_.

Edit 2021-01-06: Added /c/channel_name URLs to the regex.

That's just what I need. Thanks, Schuwi!

§
Posté le: 12/03/2022

This didn't work for me anymore so I just wanted to leave my updated version here for anyone else who might stumble upon this (as this thread was my top result on Google):

// ==UserScript==
// @name          Youtube channel default tab
// @match         https://www.youtube.com/*
// @run-at        document-start
// @license       MIT License
// @grant         none
// ==/UserScript==

(() => {
  const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)(\/(user|channel|c)\/[^/]+)(\/?$|\/featured)/;
  const DEFAULT_TAB_HREF = "/videos";
  // the byte/ascii sequence '0x12 0x06 v i d e o s' encoded with base64 and uri component encoding seems to correspond to the videos tab
  const DEFAULT_TAB_ENDPOINT_PARAMS = encodeURIComponent(btoa(String.fromCharCode(0x12, 0x06) + "videos"));

  if (RX_CHANNEL_HOME.test(location.href)) {
    // this will get invoked when a youtube channel link is reached from a non-youtube origin page
    // where we didn't rewrite the link
    location.href = RegExp.$2 + DEFAULT_TAB_HREF;
    return;
  }

  addEventListener('mousedown', event => {
    const a = event.target.closest('a');
    if (a && RX_CHANNEL_HOME.test(a.href)) {
      // a channel link was clicked so it has to be rewritten before the actual navigation happens

      // this makes sure the redirect above in line 15-20 is not needed as long as the link clicked is on a youtube page
      // e.g. when opening a channel link in a new tab
      a.href = RegExp.$2 + DEFAULT_TAB_HREF;

      // without this the url in the browsers navigation bar will show the wrong url but the videos tab is still being loaded
      try { a.data.commandMetadata.webCommandMetadata.url = RegExp.$2 + DEFAULT_TAB_HREF; } catch (e) {}

      // this makes sure that the videos tab is the one actually being loaded
      try { a.data.browseEndpoint.params = DEFAULT_TAB_ENDPOINT_PARAMS; } catch (e) {}
    }
  }, true);
})();

Basically the only relevant change is line 35 where a.data.browseEndpoint.params now has to be set as well. Not sure how those params are built so this might break in the future. I found the current value by navigating to a channels video tab and looking at document.getElementsByTagName("yt-navigation-manager")[0].currentEndpoint_.

Edit 2021-01-06: Added /c/channel_name URLs to the regex.

@Schuwi the script works flawlessly, if you can/want publish it in the normal page so other people can use it and it could get updates as well.
The alternatives don't work anymore. This is the one!

§
Posté le: 17/11/2022

Looks like YouTube has change the way URLs are presented again with their Handle Update. Some users are still using the old format but others have adopted the handle (@username). The code will still work for the non handle using users. But to make it work for the new handles the RegEx needs to be updated. So here is my updated line 10:

const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)((\/(@\\?.*))|\/(user|channel|c)\/[^\/]+(\/?$|\/featured))/;

I don't claim to be a RegEx expert so there may be improvements that can be made to the above, but this is working without issue for me.

Looks like YouTube has change the way URLs are presented again with their Handle Update. Some users are still using the old format but others have adopted the handle (@username). The code will still work for the non handle using users. But to make it work for the new handles the RegEx needs to be updated. So here is my updated line 10:

const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)((\/(@\\?.*))|\/(user|channel|c)\/[^\/]+(\/?$|\/featured))/;

I don't claim to be a RegEx expert so there may be improvements that can be made to the above, but this is working without issue for me.

Here is another regexp that also seems to work

const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com\/(?:channel\/(.*?)\/featured|(?:.*\/)*(.+)))/;

This validation is broken though, haven't had time to look into it for a good solution yet


if (RX_CHANNEL_HOME.test(location.href)) {
    // this will get invoked when a youtube channel link is reached from a non-youtube origin page
    // where we didn't rewrite the link
    location.href = RegExp.$2 + DEFAULT_TAB_HREF;
    return;
}

Here is an ugly solution in the meantime :)


var element = document.querySelectorAll("tp-yt-paper-tab");

// Force "VIDEOS" tab when navigation is from an external page or reload
for (var i = 0; i < element.length; i++) {
    if (element[i].children[0] != null) {
        var buttonText = element[i].children[0].innerText;
        if (buttonText === "VIDEOS") {
            element[i].click();
        }
    }
}
§
Posté le: 21/11/2022

Looks like YouTube has change the way URLs are presented again with their Handle Update. Some users are still using the old format but others have adopted the handle (@username). The code will still work for the non handle using users. But to make it work for the new handles the RegEx needs to be updated. So here is my updated line 10:

const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)((\/(@\\?.*))|\/(user|channel|c)\/[^\/]+(\/?$|\/featured))/;

I don't claim to be a RegEx expert so there may be improvements that can be made to the above, but this is working without issue for me.

This has an interesting bug for me, if I go between channels back and forth, at some point it'll start stacking the /videos addition to the link. Still usually works though, even though currently I have a link going https://www.youtube.com/@CoolWorldsLab/videos/videos/videos/videos/videos/videos/videos/videos

And weirdly it doesn't do this for every channel's link immediately, but it's as if it remembers which ones had stacked ones and keeps stacking on those. The longer I click between channels, the more infectious it becomes, and the link structure propagates slowly to every channel I keep clicking enough times. After that a page reload will keep adding anywhere from 2 to 5 more /videos to the link.

I'm not sure if it's because of the regexp, or the new handle format breaks something else as well...pretty noob at regexp if that wasn't clear.

§
Posté le: 28/11/2022

use this code folks


// ==UserScript==
// @name Youtube channel default tab
// @namespace http://tampermonkey.net/
// @run-at document-start
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==

(() => {
// const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)(\/(user|channel|c)\/[^/]+)(\/?$|\/featured)/;
const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)((\/(@\\?.*))|\/(user|channel|c)\/[^\/]+(\/?$|\/featured))/;
const DEFAULT_TAB_HREF = "/videos";
// the byte/ascii sequence '0x12 0x06 v i d e o s' encoded with base64 and uri component encoding seems to correspond to the videos tab
const DEFAULT_TAB_ENDPOINT_PARAMS = encodeURIComponent(btoa(String.fromCharCode(0x12, 0x06) + "videos"));

if (RX_CHANNEL_HOME.test(location.href) && String(location.href).indexOf(DEFAULT_TAB_HREF) === -1) {
// this will get invoked when a youtube channel link is reached from a non-youtube origin page
// where we didn't rewrite the link
location.href = RegExp.$2 + DEFAULT_TAB_HREF;
return;
}

addEventListener('mousedown', event => {
const a = event.target.closest('a');

if (a && RX_CHANNEL_HOME.test(a.href)) {
// a channel link was clicked so it has to be rewritten before the actual navigation happens

// this makes sure the redirect above in line 15-20 is not needed as long as the link clicked is on a youtube page
// e.g. when opening a channel link in a new tab
a.href = RegExp.$2 + DEFAULT_TAB_HREF;

// without this the url in the browsers navigation bar will show the wrong url but the videos tab is still being loaded
try { a.data.commandMetadata.webCommandMetadata.url = RegExp.$2 + DEFAULT_TAB_HREF; } catch (e) {}

// this makes sure that the videos tab is the one actually being loaded
try { a.data.browseEndpoint.params = DEFAULT_TAB_ENDPOINT_PARAMS; } catch (e) {}
}
}, true);
})();

§
Posté le: 28/11/2022

Thank you tjford - works like a charm

§
Posté le: 28/11/2022

Thanks tjford

I like your solution to video string loop issue better than what I had (cleaner).

Forgot to post up this section of code. I added this to handle the /videos stacking issue. Just a simple conditional check. Seemed to fix it for me. But I would recommend users go with tjfords solution.

if (RX_CHANNEL_HOME.test(location.href)) {
// this will get invoked when a youtube channel link is reached from a non-youtube origin page
// where we didn't rewrite the link
//Added if because right click open in new tab was causing loop
if(location.href.indexOf("/video", 0) < 0) {
location.href = RegExp.$2 + DEFAULT_TAB_HREF;
return;
}
}

§
Posté le: 28/01/2023

Thanks tjford

I like your solution to video string loop issue better than what I had (cleaner).

Forgot to post up this section of code. I added this to handle the /videos stacking issue. Just a simple conditional check. Seemed to fix it for me. But I would recommend users go with tjfords solution.

if (RX_CHANNEL_HOME.test(location.href)) {
// this will get invoked when a youtube channel link is reached from a non-youtube origin page
// where we didn't rewrite the link
//Added if because right click open in new tab was causing loop
if(location.href.indexOf("/video", 0) < 0) {
location.href = RegExp.$2 + DEFAULT_TAB_HREF;
return;
}
}

Hi here, tjford's code works well in tampermonkey+chromium, but it seems not working in greasemonkey. Would you kindly have it checked in firefox + greasemonkey, thanks in advance.

§
Posté le: 07/05/2023

Hi here,
The script stopped working recently.
Seem like this is caused by the introduction of nicknames (channels without these nicknames work fine).
These are now present in the URL and differ from the ones used before.
Here is an example:
New: https://www.youtube.com/@LinusTechTips/videos
Previous: https://www.youtube.com/channel/UCfOrKQtC1tDfGf_fFVb8pYw/videos

I assume an update is needed for the following part
const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)(\/(user|channel|c)\/[^/]+)(\/?$|\/featured)/;


Can someone please fix this?

Thank you in advance

§
Posté le: 11/05/2023

@sylph, you can also try to use Violentmonkey. It's open source, just like Greasemonkey.
@AcidCrash, you can use my improved version of @Schuwi's script. But know that I am an absolute novice to scripting.

// ==UserScript==
// @name          Youtube channel default tab
// @match         https://www.youtube.com/*
// @exclude       https://www.youtube.com/embed*
// @run-at        document-start
// @license       MIT License
// @grant         none
// ==/UserScript==

(() => {
  const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)((\/(user|channel|c)\/[^/]+)(\/?$|\/featured[^/])|(\/@(?!.*\/)[^/]+))/;
  const DEFAULT_TAB_HREF = "/videos";
  // the byte/ascii sequence '0x12 0x06 v i d e o s' encoded with base64 and uri component encoding seems to correspond to the videos tab
  const DEFAULT_TAB_ENDPOINT_PARAMS = encodeURIComponent(btoa(String.fromCharCode(0x12, 0x06) + "videos"));

  if (RX_CHANNEL_HOME.test(location.href)) {
    // this will get invoked when a youtube channel link is reached from a non-youtube origin page
    // where we didn't rewrite the link
    location.href = RegExp.$2 + DEFAULT_TAB_HREF;
    return;
  }

  addEventListener('mousedown', event => {
    const a = event.target.closest('a');
    if (a && RX_CHANNEL_HOME.test(a.href)) {
      // a channel link was clicked so it has to be rewritten before the actual navigation happens

      // this makes sure the redirect above in line 15-20 is not needed as long as the link clicked is on a youtube page
      // e.g. when opening a channel link in a new tab
      a.href = RegExp.$2 + DEFAULT_TAB_HREF;

      // without this the url in the browsers navigation bar will show the wrong url but the videos tab is still being loaded
      try { a.data.commandMetadata.webCommandMetadata.url = RegExp.$2 + DEFAULT_TAB_HREF; } catch (e) {}

      // this makes sure that the videos tab is the one actually being loaded
      try { a.data.browseEndpoint.params = DEFAULT_TAB_ENDPOINT_PARAMS; } catch (e) {}
    }
  }, true);
})();
§
Posté le: 11/05/2023

Notes:
Some time ago I used @tjford's version from here. Unfortunately, the sorting of playlists on the channel and the new "/about" link are broken in this version. In addition, incorrect extra "/videos" is often added to the URL, although this is only a cosmetic problem. My version, through trial and error, is devoid of these shortcomings. (Plus, I also added an exception for embedded videos so that the script doesn't try to run in vain on other sites.)

§
Posté le: 11/05/2023
@AcidCrash, you can use my improved version of @Schuwi's script. But know that I am an absolute novice to scripting.

Thank you for the fix, seem to be working fine

§
Posté le: 13/05/2023

@Scriptchansky thanks, works in violentmonekey!

§
Posté le: 21/05/2023

Perhaps this will work, have not tested it:

// ==UserScript==
// @name         YouTube Videos Tab Auto-Loader
// @description  A script that automatically loads the "Videos" tab on a YouTube channel page as the default tab, after the webpage has loaded.
// @version      1.0
// @author       Midnight
// @match        https://www.youtube.com/*/channel/*
// @run-at       document-ready
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    "use strict";

    // Get the current document
    const doc = document;

    // Get the channel id from the URL
    const channelId = doc.location.pathname.split("/")[3];

    // Get the "Videos" tab
    const videosTab = doc.getElementById("videos-tab");

    // Set the "Videos" tab as the default tab
    videosTab.setAttribute("selected", true);

})();
§
Posté le: 19/12/2023

@sylph, you can also try to use Violentmonkey. It's open source, just like Greasemonkey.
@AcidCrash, you can use my improved version of @Schuwi's script. But know that I am an absolute novice to scripting.

// ==UserScript==
// @name          Youtube channel default tab
// @match         https://www.youtube.com/*
// @exclude       https://www.youtube.com/embed*
// @run-at        document-start
// @license       MIT License
// @grant         none
// ==/UserScript==

(() => {
  const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)((\/(user|channel|c)\/[^/]+)(\/?$|\/featured[^/])|(\/@(?!.*\/)[^/]+))/;
  const DEFAULT_TAB_HREF = "/videos";
  // the byte/ascii sequence '0x12 0x06 v i d e o s' encoded with base64 and uri component encoding seems to correspond to the videos tab
  const DEFAULT_TAB_ENDPOINT_PARAMS = encodeURIComponent(btoa(String.fromCharCode(0x12, 0x06) + "videos"));

  if (RX_CHANNEL_HOME.test(location.href)) {
    // this will get invoked when a youtube channel link is reached from a non-youtube origin page
    // where we didn't rewrite the link
    location.href = RegExp.$2 + DEFAULT_TAB_HREF;
    return;
  }

  addEventListener('mousedown', event => {
    const a = event.target.closest('a');
    if (a && RX_CHANNEL_HOME.test(a.href)) {
      // a channel link was clicked so it has to be rewritten before the actual navigation happens

      // this makes sure the redirect above in line 15-20 is not needed as long as the link clicked is on a youtube page
      // e.g. when opening a channel link in a new tab
      a.href = RegExp.$2 + DEFAULT_TAB_HREF;

      // without this the url in the browsers navigation bar will show the wrong url but the videos tab is still being loaded
      try { a.data.commandMetadata.webCommandMetadata.url = RegExp.$2 + DEFAULT_TAB_HREF; } catch (e) {}

      // this makes sure that the videos tab is the one actually being loaded
      try { a.data.browseEndpoint.params = DEFAULT_TAB_ENDPOINT_PARAMS; } catch (e) {}
    }
  }, true);
})();

Works great. Thanks.

Poster une réponse

Connectez-vous pour poster une réponse.