BDWM PLUS

BDWM_PLUS by motaguoke

目前为 2019-04-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name BDWM PLUS
  3. // @version 1.2
  4. // @description BDWM_PLUS by motaguoke
  5. // @include http://bbs.pku.edu.cn/*
  6. // @include https://bbs.pku.edu.cn/*
  7. // @include https://*.bdwm.net/*
  8. // @include http://*.bdwm.net/*
  9. // @namespace https://greasyfork.org/users/284856
  10. // ==/UserScript==
  11. //设置
  12. var GLOBAL_VERSION = "1.2"
  13. //全局变量
  14. var GLOBAL_SEEME_STATUS = false //当前只看某一作者状态
  15. var GLOBAL_SEEME_AUTHOR = "" //当前只看某一作者的作者ID
  16.  
  17. console.log(`BDWM_PLUS by motaguoke Version: ${GLOBAL_VERSION}`)
  18. window.onload = BDWM_ADDON_MAIN()
  19.  
  20.  
  21. async function BDWM_ADDON_MAIN(){
  22. //初始化插件
  23. var observe = new MutationObserver(function (mutations){
  24. //初始化渲染插件
  25. BDWM_ADDON_SEEME_RENDER() //只看作者插件
  26. BDWM_ADDON_SETTINGINFO() //插件信息插件
  27. })
  28. observe.observe(document.documentElement,{childList:true,subtree:true})
  29. }
  30.  
  31.  
  32. document.addEventListener('click',async function(event){
  33. //点击事件钩子
  34. //console.log(event.target.className+" "+event.target.id)
  35.  
  36. if (event.target.className == "only-seeme"){ // BDWM_ADDON_SEEME插件,只看某一作者
  37. if (event.target.innerText == "只看TA"){
  38. //进入只看作者状态
  39. event.target.innerText = "取消只看TA"
  40. GLOBAL_SEEME_AUTHOR = event.target.id
  41. GLOBAL_SEEME_STATUS = true
  42.  
  43. obj_onlyseeme = document.getElementsByClassName("only-seeme")
  44. for (i=0;i<obj_onlyseeme.length;i++){
  45. obj_onlyseeme[i].innerText = "取消只看TA"
  46. }
  47.  
  48. } else {
  49. //退出只看作者状态
  50. event.target.innerText = "只看TA"
  51. GLOBAL_SEEME_AUTHOR = ""
  52. GLOBAL_SEEME_STATUS = false
  53.  
  54. obj_onlyseeme = document.getElementsByClassName("only-seeme")
  55. for (i=0;i<obj_onlyseeme.length;i++){
  56. obj_onlyseeme[i].innerText = "只看TA"
  57. }
  58. }
  59. BDWM_ADDON_SEEME_RENDER() //点击后立刻重新渲染
  60. }else{
  61.  
  62. }
  63.  
  64. },true);
  65.  
  66.  
  67.  
  68. async function BDWM_ADDON_SEEME_RENDER(){
  69.  
  70. if (window.location.href.indexOf("post-read.php")<0){
  71. //当切换到非读贴内容时,立刻重新刷新状态,避免上次查看主楼的延续
  72. GLOBAL_SEEME_AUTHOR = ""
  73. GLOBAL_SEEME_STATUS = false
  74. }
  75.  
  76. obj_found = document.getElementsByClassName("only-seeme")
  77. if (obj_found.length==0){ //本页面第一次加载时,给每个帖子加按钮
  78. obj_functions = document.getElementsByClassName("functions")
  79.  
  80. for (i = 0;i < obj_functions.length; i++){
  81. obj_widebtn = obj_functions[i].getElementsByClassName("line wide-btn")[0]
  82. obj_newobj = document.createElement("a")
  83. obj_newobj.className = "only-seeme"
  84. if (GLOBAL_SEEME_STATUS == false){
  85. obj_newobj.innerText = "只看TA"} else
  86. {obj_newobj.innerText = "取消只看TA"}
  87.  
  88. if (obj_widebtn){
  89. //存在关注按钮,在关注按钮旁边添加即可
  90. str_username = obj_widebtn.getElementsByClassName("add-friend")[0].getAttribute("data-username")
  91. obj_newobj.id = str_username
  92. obj_widebtn.appendChild(obj_newobj)
  93. } else {
  94. //不存在关注按钮,需要首先获取的user-name,再新建一个widebtn类插入
  95. //得到username
  96. str_username = obj_functions[i].parentNode.getElementsByClassName("username")[0].getElementsByTagName("a")[0].innerText
  97. obj_newobj.id = str_username
  98. //插入widebtn
  99. obj_widebtn = document.createElement("div")
  100. obj_widebtn.className = "line wide-btn"
  101. obj_functions[i].appendChild(obj_widebtn)
  102. //重新获得插入的widebtn DOM
  103. obj_functions[i].lastChild.appendChild(obj_newobj)
  104.  
  105. }
  106.  
  107.  
  108. }
  109. }
  110.  
  111. if (GLOBAL_SEEME_STATUS==true){
  112. //只看作者状态
  113. obj_postcard = document.getElementsByClassName("post-card")
  114. for (i = 0;i < obj_postcard.length; i++){
  115. str_username = obj_postcard[i].getElementsByClassName("username")[0].getElementsByTagName("a")[0].innerText //关注后“关注”按钮将被隐藏
  116. if (str_username!=GLOBAL_SEEME_AUTHOR){
  117. obj_postcard[i].style.display = "none"
  118. }else{
  119. obj_postcard[i].style.display = "block"
  120. }
  121. }
  122.  
  123. }else{
  124. //非只看作者状态
  125. obj_postcard = document.getElementsByClassName("post-card")
  126. for (i = 0;i< obj_postcard.length; i++){
  127. obj_postcard[i].style.display = "block"
  128. }
  129. }
  130.  
  131. }
  132.  
  133. async function BDWM_ADDON_SETTINGINFO(){
  134. //增加插件信息
  135. obj_footer = document.getElementById("footer")
  136. if (document.getElementById("footer").innerHTML.indexOf("BDWM_PLUS")<0) {
  137.  
  138. obj_newobj = document.createElement("a")
  139. obj_newobj.style.color = "blue"
  140. obj_newobj.innerText = `BDWM_PLUS 版本 ${GLOBAL_VERSION} 作者 motaguoke 点击查看更新`
  141. obj_newobj.href = "https://greasyfork.org/zh-CN/scripts/381264-bdwm-plus"
  142. obj_newobj.target = "_blank"
  143. obj_footer.appendChild(obj_newobj)
  144.  
  145. obj_footer.appendChild(document.createElement("br"))
  146.  
  147. obj_newobj = document.createElement("a")
  148. obj_newobj.style.color = "blue"
  149. obj_newobj.innerText = `查看该脚本的github页面`
  150. obj_newobj.href = "https://github.com/wangjunyi2008/BDWM_PLUS"
  151. obj_newobj.target = "_blank"
  152.  
  153. obj_footer.appendChild(obj_newobj)
  154. }
  155. }