Bilibili-Evolved/min/activity-apis.min.js
2021-10-26 13:22:15 +08:00

1 line
1.5 KiB
JavaScript

(()=>{return(t,e)=>{class s extends EventTarget{constructor(){super(...arguments);this.cards=[]}addCard(t){if(t instanceof Element&&t.classList.contains("card")){if(t.querySelector(".skeleton")!==null){const e=Observer.childList(t,()=>{if(t.querySelector(".skeleton")===null){e.forEach(t=>t.stop());this.addCard(t)}})}else{const e=this.parseCard(t);this.cards.push(e);const s=new CustomEvent("addCard",{detail:e});this.dispatchEvent(s)}}}removeCard(t){if(t instanceof Element&&t.classList.contains("card")){const e=this.parseCard(t).id;const s=this.cards.findIndex(t=>t.id===e);const r=this.cards[s];this.cards.splice(s,1);const n=new CustomEvent("removeCard",{detail:r});this.dispatchEvent(n)}}parseCard(t){const e=e=>{if(t.querySelector(e)===null){return""}return t.querySelector(e).innerText};const s=t=>{const s=parseInt(e(t));if(isNaN(s)){return 0}return s};const r={id:t.getAttribute("data-did"),username:e(".main-content .user-name"),text:e(".card-content .text.description"),reposts:s(".button-bar .single-button:nth-child(1) .text-offset"),comments:s(".button-bar .single-button:nth-child(2) .text-offset"),likes:s(".button-bar .single-button:nth-child(3) .text-offset")};return r}async startWatching(){const t=await SpinQuery.select(".card-list .content");if(!t){return false}const e=[...t.querySelectorAll(".content>.card")];e.forEach(t=>this.addCard(t));Observer.childList(t,t=>{t.forEach(t=>{t.addedNodes.forEach(t=>this.addCard(t));t.removedNodes.forEach(t=>this.removeCard(t))})});return true}}const r=new s;return{export:{activityCardsManager:r}}}})();