mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
6.0 KiB
JavaScript
1 line
6.0 KiB
JavaScript
(()=>(e,t)=>{function i(e,t,i){if(t in e){Object.defineProperty(e,t,{value:i,enumerable:true,configurable:true,writable:true})}else{e[t]=i}return e}class a{static get extendedSupportedRates(){return Array.from(new Set(e.extendVideoSpeedList)).sort(((e,t)=>e-t))}static get supportedRates(){if(e.extendVideoSpeed){return[...a.nativeSupportedRates,...a.extendedSupportedRates].sort(((e,t)=>e-t))}return a.nativeSupportedRates}static get fallbackVideoSpeed(){if(e.useDefaultVideoSpeed){return parseFloat(e.defaultVideoSpeed)}}static formatSpeedText(e){if(e===1){return"倍数"}return Math.trunc(e)===e?`${e}.0x`:`${e}x`}static getRememberSpeed(t){for(const[i,a]of Object.entries(e.rememberVideoSpeedList)){if(a.some((e=>e===getAid(t)))){return parseFloat(i)}}}static forgetSpeed(t){t=getAid(t);let i=-1;for(const a of Object.values(e.rememberVideoSpeedList)){i=a.indexOf(t);if(i!==-1){a.splice(i,1);break}}e.rememberVideoSpeedList=e.rememberVideoSpeedList;return i!==-1}static rememberSpeed(t,i=false,s){s=getAid(s);const d=a.forgetSpeed(s);if(!d&&!i){return}if(!e.rememberVideoSpeedList[t]){e.rememberVideoSpeedList[t]=[]}e.rememberVideoSpeedList[t].push(s);e.rememberVideoSpeedList=e.rememberVideoSpeedList}static async getInstance(e,t){const i=await SpinQuery.select(`.${a.classNameMap.speedContainer}`);const s=await SpinQuery.select(`.${a.classNameMap.video} video`);if(!i){throw"speed container element not found!"}if(!s){throw"video element not found!"}return new a(i,s,e,t)}constructor(e,t,s,d){i(this,"_containerElement",void 0);i(this,"_menuListElement",void 0);i(this,"_nameBtn",void 0);i(this,"_videoElement",void 0);i(this,"_nativeSpeedVal",void 0);i(this,"_previousSpeedVal",void 0);const n=a.instanceMap.get(e);if(n&&(!s||n._previousSpeedVal===s)&&(!d||n._nativeSpeedVal===d)){return n}this._videoElement=t;this._containerElement=e;this._previousSpeedVal=s;this._nativeSpeedVal=d!==null&&d!==void 0?d:s&&a.nativeSupportedRates.includes(s)?s:1;this._nameBtn=this._containerElement.querySelector(`.${a.classNameMap.speedNameBtn}`);this._menuListElement=this._containerElement.querySelector(`.${a.classNameMap.speedMenuList}`);a.instanceMap.set(e,this)}get menuListElement(){return this._menuListElement}get containerElement(){return this._containerElement}get videoElement(){return this._videoElement}get playbackRate(){return this._videoElement.playbackRate}getSpeedMenuItem(e){if(e){return this._menuListElement.querySelector(`.${a.classNameMap.speedMenuItem}[data-value="${e}"]`)}return this._menuListElement.querySelector(`.${a.classNameMap.speedMenuItem}.${a.classNameMap.active}`)}observe(){Observer.all(this._menuListElement,(t=>{let[i,s]=[undefined,undefined];t.forEach((t=>{var d;const n=t.target;if(!n.classList.contains(a.classNameMap.active)){var r;i=parseFloat((r=n.dataset.value)!==null&&r!==void 0?r:"1");return}s=parseFloat((d=n.dataset.value)!==null&&d!==void 0?d:"1");let l=false;if(a.nativeSupportedRates.includes(s)){this._nativeSpeedVal=s;l=true}this._containerElement.dispatchEvent(new CustomEvent("changed",{detail:{speed:s,isNativeSpeed:l,previousSpeed:this._previousSpeedVal}}));if(e.extendVideoSpeed&&a.nativeSupportedRates.includes(s)){var o;(o=this._menuListElement.querySelector(`.${a.classNameMap.speedMenuItem}.extended.${a.classNameMap.active}`))===null||o===void 0?void 0:o.classList.remove(a.classNameMap.active)}if(e.useDefaultVideoSpeed){if(e.rememberVideoSpeed){a.rememberSpeed(s,s!==a.fallbackVideoSpeed)}else{e.defaultVideoSpeed=s.toString()}}}));if(i&&i!==s){this._previousSpeedVal=i}}))}toggleVideoSpeed(e="smart",t=false){switch(e){case"smart":this.playbackRate===1?this.setVideoSpeed(this._previousSpeedVal):this.reset(t);break;case"classic":this.setVideoSpeed(this._previousSpeedVal);break;default:break}}reset(e=false){if(e){const e=a.fallbackVideoSpeed;if(!e){return}a.forgetSpeed();this.setVideoSpeed(e)}else{this.setVideoSpeed(1)}}setVideoSpeed(e){e&&this.getSpeedMenuItem(e).click()}setExtendedVideoSpeed(e){if(a.nativeSupportedRates.includes(e)){this.getSpeedMenuItem(e).click()}else{this.forceUpdate(e)}}forceUpdate(e){var t,i;(t=this._menuListElement.querySelector(`.${a.classNameMap.speedMenuItem}[data-value="${this.playbackRate}"]`))===null||t===void 0?void 0:t.classList.remove(a.classNameMap.active);(i=this._menuListElement.querySelector(`.${a.classNameMap.speedMenuItem}[data-value="${e}"]`))===null||i===void 0?void 0:i.classList.add(a.classNameMap.active);this._videoElement.playbackRate=e;this._containerElement.classList.remove(a.classNameMap.show);this._nameBtn.innerText=a.formatSpeedText(e)}}i(a,"classNameMap",{speedMenuList:"bilibili-player-video-btn-speed-menu",speedMenuItem:"bilibili-player-video-btn-speed-menu-list",speedNameBtn:"bilibili-player-video-btn-speed-name",speedContainer:"bilibili-player-video-btn-speed",active:"bilibili-player-active",show:"bilibili-player-speed-show",video:"bilibili-player-video"});i(a,"nativeSupportedRates",[.5,.75,1,1.25,1.5,2]);i(a,"instanceMap",new WeakMap);i(a,"init",_.once((()=>{let i=undefined;let s=1;let d;Observer.videoChange((async()=>{const{getExtraSpeedMenuItemElements:n}=await t.importAsync("extend-video-speed");const{calcOrder:r}=await t.importAsync("video-speed-common");const l=await a.getInstance(i,s);d=l.containerElement;if(d.classList.contains("extended")){return}if(e.extendVideoSpeed){l._menuListElement.prepend(...await n());l._menuListElement.querySelectorAll(`.${a.classNameMap.speedMenuItem}[data-value]:not(.extended)`).forEach((e=>{e.style.order=r(parseFloat(e.getAttribute("data-value")))}));l._menuListElement.addEventListener("click",(e=>{const t=e.target;const i=parseFloat(t.dataset.value);if(e.target.classList.contains("extended")){l.setExtendedVideoSpeed(i)}if(a.extendedSupportedRates.includes(l.playbackRate)&&l._nativeSpeedVal===i){l.forceUpdate(i)}}))}l.observe();d.addEventListener("changed",(({detail:{speed:e,isNativeSpeed:t}})=>{i=e;if(t){s=e}}));setTimeout((()=>{l.setVideoSpeed(e.useDefaultVideoSpeed&&e.rememberVideoSpeed&&a.getRememberSpeed()||a.fallbackVideoSpeed||i)}),100);d.classList.add("extended")}))})));return{export:{VideoSpeedController:a}}})(); |