// ==UserScript== // @name YT video tab by default // @description Open "Video" tab of youtube channel by default // @author MK // @namespace max44 // @homepage https://greasyfork.org/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.3 // @license MIT // @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js // @run-at document-idle // @downloadURL none // ==/UserScript== (function() { 'use strict'; var urlAtLastCheck = ""; var divTabs = null; //Check URL changes 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, {childList: true, subtree: true}); } function openVideoTab() { var pathArray = window.location.pathname.split('/'); var firstPath = pathArray[1]; var lastPath = pathArray[pathArray.length - 1]; if (firstPath === "results" || firstPath === "" || firstPath === "watch" || firstPath === "playlist" || firstPath === "feed" || firstPath === "gaming" || firstPath === "live" || lastPath === "search" || 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 document.querySelectorAll("ytd-channel-video-player-renderer video").forEach(videoPause); //Pause any channel preview video 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 videoPause(input) { //Pause media input.pause(); } 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; } })();