Bilibili-Evolved/min/medal-helper.min.js
2020-10-30 17:42:34 +08:00

1 line
5.8 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}const a=()=>{const e=document.URL.match(/live\.bilibili\.com\/(blanc\/)?(\d+)/);if(!e){return}const t=parseInt(e[2]);if(isNaN(t)){console.warn(`roomID not found`);return}return t};class s{constructor(e=false,t=0){this.isActive=e;this.id=t}static parseJson(e,t){const i=JSON.parse(e);if(i.code!==0){logError(`${t.errorMessage} 错误码:${i.code} ${i.message||""}`);return t.errorAction(i)}return t.successAction(i)}}class r extends s{constructor(e){const{medal_id:t,status:a,level:s,medalName:r,uname:n,roomid:c}=e;super(a===1,t);i(this,"level",void 0);i(this,"name",void 0);i(this,"upName",void 0);i(this,"roomID",void 0);this.level=s;this.name=r;this.upName=n;this.roomID=c}static async getList(){return s.parseJson(await Ajax.getTextWithCredentials("https://api.live.bilibili.com/i/api/medal?page=1&pageSize=256"),{successAction:e=>e.data.fansMedalList.map((e=>new r(e))),errorAction:()=>[],errorMessage:"无法获取勋章列表."})}static getContainer(){return document.querySelector("#medal-helper .medal-popup ul")}getItemTemplate(e){if(!e){e=this}return`\n<li data-id='${e.id}' ${e.isActive?"class='active'":""}>\n<label title='${e.upName}'>\n<input name='medal' type='radio' ${e.isActive?"checked":""}>\n<div class='fans-medal-item level-${e.level}'>\n<span class='label'>${e.name}</span>\n<span class='level'>${e.level}</span>\n</div>\n</label>\n</li>`}async activate(){if(this.isActive){return true}return s.parseJson(await Ajax.postTextWithCredentials(`https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/wear`,formData({medal_id:this.id,csrf_token:getCsrf(),csrf:getCsrf()})),{successAction:()=>{this.isActive=true;return true},errorAction:()=>false,errorMessage:"佩戴勋章失败."})}async deactivate(){if(!this.isActive){return true}return s.parseJson(await Ajax.postTextWithCredentials(`https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/take_off`,formData({csrf_token:getCsrf(),csrf:getCsrf()})),{successAction:()=>{this.isActive=false;return true},errorAction:()=>false,errorMessage:"卸下勋章失败."})}}class n extends s{constructor(e){const{id:t,cid:a,wear:s,css:r,name:c,source:l}=e;super(s,r);i(this,"tid",void 0);i(this,"cid",void 0);i(this,"name",void 0);i(this,"source",void 0);i(this,"imageUrl",void 0);this.tid=t;this.cid=a;this.name=c;this.source=l;n.getImageMap().then((e=>{this.imageUrl=e[this.id]}))}static async getImageMap(){if(n.imageMap===undefined){return s.parseJson(await Ajax.getTextWithCredentials("https://api.live.bilibili.com/rc/v1/Title/webTitles"),{successAction(e){n.imageMap={};e.data.forEach((e=>{n.imageMap[e.identification]=e.web_pic_url}));return n.imageMap},errorAction:()=>({}),errorMessage:"获取头衔图片失败."})}else{return n.imageMap}}static async getList(){return s.parseJson(await Ajax.getTextWithCredentials("https://api.live.bilibili.com/i/api/ajaxTitleInfo?page=1&pageSize=256&had=1"),{successAction:e=>e.data.list.map((e=>new n(e))),errorAction:()=>[],errorMessage:"无法获取头衔列表."})}static getContainer(){return document.querySelector("#title-helper .medal-popup ul")}getItemTemplate(e){if(!e){e=this}return`\n<li data-id='${e.id}' ${e.isActive?"class='active'":""}>\n<label title='${e.name}'>\n<input name='medal' type='radio' ${e.isActive?"checked":""}>\n<img src='${e.imageUrl}' class="title-image">\n</label>\n</li>`}async activate(){if(this.isActive){return true}return s.parseJson(await Ajax.postTextWithCredentials(`https://api.live.bilibili.com/i/ajaxWearTitle`,`id=${this.tid}&cid=${this.cid}&csrf=${getCsrf()}&csrf_token=${getCsrf()}`),{successAction:()=>{this.isActive=true;return true},errorAction:()=>false,errorMessage:"佩戴头衔失败."})}async deactivate(){if(!this.isActive){return true}return s.parseJson(await Ajax.postTextWithCredentials(`https://api.live.bilibili.com/i/ajaxCancelWearTitle`,`csrf=${getCsrf()}&csrf_token=${getCsrf()}`),{successAction:()=>{this.isActive=false;return true},errorAction:()=>false,errorMessage:"卸下头衔失败."})}}i(n,"imageMap",void 0);async function c(t,i){const a=t();const s=await i();const n=async()=>{const e=await i();e.forEach((e=>{const t=a.querySelector(`li[data-id='${e.id}']`);if(e.isActive){t.classList.add("active")}else{t.classList.remove("active")}t.querySelector(`input`).checked=e.isActive}))};s.forEach((t=>{const i=t.getItemTemplate();a.insertAdjacentHTML("beforeend",i);const c=a.querySelector(`li[data-id='${t.id}']`);const l=c.querySelector(`input`);c.addEventListener("click",(i=>{if(i.target===l){return}if(t.isActive){t.deactivate().then(n)}else{const i=s.find((e=>e.isActive));if(i){i.isActive=false}t.activate().then((()=>{if(t instanceof r){e.defaultMedalID=t.id}})).then(n)}}))}))}if(e.autoMatchMedal){(async()=>{const t=document.URL.match(/live\.bilibili\.com\/(\d+)/);if(!t){return}const i=parseInt(t[1]);if(isNaN(i)){console.warn(`roomID not found`);return}const a=await r.getList();if(!e.defaultMedalID){const t=a.find((e=>e.isActive));if(t){e.defaultMedalID=t.id;console.log(`set defaultMedalID to activeMedal (${t.id})`)}}const s=e.defaultMedalID?a.find((t=>t.id===e.defaultMedalID)):a.find((e=>e.isActive));const n=a.find((e=>e.roomID===i));if(!n){if(s){await s.activate();console.log(`no matchMedal, fallback to defaultMedal (${s.id})`)}}else{await n.activate();console.log(`activated matchMedal (${n.id})`)}})()}return{export:{Badge:s,Medal:r,Title:n},widget:{condition:()=>document.URL.startsWith("https://live.bilibili.com")&&Boolean(getUID()),content:t.import("medalHelperHtml"),success:async()=>{t.applyImportantStyle("medalHelperStyle");document.querySelectorAll(".medal-helper").forEach((e=>{const t=e.querySelector(".medal-popup");e.addEventListener("click",(e=>{if(!t.contains(e.target)){t.classList.toggle("opened")}}))}));c(r.getContainer,r.getList);await n.getImageMap();c(n.getContainer,n.getList)}}}})();