Bilibili-Evolved/min/video-speed-controller.min.js
2021-09-11 10:53:58 +08:00

1 line
7.1 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 s{static get extendedSupportedRates(){return Array.from(new Set(e.extendVideoSpeedList)).sort(((e,t)=>e-t))}static get supportedRates(){if(e.extendVideoSpeed){return[...s.nativeSupportedRates,...s.extendedSupportedRates].sort(((e,t)=>e-t))}return s.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,s]of Object.entries(e.rememberVideoSpeedList)){if(s.some((e=>e===getAid(t)))){return parseFloat(i)}}}static forgetSpeed(t){t=getAid(t);let i=-1;for(const s of Object.values(e.rememberVideoSpeedList)){i=s.indexOf(t);if(i!==-1){s.splice(i,1);break}}e.rememberVideoSpeedList=e.rememberVideoSpeedList;return i!==-1}static rememberSpeed(t,i=false,a){a=getAid(a);const n=s.forgetSpeed(a);if(!n&&!i){return}if(!e.rememberVideoSpeedList[t]){e.rememberVideoSpeedList[t]=[]}e.rememberVideoSpeedList[t].push(a);e.rememberVideoSpeedList=e.rememberVideoSpeedList}static async getInstance(e,t){const i=await SpinQuery.select(`.${s.classNameMap.speedContainer}`);const a=await SpinQuery.select(`.${s.classNameMap.video} video`);if(!i){throw"speed container element not found!"}if(!a){throw"video element not found!"}return new s(i,a,e,t)}constructor(e,t,a,n){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 d=s.instanceMap.get(e);if(d&&(!a||d._previousSpeedVal===a)&&(!n||d._nativeSpeedVal===n)){return d}this._videoElement=t;this._containerElement=e;this._previousSpeedVal=a;this._nativeSpeedVal=n!==null&&n!==void 0?n:a&&s.nativeSupportedRates.includes(a)?a:1;this._nameBtn=this._containerElement.querySelector(`.${s.classNameMap.speedNameBtn}`);this._menuListElement=this._containerElement.querySelector(`.${s.classNameMap.speedMenuList}`);this._menuListElement.querySelectorAll(`.${s.classNameMap.speedMenuItem}`).forEach((e=>{if(!e.hasAttribute("data-value")){const t=parseFloat(e.textContent).toString();e.setAttribute("data-value",t)}}));s.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(`.${s.classNameMap.speedMenuItem}[data-value="${e}"]`)}return this._menuListElement.querySelector(`.${s.classNameMap.speedMenuItem}.${s.classNameMap.active}`)}observe(){Observer.all(this._menuListElement,(t=>{let[i,a]=[undefined,undefined];t.forEach((t=>{var n;const d=t.target;if(!d.classList.contains(s.classNameMap.active)){var r;i=parseFloat((r=d.dataset.value)!==null&&r!==void 0?r:"1");return}a=parseFloat((n=d.dataset.value)!==null&&n!==void 0?n:"1");let l=false;if(s.nativeSupportedRates.includes(a)){this._nativeSpeedVal=a;l=true}this._containerElement.dispatchEvent(new CustomEvent("changed",{detail:{speed:a,isNativeSpeed:l,previousSpeed:this._previousSpeedVal}}));if(e.extendVideoSpeed&&s.nativeSupportedRates.includes(a)){var o;(o=this._menuListElement.querySelector(`.${s.classNameMap.speedMenuItem}.extended.${s.classNameMap.active}`))===null||o===void 0?void 0:o.classList.remove(s.classNameMap.active)}if(e.useDefaultVideoSpeed){if(e.rememberVideoSpeed){s.rememberSpeed(a,a!==s.fallbackVideoSpeed)}else{e.defaultVideoSpeed=a.toString()}}}));if(i&&i!==a){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=s.fallbackVideoSpeed;if(!e){return}s.forgetSpeed();this.setVideoSpeed(e)}else{this.setVideoSpeed(1)}}setVideoSpeed(e){e&&this.getSpeedMenuItem(e).click()}setExtendedVideoSpeed(e){if(s.nativeSupportedRates.includes(e)){this.getSpeedMenuItem(e).click()}else{this.forceUpdate(e)}}forceUpdate(e){var t,i;(t=this._menuListElement.querySelector(`.${s.classNameMap.speedMenuItem}[data-value="${this.playbackRate}"]`))===null||t===void 0?void 0:t.classList.remove(s.classNameMap.active);(i=this._menuListElement.querySelector(`.${s.classNameMap.speedMenuItem}[data-value="${e}"]`))===null||i===void 0?void 0:i.classList.add(s.classNameMap.active);this._videoElement.playbackRate=e;this._containerElement.classList.remove(s.classNameMap.show);this._nameBtn.innerText=s.formatSpeedText(e)}}i(s,"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(s,"nativeSupportedRates",[.5,.75,1,1.25,1.5,2]);i(s,"instanceMap",new WeakMap);i(s,"init",_.once((async()=>{let i=undefined;let a=1;let n;const{playerAgent:d}=await t.importAsync("player-agent");const r=d.provideCustomQuery({video:{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"},bangumi:{speedMenuList:"squirtle-speed-select-list",speedMenuItem:"squirtle-select-item",speedNameBtn:"squirtle-speed-select-result",speedContainer:"squirtle-speed-wrap",active:"active",show:"bilibili-player-speed-show"}});const l=e=>e.replace(/^\./,"");s.classNameMap={speedMenuList:l(r.custom.speedMenuList.selector),speedMenuItem:l(r.custom.speedMenuItem.selector),speedNameBtn:l(r.custom.speedNameBtn.selector),speedContainer:l(r.custom.speedContainer.selector),active:l(r.custom.active.selector),show:l(r.custom.show.selector),video:l(r.query.video.container.selector)};Observer.videoChange((async()=>{const{getExtraSpeedMenuItemElements:d}=await t.importAsync("extend-video-speed");const{calcOrder:r}=await t.importAsync("video-speed-common");const l=await s.getInstance(i,a);n=l.containerElement;if(n.classList.contains("extended")){return}if(e.extendVideoSpeed){l._menuListElement.prepend(...await d());l._menuListElement.querySelectorAll(`.${s.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(s.extendedSupportedRates.includes(l.playbackRate)&&l._nativeSpeedVal===i){l.forceUpdate(i)}}))}l.observe();n.addEventListener("changed",(({detail:{speed:e,isNativeSpeed:t}})=>{i=e;if(t){a=e}}));setTimeout((()=>{l.setVideoSpeed(e.useDefaultVideoSpeed&&e.rememberVideoSpeed&&s.getRememberSpeed()||s.fallbackVideoSpeed||i)}),100);n.classList.add("extended")}))})));return{export:{VideoSpeedController:s}}})();