Fix message api (fix #2171)

This commit is contained in:
the1812 2021-08-21 21:04:29 +08:00
parent 7bfdaa57ca
commit 4b31cdae60
2 changed files with 5 additions and 4 deletions

View File

@ -1 +1 @@
(()=>(t,e)=>{function a(t,e,a){if(e in t){Object.defineProperty(t,e,{value:a,enumerable:true,configurable:true,writable:true})}else{t[e]=a}return t}const{NavbarComponent:s}=e.import("custom-navbar-component");class i extends s{constructor(){super();a(this,"totalCount",void 0);a(this,"settings",void 0);this.href="https://message.bilibili.com/";this.html="消息";this.popupHtml=`\n<ul id="message-list">\n<li><a data-name="reply" target="_blank" href="https://message.bilibili.com/#/reply">回复我的</a></li>\n<li><a data-name="at" target="_blank" href="https://message.bilibili.com/#/at">@我的</a></li>\n<li><a data-name="like" target="_blank" href="https://message.bilibili.com/#/love">收到的赞</a></li>\n<li><a data-name="user_msg" target="_blank" href="https://message.bilibili.com/#/whisper">我的消息</a></li>\n<li><a data-name="sys_msg" target="_blank" href="https://message.bilibili.com/#/system">系统通知</a></li>\n</ul>\n`;this.requestedPopup=true;this.active=document.URL.startsWith("https://message.bilibili.com/");this.fetchSettings().then((t=>{if(t){this.updateCount();this.setupEvents();this.onPopup=()=>this.updateCount()}}))}get name(){return"messages"}async fetchSettings(){const t=await Ajax.getJsonWithCredentials(`https://api.vc.bilibili.com/link_setting/v1/link_setting/get?msg_notify=1&show_unfollowed_msg=1`);if(t.code!==0){return}await this.setNotifyStyle(t.data.msg_notify);this.settings={notify:t.data.msg_notify!==3,hideNotFollowedCount:t.data.show_unfollowed_msg===1,json:t.data};return t.data.msg_notify!==3}async setupEvents(){const t=await SpinQuery.select("#message-list");const e=[...t.querySelectorAll("a[data-name]")];e.forEach((t=>{t.addEventListener("click",(()=>{const e=t.getAttribute("data-count");if(!e){return}const a=parseInt(e);t.removeAttribute("data-count");this.totalCount-=a;if(this.totalCount<0){this.totalCount=0}this.setNotifyCount(this.totalCount)}))}))}async updateCount(){const t=await Ajax.getJsonWithCredentials(`https://api.bilibili.com/x/msgfeed/unread`);const e=await Ajax.getJsonWithCredentials(`https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread`);const a=await SpinQuery.select("#message-list");const s=[...a.querySelectorAll("a[data-name]")];const i=s.map((t=>t.getAttribute("data-name")));if(t.code!==0||e.code!==0){return}t.data["user_msg"]=e.data.follow_unread;if(!this.settings.hideNotFollowedCount){t.data["user_msg"]+=e.data.unfollow_unread}this.totalCount=i.reduce(((e,a)=>e+t.data[a]),0);if(!this.totalCount){return}await this.setNotifyCount(this.totalCount);i.forEach(((e,a)=>{const i=t.data[e];if(i>0){s[a].setAttribute("data-count",i.toString())}else{s[a].removeAttribute("data-count")}}))}}return{export:{Messages:i}}})();
(()=>(t,e)=>{function s(t,e,s){if(e in t){Object.defineProperty(t,e,{value:s,enumerable:true,configurable:true,writable:true})}else{t[e]=s}return t}const{NavbarComponent:i}=e.import("custom-navbar-component");class a extends i{constructor(){super();s(this,"totalCount",void 0);s(this,"settings",void 0);this.href="https://message.bilibili.com/";this.html="消息";this.popupHtml=`\n<ul id="message-list">\n<li><a data-name="reply" target="_blank" href="https://message.bilibili.com/#/reply">回复我的</a></li>\n<li><a data-name="at" target="_blank" href="https://message.bilibili.com/#/at">@我的</a></li>\n<li><a data-name="like" target="_blank" href="https://message.bilibili.com/#/love">收到的赞</a></li>\n<li><a data-name="user_msg" target="_blank" href="https://message.bilibili.com/#/whisper">我的消息</a></li>\n<li><a data-name="sys_msg" target="_blank" href="https://message.bilibili.com/#/system">系统通知</a></li>\n</ul>\n`;this.requestedPopup=true;this.active=document.URL.startsWith("https://message.bilibili.com/");this.fetchSettings().then((t=>{if(t){this.updateCount();this.setupEvents();this.onPopup=()=>this.updateCount()}}))}get name(){return"messages"}async fetchSettings(){const t=await Ajax.getJsonWithCredentials(`https://api.vc.bilibili.com/link_setting/v1/link_setting/get?msg_notify=1&show_unfollowed_msg=1`);if(t.code!==0){return}await this.setNotifyStyle(t.data.msg_notify);this.settings={notify:t.data.msg_notify!==3,hideNotFollowedCount:t.data.show_unfollowed_msg===1,json:t.data};console.log(this.settings);return t.data.msg_notify!==3}async setupEvents(){const t=await SpinQuery.select("#message-list");const e=[...t.querySelectorAll("a[data-name]")];e.forEach((t=>{t.addEventListener("click",(()=>{const e=t.getAttribute("data-count");if(!e){return}const s=parseInt(e);t.removeAttribute("data-count");this.totalCount-=s;if(this.totalCount<0){this.totalCount=0}this.setNotifyCount(this.totalCount)}))}))}async updateCount(){const t=await Ajax.getJsonWithCredentials(`https://api.bilibili.com/x/msgfeed/unread`);const e=await Ajax.getJsonWithCredentials(`https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread`);const s=await SpinQuery.select("#message-list");const i=[...s.querySelectorAll("a[data-name]")];const a=i.map((t=>t.getAttribute("data-name")));if(t.code!==0||e.code!==0){return}t.data["user_msg"]=e.data.follow_unread||0;if(!this.settings.hideNotFollowedCount){t.data["user_msg"]+=e.data.unfollow_unread||0}this.totalCount=a.reduce(((e,s)=>e+(t.data[s]||0)),0);if(!this.totalCount){return}await this.setNotifyCount(this.totalCount);a.forEach(((e,s)=>{const a=t.data[e];if(a>0){i[s].setAttribute("data-count",a.toString())}else{i[s].removeAttribute("data-count")}}))}}return{export:{Messages:a}}})();

View File

@ -43,6 +43,7 @@ export class Messages extends NavbarComponent {
hideNotFollowedCount: json.data.show_unfollowed_msg === 1,
json: json.data,
}
console.log(this.settings)
return json.data.msg_notify !== 3
}
async setupEvents() {
@ -74,11 +75,11 @@ export class Messages extends NavbarComponent {
if (mainJson.code !== 0 || messageJson.code !== 0) {
return
}
mainJson.data['user_msg'] = messageJson.data.follow_unread
mainJson.data['user_msg'] = messageJson.data.follow_unread || 0
if (!this.settings.hideNotFollowedCount) {
mainJson.data['user_msg'] += messageJson.data.unfollow_unread
mainJson.data['user_msg'] += messageJson.data.unfollow_unread || 0
}
this.totalCount = names.reduce((acc, it) => acc + mainJson.data[it], 0)
this.totalCount = names.reduce((acc, it) => acc + (mainJson.data[it] || 0), 0)
if (!this.totalCount) {
return
}