BetterYTM.min.user.js 6.1 KB

12
  1. // @license MIT
  2. (()=>{const e=Object.freeze({arrowKeySupport:!0,removeUpgradeTab:!0,switchBetweenSites:!0,geniusLyrics:!0,removeWatermark:!1}),t=Object.freeze({name:GM.info.script.name,version:GM.info.script.version,namespace:GM.info.script.namespace});function n(){const n=l();console.info(`BetterYTM: Initializing features for domain '${n}'`);try{"ytm"===n&&(e.arrowKeySupport&&(document.addEventListener("keydown",r),console.info("BetterYTM: Added key press listener")),e.removeUpgradeTab&&i(),e.removeWatermark||function(){const e=document.createElement("a");e.id="betterytm-watermark",e.className="style-scope ytmusic-nav-bar",e.innerText=t.name,e.title=`${t.name} v${t.version}`,e.href=t.namespace,e.target="_blank",e.rel="noopener noreferrer";m(" #betterytm-watermark {\n position: absolute;\n left: 45px;\n top: 43px;\n z-index: 10;\n color: white;\n text-decoration: none;\n cursor: pointer;\n }\n\n #betterytm-watermark:hover {\n text-decoration: underline;\n }","watermark");u(document.querySelector("#left-content"),e),console.info("BetterYTM: Added watermark element:",e)}(),e.geniusLyrics&&a()),e.switchBetweenSites&&function(e){document.addEventListener("keydown",(t=>{"F9"==t.key&&function(e){console.info(`BetterYTM: Switching from domain '${l()}' to '${e}'`);try{let t;if("ytm"===e?t="music":"yt"===e&&(t="www"),!t)throw new TypeError(`Unrecognized domain '${e}'`);const{pathname:n,search:r,hash:o}=new URL(location.href),i=function(){const e=l();try{if("ytm"===e){return document.querySelector("#progress-bar").value??null}return"yt"===e?0:null}catch(e){return console.error("BetterYTM: Couldn't get video time due to error:",e),null}}()??0;console.info(`BetterYTM: Found video time of ${i} seconds`);const c=`https://${t}.youtube.com${n}${r.includes("?")?`${r}&t=${i}`:`?t=${i}`}${o}`;console.info(`BetterYTM - switching to domain '${e}' at ${c}`),location.href=c}catch(e){console.error("BetterYTM: Error while switching site:",e)}}("yt"===e?"ytm":"yt")})),console.info("BetterYTM: Initialized site switch listener")}(n)}catch(e){console.error("BetterYTM: General error while executing feature:",e)}}function r(e){if(["ArrowLeft","ArrowRight"].includes(e.code)){console.info(`BetterYTM: Captured key '${e.code}' in proxy listener`);const t={altKey:!1,bubbles:!0,cancelBubble:!1,cancelable:!0,charCode:0,composed:!0,ctrlKey:!1,currentTarget:null,defaultPrevented:e.defaultPrevented,explicitOriginalTarget:document.body,isTrusted:!0,metaKey:!1,originalTarget:document.body,repeat:!1,shiftKey:!1,srcElement:document.body,target:document.body,type:"keydown",view:window};let n=!1,r={};switch(e.code){case"ArrowLeft":r={code:"KeyH",key:"h",keyCode:72,which:72};break;case"ArrowRight":r={code:"KeyL",key:"l",keyCode:76,which:76};break;default:n=!0}if(n)console.warn(`BetterYTM: Captured key '${e.code}' has no defined behavior`);else{const n={...t,...r};document.body.dispatchEvent(new KeyboardEvent("keydown",n)),console.info(`BetterYTM: Dispatched proxy keydown event: [${e.code}] -> [${n.code}]`)}}}let o=0;function i(){const e=document.querySelector('.ytmusic-nav-bar ytmusic-pivot-bar-item-renderer[tab-id="SPunlimited"]');e?(e.remove(),console.info(`BetterYTM: Removed upgrade tab after ${o} tries`)):o<20?(setTimeout(i,250),o++):console.error(`BetterYTM: Couldn't find upgrade tab to remove after ${o} tries`)}let c="",s=0;function a(){const e=document.querySelector(".middle-controls-buttons tp-yt-paper-icon-button.dropdown-trigger");if(!e)return s++,s<20?setTimeout(a,250):console.error(`BetterYTM: Couldn't find media control menu button to append lyrics button to, after ${s} tries`);const t=document.querySelector(".content-info-wrapper > yt-formatted-string"),n=document.createElement("a");n.id="betterytm-lyrics-button",n.title="Search for lyrics on genius.com",n.href=d(),n.target="_blank",n.rel="noopener noreferrer";m(" #betterytm-lyrics-button {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n position: relative;\n vertical-align: middle;\n\n margin-left: 8px;\n width: 40px;\n height: 40px;\n border-radius: 100%;\n background-color: transparent;\n }\n\n #betterytm-lyrics-button:hover {\n background-color: #383838;\n }\n\n #betterytm-lyrics-img {\n display: inline-block;\n z-index: 10;\n width: 24px;\n height: 24px;\n padding: 5px;\n }","lyrics");const r=document.createElement("img");r.id="betterytm-lyrics-img",r.src="https://raw.githubusercontent.com/Sv443/BetterYTM/develop/resources/external/genius.png",n.appendChild(r),console.info(`BetterYTM: Inserted genius button after ${s} tries:`,n),u(e,n),c=t.title;new MutationObserver((e=>{e.forEach((e=>{const t=e.target.title;if(t!=c){console.info(`BetterYTM: Song title changed from '${c}' to '${t}'`),c=t;document.querySelector("#betterytm-lyrics-button").href=d()}}))})).observe(t,{attributes:!0,attributeFilter:["title"]})}function l(){const{hostname:e}=new URL(location.href);return e.toLowerCase().includes("music")?"ytm":"yt"}function d(){try{const e=e=>{let t;return e.match(/\(|feat|ft/gim)&&(t=e.substring(0,e.indexOf("("))),(t||e).trim()},t=e(document.querySelector(".content-info-wrapper > yt-formatted-string").title),n=document.querySelector("span.subtitle > yt-formatted-string:first-child").title.split(/\s*\u2022\s*/gimu)[0],r=`https://genius.com/search?q=${encodeURIComponent(t)}%20${encodeURIComponent(n)}`;return console.info(`BetterYTM: Resolved genius.com URL for song '${t}' by '${n}': ${r}`),r}catch(e){console.error("BetterYTM: Couldn't resolve genius.com URL:",e)}}function u(e,t){return e.parentNode.insertBefore(t,e.nextSibling),t}function m(e,t){const n=document.createElement("style");n.id=`betterytm-${t}-style`,n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e)),document.querySelector("head").appendChild(n),console.info(`BetterYTM: Inserted global style with ref '${t}':`,n)}!function(){try{console.log(`${t.name} v${t.version} - ${t.namespace}`),document.addEventListener("DOMContentLoaded",n)}catch(e){console.error("BetterYTM - General Error:",e)}}()})();