BetterYTM.min.user.js 2.0 KB

12
  1. // @license MIT
  2. const features=Object.freeze({arrowKeySupport:!0,switchBetweenSites:!0}),info=Object.freeze({name:GM.info.script.name,version:GM.info.script.version,namespace:GM.info.script.namespace});function init(){console.log(`${info.name} v${info.version} - ${info.namespace}`),document.addEventListener("DOMContentLoaded",onDomLoad)}function onDomLoad(){const e=getDomain();features.arrowKeySupport&&"ytm"===e&&document.addEventListener("keydown",onKeyDown),features.switchBetweenSites&&initSiteSwitch(e)}function onKeyDown(e){if(["ArrowLeft","ArrowRight"].includes(e.code))switch(e.code){case"ArrowLeft":document.body.dispatchEvent(new KeyboardEvent("keydown",{altKey:!1,bubbles:!0,cancelBubble:!1,cancelable:!0,charCode:0,code:"KeyH",composed:!0,ctrlKey:!1,currentTarget:null,defaultPrevented:e.defaultPrevented,explicitOriginalTarget:document.body,isTrusted:!0,key:"h",keyCode:72,metaKey:!1,originalTarget:document.body,repeat:!1,shiftKey:!1,srcElement:document.body,target:document.body,type:"keydown",view:window,which:72}));break;case"ArrowRight":document.body.dispatchEvent(new KeyboardEvent("keydown",{altKey:!1,bubbles:!0,cancelBubble:!1,cancelable:!0,charCode:0,code:"KeyL",composed:!0,ctrlKey:!1,currentTarget:null,defaultPrevented:e.defaultPrevented,explicitOriginalTarget:document.body,isTrusted:!0,key:"l",keyCode:76,metaKey:!1,originalTarget:document.body,repeat:!1,shiftKey:!1,srcElement:document.body,target:document.body,type:"keydown",view:window,which:76}));break;default:console.warn("Unknown key",e.code)}}function initSiteSwitch(e){const t=document.createElement("button");("yt"===e||"ytm"===e)&&t.on("click",switchSite(e))}function switchSite(e){let t;if("yt"===e?t="music":"ytm"===e&&(t="www"),!t)throw new TypeError(`Unrecognized domain '${e}'`);const{pathname:n,search:o}=new URL(location.href),c=`https://${t}.youtube.com${n}${o}`;console.info(`BetterYTM - switching to domain '${e}' at ${c}`),location.href=c}function getDomain(){return location.href.toLowerCase().includes("music.youtube")?"ytm":"yt"}init();