Bilibili-Evolved/min/keymap.min.js
2020-03-29 13:44:03 +08:00

1 line
3.6 KiB
JavaScript

(()=>{return(e,t)=>{const i=["https://www.bilibili.com/bangumi/","https://www.bilibili.com/video/"];if(i.some(e=>document.URL.startsWith(e))){const i={f:".bilibili-player-video-fullscreen",w:".bilibili-player-video-web-fullscreen",t:".bilibili-player-video-btn-widescreen",r:".bilibili-player-video-btn-repeat",m:".bilibili-player-video-btn-volume .bilibili-player-iconfont-volume",c:".video-toolbar .coin,.tool-bar .coin-info",s:".video-toolbar .collect"," ":".bilibili-player-video-btn-start"};let n=true;let a;const o=e=>{let i=dq(".keymap-playback-tip");if(!i){const e=dq(".bilibili-player-video-wrap");if(!e){return}e.insertAdjacentHTML("afterbegin",`\n<div class="keymap-playback-tip-container">\n<i class="mdi mdi-fast-forward"></i>\n<div class="keymap-playback-tip"></div>x\n</div>\n`);t.applyStyleFromText(`\n .keymap-playback-tip-container {\n position: absolute;\n left: 50%;\n top: 50%;\n transform: translate(-50%, -50%);\n padding: 8px 16px;\n background-color: #000A;\n color: white;\n pointer-events: none;\n opacity: 0;\n z-index: 100;\n display: flex;\n align-items: center;\n font-size: 14pt;\n border-radius: 4px;\n transition: .2s ease-out;\n }\n .keymap-playback-tip-container.show {\n opacity: 1;\n }\n .keymap-playback-tip-container i {\n line-height: 1;\n margin-right: 8px;\n font-size: 18pt;\n }\n`,"keymapStyle");i=dq(".keymap-playback-tip")}i.innerHTML=e.toString();if(a){clearTimeout(a)}dq(".keymap-playback-tip-container").classList.add("show");a=setTimeout(()=>{dq(".keymap-playback-tip-container").classList.remove("show")},2e3)};document.body.addEventListener("keydown",t=>{if(document.activeElement&&["input","textarea"].includes(document.activeElement.nodeName.toLowerCase())){return}const a=t.key.toLowerCase();const l=!t.shiftKey&&!t.altKey&&!t.ctrlKey&&!t.metaKey;if(a in i&&l){const e=dq(i[a]);if(!e){return}t.stopPropagation();t.preventDefault();e.click()}else if(a==="d"&&l){const e=dq(".bilibili-player-video-danmaku-switch input");if(!e){return}t.stopPropagation();t.preventDefault();e.checked=!e.checked;raiseEvent(e,"change")}else if(a==="j"&&l){const i=dq(".bilibili-player-video video");i.currentTime+=e.keymapJumpSeconds;t.stopPropagation();t.preventDefault()}else if(a==="l"&&l){const e=dq(".video-toolbar .like");t.preventDefault();const i=(t,i)=>{const n=new CustomEvent(t,i);e.dispatchEvent(n)};n=true;setTimeout(()=>n=false,200);i("mousedown",t);document.body.addEventListener("keyup",e=>{e.preventDefault();i("mouseup",e);if(n){i("click",e)}},{once:true})}else if(a==="`"&&l){const e=dq(".bilibili-player-video-wrap");const t=e.getBoundingClientRect();e.dispatchEvent(new MouseEvent("contextmenu",{bubbles:true,cancelable:false,view:unsafeWindow,button:2,buttons:0,clientX:t.x+t.width/2-386.6/2,clientY:t.y+t.height/2-311/2}))}else if(t.shiftKey){const i=dq(".bilibili-player-video video");if(i===null){return}const n=[.5,.75,1,1.25,1.5,2,2.5,3];let l=true;if(a===">"||a==="ArrowUp".toLowerCase()){i.playbackRate=n.find(e=>e>i.playbackRate)||n[n.length-1];o(i.playbackRate)}else if(a==="<"||a==="ArrowDown".toLowerCase()){i.playbackRate=[...n].reverse().find(e=>e<i.playbackRate)||n[0];o(i.playbackRate)}else if(a==="?"){i.playbackRate=1;o(i.playbackRate)}else if(a==="w"){const e=dq(".video-toolbar .ops .watchlater,.more-ops-list .ops-watch-later");if(e!==null){e.click()}}else if(a==="j"){i.currentTime-=e.keymapJumpSeconds}else{l=false}if(l){t.stopPropagation();t.preventDefault()}}})}}})();