Bilibili-Evolved/min/download-danmaku.min.js
2020-03-22 13:25:26 +08:00

1 line
2.8 KiB
JavaScript

(()=>{return(t,e)=>{const{getFriendlyTitle:n}=e.import("title");const{DanmakuInfo:i}=e.import("video-info");const{DanmakuConverter:a}=e.import("danmaku-converter");async function o(t){const e=n();let i={title:e};try{await loadDanmakuSettingsPanel();const t=t=>{const e=parseFloat(dq(t).style.transform.replace(/translateX\(([\d\.]+)/,"$1"));return e*4/188};const e=t=>{const e=parseFloat(dq(t).style.transform.replace(/translateX\(([\d\.]+)/,"$1"));const n={0:0,44:1,94:2,144:3,188:4}[e];return n};i.font=dq(".bilibili-player-video-danmaku-setting-right-font .bui-select-result").innerText;i.alpha=(100-parseFloat(dq(".bilibili-player-setting-opacity .bui-thumb-tooltip").innerText))/100;i.duration=(()=>{const e=18-3*t(".bilibili-player-setting-speedplus .bui-thumb");return t=>{switch(t.type){case 4:case 5:return 4;default:return e}}})();i.blockTypes=(()=>{let t=[];const e={".bilibili-player-block-filter-type[ftype=scroll]":[1,2,3],".bilibili-player-block-filter-type[ftype=top]":[5],".bilibili-player-block-filter-type[ftype=bottom]":[4],".bilibili-player-block-filter-type[ftype=color]":["color"]};for(const[n,i]of Object.entries(e)){if(dq(n).classList.contains("disabled")){t=t.concat(i)}}return t.concat(7,8)})();const n=1.4-.2*t(".bilibili-player-setting-fontsize .bui-thumb");i.resolution={x:1920*n,y:1080*n};i.bottomMarginPercent=[.75,.5,.25,0,0][e(".bilibili-player-setting-area .bui-thumb")];if(i.bottomMarginPercent===0&&dq(".bilibili-player-video-danmaku-setting-left-preventshade input").checked){i.bottomMarginPercent=.15}i.bold=dq(".bilibili-player-video-danmaku-setting-right-font-bold input").checked}catch(t){i={font:"微软雅黑",alpha:.4,duration:t=>{switch(t.type){case 4:case 5:return 4;default:return 6}},blockTypes:[7,8],resolution:{x:1920,y:1080},bottomMarginPercent:.15,bold:false}}const o=new a(i);const l=o.convertToAssDocument(t);return l.generateAss()}async function l(t){const e=n();const a=new i((unsafeWindow||window).cid);await a.fetchInfo();const l=await(async()=>{if(t===true){return new Blob([await o(a.rawXML)],{type:"text/plain"})}else{return new Blob([a.rawXML],{type:"text/plain"})}})();const r=URL.createObjectURL(l);const s=dq("#danmaku-link");const c=s.getAttribute("href");if(c){URL.revokeObjectURL(c)}s.setAttribute("download",`${e}.${t?"ass":"xml"}`);s.setAttribute("href",r);s.click()}return{export:{downloadDanmaku:l,convertToAss:o},widget:{content:`\n<button\n class="gui-settings-flat-button"\n id="download-danmaku">\n<i class="icon-danmaku"></i>\n<span>下载弹幕</span>\n<a id="danmaku-link" style="display:none"></a>\n</button>`,condition:async()=>{let t=await SpinQuery.select(()=>(unsafeWindow||window).cid);return Boolean(t)},success:()=>{const t=dq("#danmaku-link");const e=dq("#download-danmaku");e.addEventListener("click",n=>{if(n.target!==t){try{e.disabled=true;l(n.shiftKey)}catch(t){logError(t)}finally{e.disabled=false}}})}}}}})();