[Bilibili] UP主备注别名

为UP主进行别名备注!

Встановити цей скрипт?
Скрипт запропонований Автором

Вам також може сподобатись [Bilibili] 关注管理器.

Встановити цей скрипт
  1. // ==UserScript==
  2. // @name [Bilibili] UP主备注别名
  3. // @namespace ckylin-script-bili-foman-plugins-up-alias
  4. // @version 0.2
  5. // @description 为UP主进行别名备注!
  6. // @author CKylinMC
  7. // @match https://www.bilibili.com/video/*
  8. // @match https://space.bilibili.com/*
  9. // @require https://greasyfork.org/scripts/441653-ckuitoolkit/code/CKUIToolkit.js?version=1034229
  10. // @grant GM_deleteValue
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @grant GM_listValues
  14. // @grant GM_registerMenuCommand
  15. // @grant unsafeWindow
  16. // @license GPLv3
  17. // ==/UserScript==
  18.  
  19. (function () {
  20.  
  21. class UpAlias{
  22. static _k(uid){
  23. return "U_"+uid;
  24. }
  25. static hasAlias(uid){
  26. return GM_listValues().includes(this._k(uid));
  27. }
  28. static getAlias(uid, fallback=null){
  29. if(this.hasAlias(uid)) return GM_getValue(this._k(uid));
  30. return fallback;
  31. }
  32. static setAlias(uid,alias){
  33. GM_setValue(this._k(uid),alias);
  34. }
  35. static removeAlias(uid){
  36. GM_deleteValue(this._k(uid));
  37. }
  38. }
  39.  
  40. const MenuManager = {
  41. ids:[],
  42. menus:{},
  43. registerMenu: (text, callback) => MenuManager.ids.push(GM_registerMenuCommand(text, callback)),
  44. clearMenu: () => {MenuManager.ids.forEach(id => GM_unregisterMenuCommand(id)); MenuManager.ids = [];},
  45. setMenu:(id,text,callback,noapply = false)=>{
  46. MenuManager.menus[id] = { text, callback };
  47. if (!noapply) MenuManager.applyMenus();
  48. },
  49. applyMenus:()=>{
  50. MenuManager.clearMenu();
  51. for (let item in MenuManager.menus) {
  52. if(!MenuManager.menus.hasOwnProperty(item)) continue;
  53. let menu = MenuManager.menus[item];
  54. MenuManager.registerMenu(menu.text, menu.callback);
  55. }
  56. }
  57. };
  58.  
  59. class UI{
  60. static async prompt(title="Input",content="",placeholder="...",okbtn="OK"){
  61. if(typeof(FloatWindow)=='function'){
  62. return FloatWindow.prompt(title,content,placeholder,okbtn);
  63. }else{
  64. return new Promise(r=>{
  65. r(prompt(`${title}${content.length?`\n\n${content}`:''}`,placeholder));
  66. })
  67. }
  68. }
  69. static async alert(title="Alert",content="",okbtn="OK"){
  70. if(typeof(FloatWindow)=='function'){
  71. return FloatWindow.alert(title,content,okbtn);
  72. }else{
  73. return new Promise(()=>{
  74. alert(`${title}${content.length?`\n\n${content}`:''}`)
  75. })
  76. }
  77. }
  78. static async confirm(title="Confirm",content="",btnoktxt='yes',btnnotxt='no'){
  79. if(typeof(FloatWindow)=='function'){
  80. return FloatWindow.confirm(title,content,btnoktxt,btnnotxt);
  81. }else{
  82. return new Promise(r=>{
  83. r(confirm(`${title}${content.length?`\n\n${content}`:''}`));
  84. })
  85. }
  86. }
  87. }
  88.  
  89. class Actions{
  90. static getInfo(){
  91. if(location.host=='space.bilibili.com'){
  92. const uid = +/\d+/.exec(location.pathname.split('/')[1])[0];
  93. if(uid&&!isNaN(uid)){
  94. const name = document.querySelector("#h-name")?.trim()??uid+"";
  95. return {uid,name};
  96. }else return null;
  97. }
  98. if(location.host=='www.bilibili.com'){
  99. if(location.pathname.startsWith('/video/')){
  100. const uid = +/space\.bilibili\.com\/(?<id>\d+)(\/)*/.exec(document.querySelector("#v_upinfo .name a")?.href)?.groups?.id;
  101. if(uid&&!isNaN(uid)) {
  102. const name = document.querySelector("#v_upinfo .name a.username")?.innerText?.trim()??uid+"";
  103. return {uid,name};
  104. }else return null;
  105. }
  106. }
  107. }
  108. static setCurrent(){
  109. const info = this.getInfo();
  110. if(info)
  111. return this.setFor(info.uid,info.name);
  112. else return Promise.reject();
  113. }
  114. static removeCurrent(){
  115. const info = this.getInfo();
  116. if(info)
  117. return this.removeFor(info.uid,info.name);
  118. else return Promise.reject();
  119. }
  120. static async setFor(uid,displayName=null){
  121. uid = +uid;
  122. let alreadyTxt = "为其创建别名";
  123. if(UpAlias.hasAlias(uid)) alreadyTxt = `为其修改别名;原别名:${UpAlias.getAlias(uid,'不存在')}`;
  124. let alias = await UI.prompt(`为 ${displayName?displayName:uid} 设置别名`,alreadyTxt,"确定");
  125. if(alias&&alias.trim().length){
  126. UpAlias.setAlias(uid,alias.trim());
  127. UI.alert("设置成功!");
  128. }
  129. }
  130. static async removeFor(uid,displayName=null){
  131. uid = +uid;
  132. let alreadyTxt = "清空此用户的别名";
  133. if(UpAlias.hasAlias(uid)) alreadyTxt = `清空此用户的别名:${UpAlias.getAlias(uid,'不存在')}`;
  134. let result = await UI.confirm(`为 ${displayName?displayName:uid} 清空别名`,alreadyTxt,"确定清空","取消");
  135. if(result){
  136. UpAlias.removeAlias(uid);
  137. UI.alert("清空成功!");
  138. }
  139. }
  140. }
  141.  
  142. function addMenus(){
  143. const info = Actions.getInfo();
  144. if(!info) return;
  145. if(location.host=='space.bilibili.com'){
  146. MenuManager.registerMenu(`为${info.name}设置别名`,()=>{
  147. Actions.setCurrent();
  148. })
  149. MenuManager.registerMenu(`为${info.name}删除别名`,()=>{
  150. Actions.removeCurrent();
  151. })
  152. }
  153. if(location.host=='www.bilibili.com'){
  154. if(location.pathname.startsWith('/video/')){
  155. MenuManager.registerMenu(`为当前UP主设置别名`,()=>{
  156. Actions.setCurrent();
  157. })
  158. MenuManager.registerMenu(`为当前UP主删除别名`,()=>{
  159. Actions.removeCurrent();
  160. })
  161. }
  162. }
  163. }
  164. if(!unsafeWindow.FoManPlugins){
  165. unsafeWindow.FoManPlugins = {}
  166. }
  167. unsafeWindow.FoManPlugins.UpAlias = {provider:UpAlias,actions:Actions};
  168. addMenus();
  169. })();