YT video tab by default

Open "Video" tab of youtube channel by default

目前為 2022-10-20 提交的版本,檢視 最新版本

// ==UserScript==
// @name          YT video tab by default
// @description   Open "Video" tab of youtube channel by default
// @author        MK
// @namespace     max44
// @homepage      https://gf.qytechs.cn/en/users/309172-max44
// @match         *://*.youtube.com/*
// @match         *://*.youtu.be/*
// @icon          https://cdn.icon-icons.com/icons2/1488/PNG/512/5295-youtube-i_102568.png
// @version       1.1
// @license       MIT
// @require       https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @run-at        document-idle
// ==/UserScript==

(function() {
  'use strict';

  var urlAtLastCheck = "";
  var divTabs = null;

  const config = {childList: true, subtree: true};

  const rootCallback = function (mutationsList, observer) {
    if (urlAtLastCheck != document.location.href) {
      urlAtLastCheck = document.location.href;
      openVideoTab();
    }
  }

  const rootNode = document.querySelector("body");
  if (rootNode != null) {
    const rootObserver = new MutationObserver(rootCallback);
    rootObserver.observe(rootNode, config);
  }


  function openVideoTab() {
    var pathArray = window.location.pathname.split('/');
    var firstPath = pathArray[1];
    var lastPath = pathArray[pathArray.length - 1];

    if (firstPath === "" || firstPath === "watch" || firstPath === "playlist" || firstPath === "feed" || firstPath === "gaming" || firstPath === "live" || lastPath === "history" || lastPath === "featured" || lastPath === "videos" || lastPath === "playlists" || lastPath === "community" || lastPath === "channels" || lastPath === "about") { //If not a channel or any channel's tab was selected
      //console.log("Not a channel or any tab is selected");

    } else { //If a channel
      let waitHeader = setInterval(function() { //Wait untile header loaded
        divTabs = $( "div#tabsContainer > div#tabsContent > tp-yt-paper-tab" ); //Get array of tabs as HTML elements

        if (divTabs != null && divTabs.length >= 2) {
          clearInterval(waitHeader); //Stop waiting for tabs

          //Count visible tabs
          var countVisible = 0;
          var i;
          var tabIndex = -1;
          for (i = 0; i < divTabs.length; i++) {
            if (isVisible(divTabs[i])) countVisible++;
            if (countVisible == 2) tabIndex = i; //Video tab is 2nd visible
          }

          if (countVisible > 4) { //Enough number of visible tabs - normal channel
            divTabs[tabIndex].click();
            //console.log("Video tab (" + tabIndex + ") is activated. Visible tabs: " + countVisible);
          }
        }
      }, 250); //Interval to wait for header with tabs loading
      if (divTabs != null) divTabs = null;
      //console.log(" ");
    }
  }

  function isVisible(pObj) { //Check all the parents of element to find whether it is visible or not
    if (pObj != null) {
      var checkNext = true;
      var vObj = pObj;

      while (checkNext) {
        checkNext = false;
        //console.log("checking element " + vObj.tagName + "#" + vObj.id + ": '" + document.defaultView.getComputedStyle(vObj,null)['display'] + "'");
        if (document.defaultView.getComputedStyle(vObj,null)['display'] != "none") {
          if (vObj.parentElement != null) {
            vObj = vObj.parentElement;
            checkNext = true;
          }
        } else {
          return false;
        }
      }
      return true;
    }
    return false;
  }
})();

QingJ © 2025

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