mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
4.8 KiB
JavaScript
1 line
4.8 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 getDefaultVideoSpeed(){return parseFloat(e.defaultVideoSpeed)}static formatSpeedText(e){if(e===1){return"倍数"}return Math.trunc(e)===e?`${e}.0x`:`${e}x`}static getSpeedFromSetting(){for(const[t,i]of Object.entries(e.rememberVideoSpeedList)){if(i.some((e=>e===unsafeWindow.aid))){return parseFloat(t)}}}static rememberSpeed(t,i,a=false){let s=-1;for(const t of Object.values(e.rememberVideoSpeedList)){s=t.indexOf(i);if(s!==-1){t.splice(s,1);break}}if(s===-1&&!a){return}if(!e.rememberVideoSpeedList[t]){e.rememberVideoSpeedList[t]=[]}e.rememberVideoSpeedList[t].push(i);e.rememberVideoSpeedList=e.rememberVideoSpeedList}constructor(e,t,s){i(this,"_containerElement",void 0);i(this,"_menuListElement",void 0);i(this,"_nameBtn",void 0);i(this,"_videoElement",void 0);i(this,"_nativeSpeedVal",1);this._containerElement=e;this._videoElement=t;this._nativeSpeedVal=s;this._nameBtn=this._containerElement.querySelector(`.${a.classNameMap.speedNameBtn}`);this._menuListElement=this._containerElement.querySelector(`.${a.classNameMap.speedMenuList}`)}get playbackRate(){return this._videoElement.playbackRate}get nativeSpeedVal(){return this._nativeSpeedVal}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=>{t.forEach((t=>{const i=t.target;if(!i.classList.contains(a.classNameMap.active)){return}const s=parseFloat(i.dataset.value||"1");this._containerElement.dispatchEvent(new CustomEvent("changed",{detail:{speed:s}}));if(a.nativeSupportedRates.includes(s)){this._nativeSpeedVal=s;this._containerElement.dispatchEvent(new CustomEvent("native-speed-changed",{detail:{speed:this._nativeSpeedVal}}))}if(e.extendVideoSpeed&&a.nativeSupportedRates.includes(s)){var n;(n=this._menuListElement.querySelector(`.${a.classNameMap.speedMenuItem}.extended.${a.classNameMap.active}`))===null||n===void 0?void 0:n.classList.remove(a.classNameMap.active)}if(!unsafeWindow.aid){throw"aid is undefined"}if(e.useDefaultVideoSpeed){if(e.rememberVideoSpeed){a.rememberSpeed(s,unsafeWindow.aid,s!==a.getDefaultVideoSpeed())}else{e.defaultVideoSpeed=s.toString()}}}))}))}extendMenuItem(){this._menuListElement.addEventListener("click",(e=>{const t=e.target;const i=parseFloat(t.dataset.value);if(e.target.classList.contains("extended")){this.setUnsafeVideoSpeed(i)}if(a.extendedSupportedRates.includes(this.playbackRate)&&this._nativeSpeedVal===i){this.forceUpdate(i)}}));for(const e of a.extendedSupportedRates){const t=document.createElement("li");t.innerText=a.formatSpeedText(e);t.classList.add(a.classNameMap.speedMenuItem,"extended");t.dataset.value=e.toString();this._menuListElement.prepend(t)}}reset(){this.setUnsafeVideoSpeed(1)}setVideoSpeed(e){if(a.supportedRates.includes(e)){this.getSpeedMenuItem(e).click()}}setUnsafeVideoSpeed(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,"extendedSupportedRates",[2.5,3]);i(a,"supportedRates",e.extendVideoSpeed?[...a.nativeSupportedRates,...a.extendedSupportedRates]:a.nativeSupportedRates);if(e.useDefaultVideoSpeed||e.extendVideoSpeed){let t=1;Observer.videoChange((async()=>{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!"}const n=new a(i,s,t);n.observe();if(e.extendVideoSpeed){n.extendMenuItem();i.addEventListener("native-speed-changed",(e=>{t=e.detail.speed}))}setTimeout((()=>{e.useDefaultVideoSpeed&&n.setVideoSpeed(e.rememberVideoSpeed&&a.getSpeedFromSetting()||a.getDefaultVideoSpeed())}),100)}))}return{export:{VideoSpeedController:a}}})(); |