mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
(()=>{return(t,i)=>{const e=t=>{let i=0;let e=0;while(t&&!isNaN(t.offsetLeft)&&!isNaN(t.offsetTop)){i+=t.offsetLeft-t.scrollLeft;e+=t.offsetTop-t.scrollTop;t=t.offsetParent}return{x:i,y:e}};const s=(t,i=1)=>{const e=t.toString();const s=e.indexOf(".");if(s!==-1){if(e.length-s>i+1){return e.substring(0,s+i+1)}else{return e}}else{return e+".0"}};const o=t=>{t=Math.abs(t);const i=Math.floor(t/3600);const e=Math.floor((t-i*3600)/60);const o=t-i*3600-e*60;let n=s(o)+"秒";if(e>0){n=e+"分"+n}if(i>0){n=i+"时"+n}return n};const n=t=>{t=Math.abs(t);const i=Math.floor(t/3600);const e=Math.floor((t-i*3600)/60);const o=t-i*3600-e*60;let n=(o<10?"0":"")+s(o);n=(e<10?"0":"")+e+":"+n;n=(i<10?"0":"")+i+":"+n;return n};class a{constructor(t){this.action=new l(t);this.onTouchStart=null;this.onTouchEnd=null;this._direction=null;t.addEventListener("touchstart",t=>{this._xDown=t.touches[0].clientX;this._yDown=t.touches[0].clientY;if(this.onTouchStart){this.onTouchStart(t)}});t.addEventListener("touchmove",i=>{if(!this._xDown||!this._yDown){return}const s=i.touches[0].clientX;const o=i.touches[0].clientY;const n=e(t);const a={x:(i.touches[0].pageX-n.x)/t.clientWidth,y:(i.touches[0].pageY-n.y)/t.clientHeight,width:t.clientWidth,height:t.clientHeight};const l=this._xDown-s;const c=this._yDown-o;if(!this._direction){let t="";if(Math.abs(l)>Math.abs(c)){t="horizontal"}else{t="vertical"}this._direction=t;i.preventDefault()}else{if(this._direction==="vertical"){this.action.startAction(this._direction,c,a)}else if(this._direction==="horizontal"){this.action.startAction(this._direction,-l,a)}i.preventDefault()}});t.addEventListener("touchend",t=>{this._xDown=null;this._yDown=null;this._direction=null;if(this.onTouchEnd){this.onTouchEnd(t)}})}}class l{constructor(t){this.lowSpeedForward=null;this.lowSpeedBackward=null;this.mediumSpeedForward=null;this.mediumSpeedBackward=null;this.highSpeedForward=null;this.highSpeedBackward=null;this.lowVolumeUp=null;this.lowVolumeDown=null;this.mediumVolumeUp=null;this.mediumVolumeDown=null;this.highVolumeUp=null;this.highVolumeDown=null;this.speedCancel=null;this.volumeCancel=null;this.minSwipeDistance=20;this.onActionStart=null;this.onActionEnd=null;this._element=t;this._touchStart=false;this._startPosition=null;this._lastAction=null;t.addEventListener("touchstart",t=>{this._touchStart=true});t.addEventListener("touchend",t=>{this._startPosition=null;this.onActionEnd&&this.onActionEnd(this._lastAction);this._lastAction=null})}startAction(t,i,e){if(this._touchStart){this.onActionStart&&this.onActionStart(t);this._startPosition=e;this._touchStart=false}if(t==="vertical"){if(Math.abs(i)<this.minSwipeDistance){this.volumeCancel&&this.volumeCancel();this._lastAction=null}else{let t=0;let s=undefined;let o=undefined;if(this._startPosition.x<1/3){t=.4;s=this.lowVolumeUp;o=this.lowVolumeDown}else if(this._startPosition.x>=1/3&&this._startPosition.x<=2/3){t=1;s=this.mediumVolumeUp;o=this.mediumVolumeDown}else{t=2;s=this.highVolumeUp;o=this.highVolumeDown}if(i>0){const o=Math.round(t*100*(i-this.minSwipeDistance)/(1.5*e.height));s&&s(o);this._lastAction={type:"volume",volume:o}}else{const s=Math.round(t*100*(i+this.minSwipeDistance)/(1.5*e.height));o&&o(s);this._lastAction={type:"volume",volume:s}}}}else if(t==="horizontal"){if(e.y<1/3&&(e.x<.1||e.x>.9)||Math.abs(i)<this.minSwipeDistance){this.speedCancel&&this.speedCancel();this._lastAction=null}else{let t=0;let e=undefined;let s=undefined;if(this._startPosition.y<1/3){t=.05;e=this.lowSpeedForward;s=this.lowSpeedBackward}else if(this._startPosition.y>=1/3&&this._startPosition.y<=2/3){t=.2;e=this.mediumSpeedForward;s=this.mediumSpeedBackward}else{t=1;e=this.highSpeedForward;s=this.highSpeedBackward}if(i>0){const s=(i-this.minSwipeDistance)*t;e&&e(s);this._lastAction={type:"playback",seconds:s}}else{const e=(i+this.minSwipeDistance)*t;s&&s(e);this._lastAction={type:"playback",seconds:e}}}}}}class c{constructor(){const t=window.location.href;const i=t.match(/av([\d]+)/);this.aid=i[1];const e=t.match(/p=([\d]+)/);this.page=e?e[1]:1;this.pagesData=null;this.cidMap={};this.cidData={};this.supportWebp=c.supportWebp;downloadText(t,t=>this.findCid(t))}findCid(t){const i=t.match(/"pages":(\[[^\0]*\]),"embedPlayer"/);if(i){this.pagesData=JSON.parse(i[1]);this.pagesData.forEach(t=>{this.cidMap[t.page]=t.cid})}}getVideoshot(t,i){if(!this.cidData[this.page]){downloadText(`https://api.bilibili.com/x/player/videoshot?aid=${this.aid}&cid=${this.cidMap[this.page]}&index=1`,e=>{this.cidData[this.page]=JSON.parse(e).data;this.getVideoshot(t,i)})}else{const e=this.cidData[this.page];const s=e.index;let o=0;for(let i=0;i<s.length-2;i++){if(t>=s[i]&&t<s[i+1]){o=i;break}}let n=e.image;if(this.supportWebp){n=n.map(t=>t.replace(".jpg",".jpg@.webp"))}const a=parseInt(e.pv_x_len)||10;const l=parseInt(e.pv_y_len)||10;const c=parseInt(e.pv_x_size)||160;const r=parseInt(e.pv_y_size)||90;const h=-(o%100%a)*c;const d=-Math.floor(o%100/l)*r;i({width:c,height:r,backgroundImage:`url(${n[Math.floor(o/100)]})`,backgroundPosition:`${h}px ${d}px`})}}static get supportWebp(){try{const t=document.createElement("canvas");if(t.getContext&&t.getContext("2d"))try{return t.toDataURL("image/webp").indexOf("data:image/webp")===0}catch(t){return false}else return false}catch(t){return false}}}function r(t){t.unbind("click")}function h(i){if($(".touch-video-box").length!==0){return}$(".bilibili-player-video-subtitle").before(`\n <div class='touch-video-box-wrapper'>\n <div class='touch-video-box adjust-closed'>\n <div class='touch-video-info'></div>\n <div class='touch-progress'></div>\n </div>\n </div>`);const e=$("video");const l=e.prop("duration");const r=new a(i.get(0));const h=document.getElementsByClassName("touch-video-info")[0];const d=document.getElementsByClassName("touch-video-box")[0];let u=Math.round(e.prop("volume")*100);const p=t=>{t/=100;if(t<0){t=0}else if(t>1){t=1}e.prop("volume",t);$(".bilibili-player-video-volume-num").text(Math.round(t*100));$(".bui-thumb").css("transform",`translateY(-${48*t}px)`);$(".bui-track-vertical .bui-bar").css("transform",`scaleY(${t})`);if(t===0){$(".bilibili-player-video-btn-volume").addClass(".video-state-volume-min");$(".bilibili-player-video-btn-volume").removeClass(".video-state-volume-max");e.prop("muted",true)}else if(t===1){$(".bilibili-player-video-btn-volume").removeClass(".video-state-volume-min");$(".bilibili-player-video-btn-volume").addClass(".video-state-volume-max");e.prop("muted",false)}else{$(".bilibili-player-video-btn-volume").removeClass(".video-state-volume-min");$(".bilibili-player-video-btn-volume").removeClass(".video-state-volume-max");e.prop("muted",false)}};if(t.touchVideoPlayerAnimation){d.classList.add("animation")}r.action.onActionStart=(t=>{d.classList.add("adjust-opened");h.innerHTML="";u=Math.round(e.prop("volume")*100)});const m=new c;const v=t=>{return i=>{const a=e.prop("currentTime");let c=a+i;let r=s(100*c/l);let d=i;if(c>l){c=l;r=100;d=l-a}else if(c<0){c=0;r=0;d=a}const u=`${n(a)} →<br/>${n(c)} (${r}%)`;const p=`\n <div class='touch-row'>\n <div class='touch-row-item'>\n <span class='touch-speed'>${t}速</span>\n </div>\n <div class='touch-row-item-wide'>\n <span class='touch-info'>进度: ${i>0?"+":"-"}${o(d)}</span>\n </div>\n </div>\n <div class='touch-row'>\n <div class='videoshot-wrapper touch-row-item'>\n <div class='videoshot'></div>\n </div>\n <div class='touch-row-item-wide'>\n <span class='touch-result'>${u}</span>\n </div>\n </div>\n `;h.innerHTML=p;m.getVideoshot(c,t=>$(".videoshot").css(t));$(".touch-progress").css("transform",`scaleX(${r/100})`)}};r.action.lowSpeedBackward=v("低");r.action.lowSpeedForward=v("低");r.action.mediumSpeedBackward=v("中");r.action.mediumSpeedForward=v("中");r.action.highSpeedBackward=v("高");r.action.highSpeedForward=v("高");const f=t=>{return i=>{let e=u+i;let s=Math.abs(i);if(e>100){e=100;s=100-u}else if(e<0){e=0;s=u}const o=`${u} → ${e}`;p(e);const n=`\n <div class='touch-row'>\n <div class='touch-row-item'>\n <span class='touch-speed'>${t}速</span>\n </div>\n <div class='touch-row-item-wide'>\n <span class='touch-info'>音量: ${i>0?"+":"-"}${s}</span>\n </div>\n </div>\n <div class='touch-row'>\n <div class='touch-row-item'>\n <span class='touch-result'>${o}</span>\n </div>\n </div>\n `;h.innerHTML=n;$(".touch-progress").css("transform",`scaleX(${e/100})`)}};r.action.lowVolumeUp=f("低");r.action.lowVolumeDown=f("低");r.action.mediumVolumeUp=f("中");r.action.mediumVolumeDown=f("中");r.action.highVolumeUp=f("高");r.action.highVolumeDown=f("高");r.action.speedCancel=(()=>{h.innerHTML=`松开手指,取消进退`;$(".touch-progress").css("transform","scaleX(0)")});r.action.volumeCancel=(()=>{h.innerHTML=`松开手指,取消调整`;$(".touch-progress").css("transform","scaleX(0)");p(u)});if(!unsafeWindow.TOUCH_PLAYER_DEBUG){r.action.onActionEnd=(t=>{h.innerHTML="";if(t){if(t.type==="playback"){let i=e.prop("currentTime");i+=t.seconds;if(i<0){i=0}else if(i>l){i=l}e.prop("currentTime",i)}}d.classList.remove("adjust-opened")})}}function d(t){if(!t.hasClass("disable-original-hover")){t.addClass("disable-original-hover");const i=unsafeWindow.$(".bilibili-player-video");const e="touch-video-control-show";const s=i.data("events").click[0].handler;let o=false;const n=(i,o)=>{if(i){s(o)}else{t.toggleClass(e)}};const a=t=>{if(!o){o=true;setTimeout(()=>{if(o){o=false;n(false,t)}},160)}else{o=false;n(true,t)}};i.unbind("click");i.on("click",a)}}function u(){SpinQuery.any(()=>$(".bilibili-player-iconfont,.bilibili-player-video-quality-menu"),r);new SpinQuery(()=>$(".bilibili-player-video"),t=>t.length>0&&$("video").length>0&&$("video").prop("duration"),h).start();if(t.touchVideoPlayerDoubleTapControl){new SpinQuery(()=>$(".bilibili-player-area"),t=>t.length>0&&unsafeWindow.$&&unsafeWindow.$(".bilibili-player-video").data("events"),d).start()}}runAndObserveDomMutation("#bofqi",()=>u());i.applyStyle("touchPlayerStyle","bilibili-touch-video-player");return{ajaxReload:false}}})(); |