BetterYTM.min.user.js 5.4 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=a();console.info(`BetterYTM: Initializing features for domain '${n}'`);try{"ytm"===n&&(e.arrowKeySupport&&(document.addEventListener("keydown",o),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";const n="\n #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 }\n ",o=document.createElement("style");o.id="betterytm-watermark-style",o.styleSheet?o.styleSheet.cssText=n:o.appendChild(document.createTextNode(n));document.querySelector("head").appendChild(o);const r=document.querySelector("#left-content");r.parentNode.insertBefore(e,r.nextSibling),console.info("BetterYTM: Added watermark element:",e)}(),e.geniusLyrics&&s()),e.switchBetweenSites&&function(e){document.addEventListener("keydown",(t=>{"F9"==t.key&&function(e){console.info(`BetterYTM: Switching from domain '${a()}' 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:o,hash:r}=new URL(location.href),i=function(){const e=a();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}${o.includes("?")?`${o}&t=${i}`:`?t=${i}`}${r}`;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 o(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,o={};switch(e.code){case"ArrowLeft":o={code:"KeyH",key:"h",keyCode:72,which:72};break;case"ArrowRight":o={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,...o};document.body.dispatchEvent(new KeyboardEvent("keydown",n)),console.info(`BetterYTM: Dispatched proxy keydown event [${e.code}] -> [${n.code}]`)}}}let r=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 ${r} tries`)):r<10?(setTimeout(i,250),r++):console.info(`BetterYTM: Couldn't find upgrade tab to remove after ${r} tries`)}let c="";function s(){const e=document.querySelector(".middle-controls-buttons tp-yt-paper-icon-button.dropdown-trigger");if(!e)return setTimeout(s,250);const t=document.createElement("a");t.id="betterytm-genius-button",t.title="Search for lyrics on genius.com",t.href=d(),t.target="_blank",t.rel="noopener noreferrer";const n=document.createElement("img");n.src="https://raw.githubusercontent.com/Sv443/BetterYTM/develop/resources/external/genius.png",n.style="z-index: 10; width: 24px; height: 24px; padding: 8px; padding-left: 16px;",t.appendChild(n),console.info("BetterYTM: Inserted genius button:",t),e.parentNode.insertBefore(t,e.nextSibling),c=document.querySelector(".content-info-wrapper > yt-formatted-string").title,setInterval((()=>{let e=document.querySelector(".content-info-wrapper > yt-formatted-string").title;if(e!=c){console.info("BetterYTM: Detected song change, refreshing genius button"),c=e;document.querySelector("#betterytm-genius-button").href=d()}}),1e3)}function a(){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],o=`https://genius.com/search?q=${encodeURIComponent(n)}%20${encodeURIComponent(t)}`;return console.info(`BetterYTM: Resolved genius.com URL for song '${t}' by '${n}': ${o}`),o}catch(e){console.error("BetterYTM: Couldn't resolve genius.com URL:",e)}}!function(){try{console.log(`${t.name} v${t.version} - ${t.namespace}`),document.addEventListener("DOMContentLoaded",n)}catch(e){console.error("BetterYTM - General Error:",e)}}()})();