BDWM PLUS

BDWM_PLUS by motaguoke

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

  1. // ==UserScript==
  2. // @name BDWM PLUS
  3. // @version 1.0
  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.0"
  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. })
  27. observe.observe(document.documentElement,{childList:true,subtree:true})
  28. }
  29.  
  30.  
  31. document.addEventListener('click',async function(event){
  32. //点击事件钩子
  33. console.log(event.target.className+" "+event.target.id)
  34.  
  35. if (event.target.className == "only-seeme"){ // BDWM_ADDON_SEEME插件,只看某一作者
  36. if (event.target.innerText == "只看TA"){
  37. //进入只看作者状态
  38. event.target.innerText = "取消只看TA"
  39. GLOBAL_SEEME_AUTHOR = event.target.id
  40. GLOBAL_SEEME_STATUS = true
  41.  
  42. obj_onlyseeme = document.getElementsByClassName("only-seeme")
  43. for (i=0;i<obj_onlyseeme.length;i++){
  44. obj_onlyseeme[i].innerText = "取消只看TA"
  45. }
  46.  
  47. } else {
  48. //退出只看作者状态
  49. event.target.innerText = "只看TA"
  50. GLOBAL_SEEME_AUTHOR = ""
  51. GLOBAL_SEEME_STATUS = false
  52.  
  53. obj_onlyseeme = document.getElementsByClassName("only-seeme")
  54. for (i=0;i<obj_onlyseeme.length;i++){
  55. obj_onlyseeme[i].innerText = "只看TA"
  56. }
  57. }
  58. BDWM_ADDON_SEEME_RENDER() //点击后立刻重新渲染
  59. }else{
  60.  
  61. }
  62.  
  63. },true);
  64.  
  65.  
  66.  
  67. async function BDWM_ADDON_SEEME_RENDER(){
  68.  
  69. if (window.location.href.indexOf("post-read.php")<0){
  70. //当切换到非读贴内容时,立刻重新刷新状态,避免上次查看主楼的延续
  71. GLOBAL_SEEME_AUTHOR = ""
  72. GLOBAL_SEEME_STATUS = false
  73. }
  74.  
  75. obj_found = document.getElementsByClassName("only-seeme")
  76. if (obj_found.length==0){ //本页面第一次加载时,给每个帖子加按钮
  77. obj_functions = document.getElementsByClassName("functions")
  78.  
  79. for (i = 0;i < obj_functions.length; i++){
  80. obj_widebtn = obj_functions[i].getElementsByClassName("line wide-btn")[0]
  81. obj_newobj = document.createElement("a")
  82. obj_newobj.className = "only-seeme"
  83. if (GLOBAL_SEEME_STATUS == false){
  84. obj_newobj.innerText = "只看TA"} else
  85. {obj_newobj.innerText = "取消只看TA"}
  86.  
  87. if (obj_widebtn){
  88. //存在关注按钮,在关注按钮旁边添加即可
  89. str_username = obj_widebtn.getElementsByClassName("add-friend")[0].getAttribute("data-username")
  90. obj_newobj.id = str_username
  91. obj_widebtn.appendChild(obj_newobj)
  92. } else {
  93. //不存在关注按钮,需要首先获取mail中的user-name,再新建一个widebtn类插入
  94. //得到username
  95. str_username = obj_functions[i].getElementsByClassName("mail")[0].getAttribute("data-username")
  96. obj_newobj.id = str_username
  97. //插入widebtn
  98. obj_widebtn = document.createElement("div")
  99. obj_widebtn.className = "line wide-btn"
  100. obj_functions[i].appendChild(obj_widebtn)
  101. //重新获得插入的widebtn DOM
  102. obj_functions[i].lastChild.appendChild(obj_newobj)
  103.  
  104. }
  105.  
  106.  
  107. }
  108. }
  109.  
  110. if (GLOBAL_SEEME_STATUS==true){
  111. console.log("开始渲染只看作者状态")
  112. //只看作者状态
  113. obj_postcard = document.getElementsByClassName("post-card")
  114. for (i = 0;i < obj_postcard.length; i++){
  115. obj_mail = obj_postcard[i].getElementsByClassName("mail")[0] //关注后“关注”按钮将被隐藏
  116. str_username = obj_mail.getAttribute("data-username")
  117. if (str_username!=GLOBAL_SEEME_AUTHOR){
  118. obj_postcard[i].style.display = "none"
  119. }else{
  120. obj_postcard[i].style.display = "block"
  121. }
  122. }
  123.  
  124. }else{
  125. //非只看作者状态
  126. console.log("开始渲染查看全部状态")
  127. obj_postcard = document.getElementsByClassName("post-card")
  128. for (i = 0;i< obj_postcard.length; i++){
  129. obj_postcard[i].style.display = "block"
  130. }
  131. }
  132.  
  133. console.log("渲染完毕")
  134. }