Bilibili-Evolved/min/touch-player.min.js
2019-07-07 12:26:13 +08:00

1 line
9.3 KiB
JavaScript

(()=>{return(t,e)=>{const i=t=>{let e=0;let i=0;while(t&&!isNaN(t.offsetLeft)&&!isNaN(t.offsetTop)){e+=t.offsetLeft-t.scrollLeft;i+=t.offsetTop-t.scrollTop;t=t.offsetParent}return{x:e,y:i}};const s=t=>{t=Math.abs(t);const e=Math.floor(t/3600);const i=Math.floor((t-e*3600)/60);const s=t-e*3600-i*60;let o=fixed(s)+"秒";if(i>0){o=i+"分"+o}if(e>0){o=e+"时"+o}return o};const o=t=>{t=Math.abs(t);const e=Math.floor(t/3600);const i=Math.floor((t-e*3600)/60);const s=t-e*3600-i*60;let o=(s<10?"0":"")+fixed(s);o=(i<10?"0":"")+i+":"+o;o=(e<10?"0":"")+e+":"+o;return o};class n{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",e=>{if(!this.xDown||!this.yDown){return}const s=e.touches[0].clientX;const o=e.touches[0].clientY;const n=i(t);const l={x:(e.touches[0].pageX-n.x)/t.clientWidth,y:(e.touches[0].pageY-n.y)/t.clientHeight,width:t.clientWidth,height:t.clientHeight};const a=this.xDown-s;const c=this.yDown-o;if(!this.direction){let t="";if(Math.abs(a)>Math.abs(c)){t="horizontal"}else{t="vertical"}this.direction=t}else{if(this.direction==="vertical"){this.action.startAction(this.direction,c,l)}else if(this.direction==="horizontal"){this.action.startAction(this.direction,-a,l)}}if(e.cancelable){e.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.volumeUp=null;this.volumeDown=null;this.brightnessUp=null;this.brightnessDown=null;this.speedCancel=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",()=>{this.touchStart=true});t.addEventListener("touchend",()=>{this.startPosition=null;this.onActionEnd&&this.onActionEnd(this.lastAction);this.lastAction=null})}startAction(t,e,i){if(this.touchStart){this.onActionStart&&this.onActionStart(t);this.startPosition=i;this.touchStart=false}if(t==="vertical"){let t;let s;if(this.startPosition.x<1/2){t=this.brightnessUp;s=this.brightnessDown;const o=Math.round(200*(e-this.minSwipeDistance)/(1.5*i.height));if(e>0){t&&t(o)}else{s&&s(o)}this.lastAction={type:"brightness",brightness:o}}else{t=this.volumeUp;s=this.volumeDown;const o=Math.round(200*(e-this.minSwipeDistance)/(1.5*i.height));if(e>0){t&&t(o)}else{s&&s(o)}this.lastAction={type:"volume",volume:o}}}else if(t==="horizontal"){if(i.y<1/3&&(i.x<.1||i.x>.9)){this.speedCancel&&this.speedCancel();this.lastAction=null}else{let t=0;let i;let s;if(this.startPosition.y<1/3){t=.05;i=this.lowSpeedForward;s=this.lowSpeedBackward}else if(this.startPosition.y>=1/3&&this.startPosition.y<=2/3){t=.2;i=this.mediumSpeedForward;s=this.mediumSpeedBackward}else{t=1;i=this.highSpeedForward;s=this.highSpeedBackward}if(e>0){const s=(e-this.minSwipeDistance)*t;i&&i(s);this.lastAction={type:"playback",seconds:s}}else{const i=(e+this.minSwipeDistance)*t;s&&s(i);this.lastAction={type:"playback",seconds:i}}}}}}class a{constructor(){this.aid=unsafeWindow.aid;this.cid=unsafeWindow.cid;this.cidData=null;this.supportWebp=a.supportWebp}getVideoshot(t,e){if(!(this.aid&&this.cid)){return}if(!this.cidData){Ajax.getText(`https://api.bilibili.com/x/player/videoshot?aid=${this.aid}&cid=${this.cid}&index=1`).then(i=>{this.cidData=JSON.parse(i).data;this.getVideoshot(t,e)})}else{const i=this.cidData;const s=i.index;let o=0;for(let e=0;e<s.length-2;e++){if(t>=s[e]&&t<s[e+1]){o=e;break}}let n=i.image;if(n===null){return}if(this.supportWebp){n=n.map(t=>t.replace(".jpg",".jpg@.webp"))}const l=parseInt(i.pv_x_len)||10;const a=parseInt(i.pv_y_len)||10;const c=parseInt(i.pv_x_size)||160;const r=parseInt(i.pv_y_size)||90;const h=-(o%100%l)*c;const d=-Math.floor(o%100/a)*r;e({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 c(t){t.unbind("click")}function r(t){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 animation'>\n <div class='touch-video-info'>\n <div class='touch-row'>\n <div class='touch-row-item'>\n <span class='touch-speed'></span>\n </div>\n <div class='touch-row-item-wide'>\n <span class='touch-info'></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'></span>\n </div>\n </div>\n </div>\n <div class='touch-progress'></div>\n </div>\n </div>`);let e=$("video");const i=e.prop("duration");const l=new n(t.get(0));const c=document.querySelector(".touch-video-info");const r=document.querySelector(".touch-video-box");let h=100;let d=Math.round(e.prop("volume")*100);const u=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)}};l.action.onActionStart=(t=>{r.classList.add("adjust-opened");c.classList[t==="vertical"?"remove":"add"]("speed");e=$("video");d=Math.round(e.prop("volume")*100);const i=e.css("filter").match(/brightness\((.+)\)/);h=Math.trunc((i?i[1]:1)*100)});const p=new a;const v=t=>{return n=>{const l=e.prop("currentTime");const a=fixed(100*l/i);let r=l+n;let h=fixed(100*r/i);let d=n;if(r>i){r=i;h=100;d=i-l}else if(r<0){r=0;h=0;d=l}const u=`${o(l)} (${a}%)<br>👇<br>${o(r)} (${h}%)`;c.classList.remove("cancel");c.querySelector(".touch-speed").innerHTML=`${t}`;c.querySelector(".touch-info").innerHTML=`进度: ${n>0?"+":"-"}${s(d)}`;c.querySelector(".touch-result").innerHTML=u;p.getVideoshot(r,t=>$(".videoshot").css(t));$(".touch-progress").css("transform",`scaleX(${h/100})`)}};l.action.lowSpeedBackward=v("低");l.action.lowSpeedForward=v("低");l.action.mediumSpeedBackward=v("中");l.action.mediumSpeedForward=v("中");l.action.highSpeedBackward=v("高");l.action.highSpeedForward=v("高");const f=t=>{let e=d+t;let i=Math.abs(t);if(e>100){e=100;i=100-d}else if(e<0){e=0;i=d}const s=`${d} 👉 ${e}`;u(e);c.classList.remove("cancel");c.querySelector(".touch-info").innerHTML=`音量: ${t>0?"+":"-"}${i}`;c.querySelector(".touch-result").innerHTML=s;$(".touch-progress").css("transform",`scaleX(${e/100})`)};l.action.volumeUp=f;l.action.volumeDown=f;const b=t=>{let i=h+t;let s=Math.abs(t);if(i>100){i=100;s=100-h}else if(i<0){i=0;s=h}const o=`${h} 👉 ${i}`;e.css("filter",`brightness(${i/100})`);c.classList.remove("cancel");c.querySelector(".touch-info").innerHTML=`亮度: ${t>0?"+":"-"}${s}`;c.querySelector(".touch-result").innerHTML=o;$(".touch-progress").css("transform",`scaleX(${i/100})`)};l.action.brightnessUp=b;l.action.brightnessDown=b;l.action.speedCancel=(()=>{c.querySelector(".touch-info").innerHTML=`取消时间调整`;c.classList.add("cancel")});if(!unsafeWindow.TOUCH_PLAYER_DEBUG){l.action.onActionEnd=(t=>{if(t){if(t.type==="playback"){let s=e.prop("currentTime");s+=t.seconds;if(s<0){s=0}else if(s>i){s=i}e.prop("currentTime",s)}else if(t.type==="volume"){const t=JSON.parse(localStorage.bilibili_player_settings);t.video_status.volume=e.prop("volume");localStorage.bilibili_player_settings=JSON.stringify(t)}}r.classList.remove("adjust-opened")})}}function h(t){if(!t.hasClass("disable-original-hover")){t.addClass("disable-original-hover");const e=unsafeWindow.$(".bilibili-player-video");const i="touch-video-control-show";const s=e.data("events").click[0].handler;const o=new DoubleClickEvent(t=>s(t),()=>t.toggleClass(i));e.unbind("click");o.bind(e[0])}}const d=()=>{SpinQuery.any(()=>$(".bilibili-player-iconfont,.bilibili-player-video-quality-menu"),c);SpinQuery.condition(()=>$(".bilibili-player-video"),t=>t.length>0&&$("video").length>0&&$("video").prop("duration"),r);if(t.touchVideoPlayerDoubleTapControl){new SpinQuery(()=>$(".bilibili-player-area"),t=>t.length>0&&unsafeWindow.$&&unsafeWindow.$(".bilibili-player-video").data("events"),h).start()}};Observer.videoChange(d);e.applyStyle("touchPlayerStyle")}})();