BYR BBS User Blocker

在北邮人论坛中屏蔽黑名单用户的发言

  1. // ==UserScript==
  2. // @name BYR BBS User Blocker
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 在北邮人论坛中屏蔽黑名单用户的发言
  6. // @author MadDevil
  7. // @match *://bbs.byr.cn/*
  8. // @grant GM_xmlhttpRequest
  9. // @require https://code.jquery.com/jquery-3.6.0.min.js
  10. // ==/UserScript==
  11.  
  12.  
  13. class Blocker {
  14. constructor() {
  15. this.username = document.getElementsByClassName('u-login-id')[0].getElementsByTagName('a')[0].innerText;
  16. this.blocked = new Set();
  17. this.orginal_page = null;
  18. this.get_blacklist();
  19. }
  20.  
  21.  
  22. update_page() {
  23. if (this.orginal_page === null) this.orginal_page = $('.b-content').html();
  24. else $('.b-content').html(this.orginal_page);
  25. let articles = $('.article');
  26. for (let i = 0; i < articles.length; ++i) {
  27. let article = $(articles[i]);
  28. let u_name = article.find('.a-u-name').text();
  29. let is_blocked = this.blocked.has(u_name);
  30. let bt_text = ["屏蔽此人", "取消屏蔽"][Number(is_blocked)];
  31. let button = $('<li><samp class="ico-pos-deny"></samp><a href="javascript:void(0)">' + bt_text + '</a></li>');
  32. $(article.find('.a-func').find('li')[4]).after(button);
  33. button.click(that => {
  34. if (is_blocked) this.del_user(u_name);
  35. else this.add_user(u_name);
  36. });
  37. article.find('.a-content')[0].style.display = ["none", "block"][Number(!is_blocked)];
  38. article.find('.a-content2')[0].style.display = ["none", "block"][Number(is_blocked)];
  39. if (is_blocked) article.find('.a-content2').find('p')[0].innerText = "此用户发言已被您屏蔽。"
  40. }
  41.  
  42. }
  43.  
  44. get_blacklist(page = 1) {
  45. $.ajax("/blacklist?p=" + page + "&_uid=" + this.username).done(res => {
  46. let doc = $(res), users = doc.find('.title_2');
  47. for (let i = 0; i < users.length; ++i)
  48. this.blocked.add(users[i].innerText);
  49. if(doc.find("[title=下一页]").length!==0) this.get_blacklist(page+1);
  50. else if (location.hash.search(/^#!article/) === 0) this.update_page();
  51. })
  52.  
  53. }
  54.  
  55.  
  56. add_user(u_name) {
  57. $.ajax({
  58. "url": "/blacklist/ajax_add.json",
  59. "type": "POST",
  60. "data": {"id": u_name}
  61. }).done(res => {
  62. this.blocked.add(u_name);
  63. this.update_page();
  64. });
  65. }
  66.  
  67. del_user(u_name) {
  68. $.ajax({
  69. "url": "/blacklist/ajax_delete.json",
  70. "type": "POST",
  71. "data": "f_" + u_name + "=on"
  72. }).done(res => {
  73. this.blocked.delete(u_name);
  74. this.update_page();
  75. });
  76.  
  77. }
  78. }
  79.  
  80. function wait_until(condition, handler) {
  81. function wrapper() {
  82. if (condition()) handler();
  83. else setTimeout(wrapper, 100);
  84. }
  85.  
  86. wrapper();
  87. }
  88.  
  89. var blocker = null;
  90. $(document).ready(() => wait_until(() => document.getElementById('nforum_tips').style.display === 'none', () => blocker = new Blocker()));
  91. window.addEventListener('hashchange', function (e) {
  92. if (blocker !== null) {
  93. blocker.orginal_page = null;
  94. if (location.hash.search(/^#!article/) === 0)
  95. wait_until(() => document.getElementById('nforum_tips').style.display === 'none', () => blocker.update_page());
  96. }
  97. }, false);