mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
6.8 KiB
JavaScript
1 line
6.8 KiB
JavaScript
(()=>{return(e,t)=>{const n={repost:{id:1,name:"转发"},textWithImages:{id:2,name:"图文"},text:{id:4,name:"文字"},video:{id:8,name:"视频"},miniVideo:{id:16,name:"小视频"},column:{id:64,name:"专栏"},audio:{id:256,name:"音频"},bangumi:{id:512,name:"番剧"},share:{id:2048,name:"分享"},manga:{id:2049,name:"漫画"},film:{id:4098,name:"电影"},tv:{id:4099,name:"TV剧"},chinese:{id:4100,name:"国创"},documentary:{id:4101,name:"纪录片"},mediaList:{id:4300,name:"收藏夹"},liveRecord:{id:2047,name:"开播记录"}};const i=e=>{if(e.querySelector(".repost")){return n.repost}if(e.querySelector(".imagesbox")){return n.textWithImages}if(e.querySelector(".video-container")){return n.video}if(e.querySelector(".bangumi-container")){return n.bangumi}if(e.querySelector(".article-container")){return n.column}if(e.querySelector(".music-container")){return n.audio}if(e.querySelector(".h5share-container")){return n.share}if(e.querySelector(".vc-ctnr")){return n.miniVideo}if(e.querySelector(".live-container")){return n.liveRecord}return n.text};const r=[];class a extends EventTarget{constructor(){super(...arguments);this.watching=false;this.cards=[]}addEventListener(e,t,n){super.addEventListener(e,t,n)}removeEventListener(e,t,n){super.removeEventListener(e,t,n)}async addCard(e){if(e instanceof HTMLElement&&e.classList.contains("card")){if(e.querySelector(".skeleton")!==null){const t=Observer.childList(e,()=>{if(e.querySelector(".skeleton")===null){t.stop();this.addCard(e)}})}else{if(e.parentNode===null){return}const t=await this.parseCard(e);if(!t.presented){return}if(this.cards.find(e=>e.id===t.id)){return}this.cards.push(t);this.cards.sort((e,t)=>{if(e.id===t.id){return 0}return e.id>t.id?-1:1});const n=new CustomEvent("addCard",{detail:t});this.dispatchEvent(n);r.forEach(e=>e.added(t))}}}async removeCard(e){if(e instanceof HTMLElement&&e.classList.contains("card")){const t=(await this.parseCard(e)).id;const n=this.cards.findIndex(e=>e.id===t);if(n===-1){return}const i=this.cards[n];this.cards.splice(n,1);const a=new CustomEvent("removeCard",{detail:i});this.dispatchEvent(a);r.forEach(e=>e.removed(i))}}async parseCard(e){const t=async t=>{const n=await SpinQuery.condition(()=>e.querySelector(t),t=>t!==null||e.parentNode===null);if(e.parentNode===null){return""}if(n===null){console.warn(e,t,e.parentNode);return""}const i=n.innerText.trim();return i};const r=async t=>{if(t===n.bangumi){return""}const i=await SpinQuery.condition(()=>e,t=>Boolean(t.__vue__||!e.parentNode));if(e.parentNode===null){return""}if(i===null){console.warn(i);return""}if(t===n.repost){const e=JSON.parse(i.__vue__.card.origin);const t=i.__vue__.originCardData.pureText;const n=_.get(e,"item.description","");const r=e.title;const a=i.__vue__.card.item.content;return[a,t,n,r].filter(e=>Boolean(e)).join("\n")}const r=i.__vue__.originCardData.pureText;const a=i.__vue__.originCardData.title;return[r,a].filter(e=>Boolean(e)).join("\n")};const a=async e=>{const n=parseInt(await t(e));if(isNaN(n)){return 0}return n};const s={id:e.getAttribute("data-did"),username:await t(".main-content .user-name"),text:"",reposts:await a(".button-bar .single-button:nth-child(1) .text-offset"),comments:await a(".button-bar .single-button:nth-child(2) .text-offset"),likes:await a(".button-bar .single-button:nth-child(3) .text-offset"),element:e,type:i(e),presented:true,async getText(){const e=await r(this.type);this.text=e;return e}};await s.getText();s.presented=e.parentNode!==null;e.setAttribute("data-type",s.type.id.toString());if(s.type===n.repost){const t=s.username;const n=_.get(s,"element.__vue__.card.origin_user.info.uname","");if(t===n){e.setAttribute("data-self-repost","true")}}return s}async startWatching(){const e=e=>{const t=[...e.querySelectorAll(".card[data-did]")];t.forEach(e=>this.addCard(e));return Observer.childList(e,e=>{e.forEach(e=>{e.addedNodes.forEach(e=>this.addCard(e));e.removedNodes.forEach(e=>this.removeCard(e))})})};if(this.watching){return true}this.watching=true;if(document.URL.includes("//space.bilibili.com")){console.log("space watch");const t=await SpinQuery.select(".s-space");if(!t){return false}let n=null;Observer.childList(t,async()=>{if(dq("#page-dynamic")){const t=await SpinQuery.select(".feed-card .content");console.log("enter feeds tab");if(n){n.stop()}n=e(t)}else{console.log("leave feeds tab");if(n){n.stop();n=null}}});this.watching=true;return true}const t=await SpinQuery.select(".feed-card .content, .detail-content .detail-card");if(!t){return false}e(t);return true}}const s=new a;const o=async(e="video")=>{if(!getUID()){return[]}const t=await Ajax.getJsonWithCredentials(`https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new?uid=${getUID()}&type_list=${e==="video"?8:512}`);if(t.code!==0){throw new Error(t.message)}if(e==="video"){return _.uniqBy(t.data.cards.map(e=>{const t=JSON.parse(e.card);const n=_.get(e,"display.topic_info.topic_details",[]).map(e=>{return{id:e.topic_id,name:e.topic_name}});return{id:e.desc.dynamic_id_str,aid:t.aid,bvid:e.desc.bvid||t.bvid,title:t.title,upID:e.desc.user_profile.info.uid,upName:e.desc.user_profile.info.uname,upFaceUrl:e.desc.user_profile.info.face,coverUrl:t.pic,description:t.desc,timestamp:e.timestamp,time:new Date(e.timestamp*1e3),topics:n,dynamic:t.dynamic,like:formatCount(e.desc.like),duration:t.duration,durationText:formatDuration(t.duration,0),playCount:formatCount(t.stat.view),danmakuCount:formatCount(t.stat.danmaku),watchlater:store.state.watchlaterList.includes(t.aid)}}),e=>e.aid)}else if(e==="bangumi"){return t.data.cards.map(e=>{const t=JSON.parse(e.card);return{id:e.desc.dynamic_id_str,aid:t.aid,bvid:e.desc.bvid||t.bvid,epID:t.episode_id,title:t.new_desc,upName:t.apiSeasonInfo.title,upFaceUrl:t.apiSeasonInfo.cover,coverUrl:t.cover,description:"",timestamp:e.timestamp,time:new Date(e.timestamp*1e3),like:formatCount(e.desc.like),durationText:"",playCount:formatCount(t.play_count),danmakuCount:formatCount(t.bullet_count),watchlater:false}})}else{return[]}};const d=e=>{const t=[/^https:\/\/t\.bilibili\.com\/$/,/^https:\/\/space\.bilibili\.com\//,/^https:\/\/live\.bilibili\.com\/(blanc\/)?[\d]+/,/^https:\/\/t\.bilibili\.com\//];if(t.every(e=>!e.test(document.URL))){return}(async()=>{const t=await s.startWatching();if(!t){console.error("feedsCardsManager.startWatching() failed");return}const{added:n}=e;if(n){s.cards.forEach(e=>n(e))}const i=()=>{};r.push({added:i,removed:i,...e})})()};const c=(e,t)=>{const n=dq(e.element,".more-panel");const{className:i,text:r,action:a}=t;if(!n||dq(n,`.${i}`)){return}const s=document.createElement("p");s.classList.add("child-button","c-pointer",i);s.textContent=r;const o=[...new Set([...n.children].map(e=>{return e.getAttributeNames().filter(e=>e.startsWith("data-v-"))}).flat())];o.forEach(e=>s.setAttribute(e,""));s.addEventListener("click",t=>{a(t);e.element.click()});n.appendChild(s)};return{export:{feedsCardsManager:s,feedsCardTypes:n,getVideoFeeds:o,forEachFeedsCard:d,addMenuItem:c}}}})(); |