Hide Bot Comments

Removes comments made by bots on websites such as YouTube.

当前为 2022-02-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Hide Bot Comments
  3. // @namespace https://theusaf.org
  4. // @version 1.3.0
  5. // @description Removes comments made by bots on websites such as YouTube.
  6. // @author theusaf
  7. // @match https://www.youtube.com/**
  8. // @copyright 2022 theusaf
  9. // @license MIT
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. const SITES = Object.freeze({
  14. YOUTUBE: [
  15. /^\s{2,}/, // starts with too much whitespace
  16. /^(\s*@.+)?\s*(https:\/\/[^\s]+|[\n.\s])+$/, // only links and other punctuation
  17. /^(\s*@.+)?\s*[A-Z\s\r\n!]*https:\/\/[^\s]+[A-Z\s\r\n!]*$/, // all caps and a link
  18. /^(\s*@.+)?\s*https:\/\/[^\s]+(\n|.|\s)*([dD]on'?t [mM]iss|Bots for u|Finally|💜|fax)/, // A link and a random message afterwards
  19. /^(\s*@.+)?\s*(This|[Ww]ow!?)\s*https:\/\/[^\s]+/, // word + link
  20. /^(\s*@.+)?\s*https:\/\/[^\s]+\s*[a-z]+\s*$/, // link + random "word"
  21. /PRIVATE S\*X|over 18/, // ...
  22. /beautyzone\.\w+|\.cam|lust\.\w+/i, // suspicious websites
  23. /-{5,}/, // too many "-"
  24. /SPECIAL FOR YOU|MY CONTENT/, // common phrase
  25. (text) => {
  26. const charSets = [
  27. /[ᴀʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘᴏ̨ʀsᴛᴜᴠᴡxʏᴢ\s]/g,
  28. /[\u1D538-\u1D56B]/g // math letter symbols
  29. ]
  30. const smallLatinCaps = text.match(/[ᴀʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘᴏ̨ʀsᴛᴜᴠᴡxʏᴢ\s]/g)?.length ?? 0;
  31. return smallLatinCaps / text.length > 0.7 && text.length > 10;
  32. }
  33. ]
  34. }),
  35. site = getCurrentSite(),
  36. commentMutationListener = new MutationObserver((mutations) => {
  37. for (const mutation of mutations) {
  38. for (const node of mutation.addedNodes) {
  39. const text = getCommentText(node, site);
  40. if (text) {
  41. if (isCommentLikelyBotComment(text, site)) {
  42. node.style.display = "none";
  43. }
  44. }
  45. }
  46. }
  47. });
  48.  
  49. commentMutationListener.observe(document.body, {
  50. subtree: true,
  51. childList: true
  52. });
  53.  
  54. /**
  55. * Determines whether a comment is likely spam.
  56. *
  57. * @param {String} text The comment's content
  58. * @param {Object} site The website the comment is from
  59. * @return {Boolean}
  60. */
  61. function isCommentLikelyBotComment(text, siteChecks) {
  62. for (const check of siteChecks) {
  63. if (typeof check === "function") {
  64. if (check(text)) {
  65. return true;
  66. }
  67. } else {
  68. // assume regex
  69. if (check.test(text)) {
  70. return true;
  71. }
  72. }
  73. }
  74. return false;
  75. }
  76.  
  77. function getCommentText(node, site) {
  78. switch (site) {
  79. case SITES.YOUTUBE: {
  80. if (node.nodeName === "YTD-COMMENT-RENDERER") {
  81. return node.querySelector("#content-text").textContent;
  82. }
  83. }
  84. }
  85. return null;
  86. }
  87.  
  88. function getCurrentSite() {
  89. switch (location.hostname) {
  90. case "www.youtube.com": {
  91. return SITES.YOUTUBE;
  92. }
  93. }
  94. }