三相之力指示器 - 改

随便写点东西

  1. // ==UserScript==
  2. // @name 三相之力指示器 - 改
  3. // @namespace KAJIYAKISCRIPT
  4. // @version 1.0
  5. // @description 随便写点东西
  6. // @author kajiyaki
  7. // @match https://www.bilibili.com/video/*
  8. // @match https://t.bilibili.com/*
  9. // @match https://space.bilibili.com/*
  10. // @match https://space.bilibili.com/*
  11. // @match https://www.bilibili.com/read/*
  12. // @match https://www.bilibili.com/bangumi/play/*
  13. // @icon https://static.hdslb.com/images/favicon.ico
  14. // @connect bilibili.com
  15. // @grant GM_xmlhttpRequest
  16. // @license MIT
  17. // @run-at document-end
  18. // ==/UserScript==
  19. let bShow = true;
  20. const blog = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space?&host_mid=';
  21. //未测试过旧版环境,懒得测
  22. let is_new = true;
  23. //适配番剧页面
  24. let is_bangumi = false;
  25. let userlist = [];
  26. let resloved_userList = [];
  27. let commentBar = null;
  28. //在此添加标签即可.当然,还是关键字判断,只能图个乐(我是用标签作为关键字判断,也可在check_data中修改判断逻辑)
  29. const tag_text_list = ["原神","明日方舟","王者荣耀","嘉然","塔菲","雪蓮","七海","猫雷"];
  30. (
  31. function () {
  32. 'use strict';
  33. console.log("enter")
  34. addEventListener("load",function(){
  35. is_new = document.getElementsByClassName('item goback').length == 0; // 检测是不是新版
  36. is_bangumi = document.location.href.includes("/bangumi/play/");
  37. })
  38. let jiance = setInterval(()=>{
  39. if(!bShow) return;
  40. get_comment_list()
  41. for(let user of userlist){
  42. let pid = get_pid(user)
  43. let blogurl = blog + pid
  44. GM_xmlhttpRequest({
  45. method:"get",
  46. url: blogurl,
  47. data: '',
  48. headers:{
  49. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
  50. },
  51. onload: (res)=>{
  52. if(res.status == 200){
  53. let content = JSON.stringify(JSON.parse(res.response).data)
  54. if(content == null) return;
  55. let tags = check_data(user, content)
  56. let newData = {user:user,tags:tags}
  57. update_resolve(newData)
  58. }
  59. }
  60. })
  61. }
  62. }, 4000)
  63. }
  64. )();
  65. function get_pid(c){
  66. if(is_bangumi){
  67. return c.dataset["usercardMid"]
  68. }
  69. if(is_new){
  70. return c.dataset["userId"]
  71. }
  72. else{
  73. return c.children[0]["href"].replace(/[^\d]/g,"")
  74. }
  75. }
  76. function get_comment_list(){
  77. userlist = []
  78. resloved_userList = []
  79. if(is_new){
  80. for (let c of document.getElementsByClassName('user-name')){
  81. userlist.push(c)
  82. }
  83. for (let c of document.getElementsByClassName(('sub-user-name'))){
  84. userlist.push(c)
  85. }
  86. }else{
  87. for (let c of document.getElementsByClassName('user')){
  88. userlist.push(c)
  89. }
  90. }
  91. if(is_bangumi){
  92. for (let c of document.getElementsByClassName('name')){
  93. userlist.push(c)
  94. }
  95. }
  96. }
  97. function add_tag(comment, tag, bkGround){
  98. var bkg = bkGround || "#11DD77"
  99. let tag_inner = "<b style= 'color:" + bkg + "; border:1px; padding:3px; margin: 3px; background:#dddddd; border-radius:10px; '>" + tag + "</b>";
  100. if(comment.textContent.includes(tag) === false)
  101. comment.innerHTML += tag_inner
  102. }
  103. function check_data(user, data){
  104. var taglist = [];
  105. for(let i = 0 ;i < tag_text_list.length; i++){
  106. //归一化RGB中的G值
  107. var color_green_lowlimit = 50
  108. var color_green = Math.floor(i / tag_text_list.length * (256 - color_green_lowlimit)) + color_green_lowlimit
  109. if(data.includes(tag_text_list[i])){
  110. taglist.push({tag:tag_text_list[i], user:user})
  111. add_tag(user, tag_text_list[i], "rgb(" + 0 + "," + color_green + "," + 0 + ")")
  112. }
  113. }
  114. // if(data.includes(tag_text_yuanshen)){
  115. // taglist.push({tag:tag_text_yuanshen, user:user})
  116. // add_tag(user, tag_text_yuanshen, "#66CDAA")
  117. // }
  118. // if(data.includes(tag_text_fangzhou)){
  119. // taglist.push({tag:tag_text_fangzhou, user:user})
  120. // add_tag(user, tag_text_fangzhou, "#458B00")
  121. // }
  122. // if(data.includes(tag_text_wangzhe)){
  123. // taglist.push({tag:tag_text_wangzhe, user:user})
  124. // add_tag(user, tag_text_wangzhe, "#FFFF00")
  125. // }
  126. return taglist;
  127. }
  128. function update_resolve(newData){
  129. var taglist = []
  130. resloved_userList.push(newData)
  131. //取出所有用户的所有tag
  132. for(let user of resloved_userList){
  133. //取出一个用户的所有tag
  134. for(let tag of user.tags){
  135. taglist.push(tag)
  136. }
  137. }
  138. //将所有tag分组
  139. let groupData = getGroup(taglist,'tag')
  140. commentBar = document.getElementsByClassName("nav-bar")[0];
  141. if(is_bangumi){
  142. commentBar = document.getElementsByClassName("b-head")[0]
  143. }
  144. let customDiv = document.getElementsByClassName("nav-sort div custom")[0]
  145. if(customDiv == null){
  146. customDiv = document.createElement('div');
  147. customDiv.className = "nav-sort div custom"
  148. customDiv.setAttribute("style","border:1px; border-style:solid; border-color:black");
  149. commentBar.appendChild(customDiv)
  150. }
  151.  
  152. let liObj = document.getElementsByClassName("nav-sort total_number")[0]
  153. if(liObj == null){
  154. liObj = document.createElement('li');
  155. liObj.className = "nav-sort total_number";
  156. customDiv.appendChild(liObj);
  157. }
  158. liObj.innerText = "当前页面共" + resloved_userList.length + "个评论";
  159. //遍历分组后的对象,创建与Tag对应的Element
  160. Object.keys(groupData).forEach(key => {
  161. let tagObj = document.getElementsByClassName("nav-sort " + key)[0]
  162. if(tagObj == null){
  163. tagObj = document.createElement('li');
  164. tagObj.className = "nav-sort " + key;
  165. customDiv.appendChild(tagObj);
  166. }
  167. //获取该Tag组下包含多少个用户
  168. tagObj.innerText = groupData[key][0].tag + " " + groupData[key].length;
  169. })
  170. }
  171. ///////////////////
  172. //将List按照属性分组
  173. //data: List
  174. //key: 属性
  175. //references: https://blog.csdn.net/weixin_36339245/article/details/103522998
  176. //////////////////
  177. let getGroup=(data,key)=>{
  178. let groups={};
  179. data.forEach(c=>{
  180. let value=c[key];
  181. groups[value]=groups[value]||[];
  182. groups[value].push(c);
  183. });
  184. return groups;
  185. }