mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
7.0 KiB
JavaScript
1 line
7.0 KiB
JavaScript
(()=>(e,t)=>{function n(e,t,n){if(t in e){Object.defineProperty(e,t,{value:n,enumerable:true,configurable:true,writable:true})}else{e[t]=n}return e}const i={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 r=e=>{if(e.querySelector(".repost")){return i.repost}if(e.querySelector(".imagesbox")){return i.textWithImages}if(e.querySelector(".video-container")){return i.video}if(e.querySelector(".bangumi-container")){return i.bangumi}if(e.querySelector(".article-container")){return i.column}if(e.querySelector(".music-container")){return i.audio}if(e.querySelector(".h5share-container")){return i.share}if(e.querySelector(".vc-ctnr")){return i.miniVideo}if(e.querySelector(".live-container")){return i.liveRecord}return i.text};const a=[];class s extends EventTarget{constructor(...e){super(...e);n(this,"watching",false);n(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);a.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 r=new CustomEvent("removeCard",{detail:i});this.dispatchEvent(r);a.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 n=async t=>{if(t===i.bangumi){return""}const n=await SpinQuery.condition((()=>e),(t=>Boolean(t.__vue__||!e.parentNode)));if(e.parentNode===null){return""}if(n===null){console.warn(n);return""}if(t===i.repost){const e=JSON.parse(n.__vue__.card.origin);const t=n.__vue__.originCardData.pureText;const i=_.get(e,"item.description","");const r=e.title;const a=n.__vue__.card.item.content;return[a,t,i,r].filter((e=>Boolean(e))).join("\n")}const r=n.__vue__.originCardData.pureText;const a=n.__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:r(e),presented:true,async getText(){const e=await n(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===i.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 o=new s;const d=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=>({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 c=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 o.startWatching();if(!t){console.error("feedsCardsManager.startWatching() failed");return}const{added:n}=e;if(n){o.cards.forEach((e=>n(e)))}const i=()=>{};a.push({added:i,removed:i,...e})})()};const u=(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=>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:o,feedsCardTypes:i,getVideoFeeds:d,forEachFeedsCard:c,addMenuItem:u}}})(); |