NGA Filter

troll must die

当前为 2019-08-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name NGA Filter
  3. // @version 0.1
  4. // @author snyssss
  5. // @description troll must die
  6. // @match *bbs.nga.cn/thread.php?fid=*
  7. // @match *bbs.nga.cn/read.php?tid=*
  8. // @match *ngabbs.com/thread.php?fid=*
  9. // @match *ngabbs.com/read.php?tid=*
  10. // @grant none
  11. // @noframes。
  12.  
  13. // todo 屏蔽模式;直接在页面里添加屏蔽人
  14.  
  15. // v0.1 简单做了屏蔽逻辑,根据trollArray里的用户ID删除相关帖子或者楼层。
  16. // @namespace https://greasyfork.org/users/263018
  17. // ==/UserScript==
  18.  
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. const trollArray = [35159831];
  24. const trollMap = Object.assign({}, ...trollArray.map(item => ({ [item]: true })));
  25.  
  26. const getUID = function(e) {
  27. let author = e.getElementsByClassName('author')[0];
  28. if (author) {
  29. return author.search.match(/uid=(\S*)/)[1];
  30. }
  31. };
  32.  
  33. const isTroll = function (uid) {
  34. uid = ~~uid;
  35. if (uid) {
  36. return trollMap[uid];
  37. }
  38. return false;
  39. };
  40.  
  41. const observerElements = [
  42. [
  43. document.getElementById('topicrows'),
  44. function (e) {
  45. let uid = getUID(e);
  46. if (isTroll(uid)) {
  47. e.remove();
  48. }
  49. }
  50. ],
  51. [
  52. document.getElementById('m_posts_c'),
  53. function (e) {
  54. let uid = getUID(e);
  55. if (isTroll(uid)) {
  56. e.remove();
  57. }
  58. }
  59. ]
  60. ];
  61.  
  62. [].slice.call(observerElements).forEach(function (e) {
  63. if (!e[0]) return;
  64.  
  65. e[0].refilter = function() {
  66. [].slice.call(e[0].children).forEach(function (c) {
  67. e[1](c);
  68. });
  69. }
  70.  
  71. e[0].refilter();
  72.  
  73. let observer = new MutationObserver(function (mutations) {
  74. mutations.forEach(function (mutation) {
  75. if (mutation.addedNodes.length) {
  76. e[1](mutation.addedNodes[0]);
  77. }
  78. });
  79. });
  80.  
  81. observer.observe(e[0], {
  82. childList: true
  83. });
  84. });
  85. })();