Bilibili-Evolved/min/feeds-apis.min.js
2020-08-08 14:01:37 +08:00

1 line
6.1 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:"收藏夹"}};const r=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}return n.text};const i=[];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);i.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 r=this.cards[n];this.cards.splice(n,1);const a=new CustomEvent("removeCard",{detail:r});this.dispatchEvent(a);i.forEach(e=>e.removed(r))}}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 r=n.innerText.trim();return r};const i=async t=>{if(t===n.bangumi){return""}const r=await SpinQuery.condition(()=>e,t=>Boolean(t.__vue__||!e.parentNode));if(e.parentNode===null){return""}if(r===null){console.warn(r);return""}if(t===n.repost){const e=JSON.parse(r.__vue__.card.origin);const t=r.__vue__.originCardData.pureText;const n=_.get(e,"item.description","");const i=e.title;const a=r.__vue__.card.item.content;return[a,t,n,i].filter(e=>Boolean(e)).join("\n")}const i=r.__vue__.originCardData.pureText;const a=r.__vue__.originCardData.title;return[i,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 i(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=>{(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 r=()=>{};i.push({added:r,removed:r,...e})})()};return{export:{feedsCardsManager:s,feedsCardTypes:n,getVideoFeeds:o,forEachFeedsCard:d}}}})();