By default, clicking on a channel while browsing Youtube loads the "Home" tab. This loads the "Videos" tab instead.
当前为
// ==UserScript==
// @name Youtube channels open to "Videos" tab
// @description By default, clicking on a channel while browsing Youtube loads the "Home" tab. This loads the "Videos" tab instead.
// @version 0.3
// @namespace BawdyInkSlinger
// @author BawdyInkSlinger
// @match https://www.youtube.com/*
// @exclude https://www.youtube.com/embed*
// @run-at document-start
// @license MIT License
// @grant none
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// ==/UserScript==
// original version: https://greasyfork.org/en/discussions/requests/56798-request-make-videoes-the-default-tab-on-youtube-channels#comment-455176
(() => {
const RX_CHANNEL_HOME = /^(https?:\/\/www\.youtube\.com)((\/(user|channel|c)\/[^/]+)(\/?$|\/featured[^/])|(\/@(?!.*\/)[^/]+))/;
const DEFAULT_TAB_HREF = "/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,
// redirecting the link to /videos
location.href = RegExp.$2 + DEFAULT_TAB_HREF;
// BIS: I think there's a bug here but I've yet to prove it: If you navigate to another part of youtube, will
// the script work due to this early return?
return;
}
addEventListener('mousedown', event => {
const anchorTag = event.target.closest('a');
if (anchorTag && RX_CHANNEL_HOME.test(anchorTag.href)) {
const channelName = RegExp.$2;
// a channel link was clicked so it has to be rewritten before the actual navigation happens
// BIS: I don't fully understand this comment:
// this makes sure the previous redirect not needed as long as the link clicked is on a youtube page
// e.g. when opening a channel link in a new tab
anchorTag.href = channelName + DEFAULT_TAB_HREF;
setLinkAddressBar(channelName, anchorTag);
setLinkToLoadVideosTab(anchorTag);
}
}, true);
function setLinkAddressBar(channelName, anchorTag) {
try {
anchorTag.data.commandMetadata.webCommandMetadata.url = channelName + DEFAULT_TAB_HREF;
} catch (e) {
console.error(`Youtube channel default tab: setLinkAddressBar:`, e);
}
}
function setLinkToLoadVideosTab(anchorTag) {
try {
/*
BIS:
How to get "anchorTag.data.browseEndpoint.params" if this stops working:
1. Visit any channel's "Home" tab
2. Below the tabs, there should be a few sub sections, one of them being "Videos". It should be a link.
If this doesn't exist, find a different channel that matches this description.
3. Using your browser's devtools, inspect the "Videos" sub section link.
4. Right-click on the nearest "a" tag and select "Use in console" (Firefox). This creates a console variable named "temp0"
5. In the console, type "temp0.data.browseEndpoint.params". Hit enter.
6. This is the value that will bring you to the videos tab.
7. Copy and paste it below.
*/
anchorTag.data.browseEndpoint.params = `EgZ2aWRlb3MYAyAAcAHyBgsKCToCEASiAQIIAQ%3D%3D`;
} catch (e) {
console.error(`Youtube channel default tab: setLinkToLoadVideosTab:`, e);
}
}
})();