Hide Bot Comments

Removes comments made by bots on websites such as YouTube.

目前為 2022-02-14 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Hide Bot Comments
  3. // @namespace https://theusaf.org
  4. // @version 1.1.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. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. const SITES = Object.freeze({
  15. YOUTUBE: [
  16. /^\s{2,}/,
  17. /^https:\/\/[^\s]+$/,
  18. (text) => {
  19. const smallLatinCaps = text.match(/[ᴀʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘᴏ̨ʀsᴛᴜᴠᴡxʏᴢ\s]/g)?.length ?? 0;
  20. return smallLatinCaps / text.length > 0.7 && text.length > 10;
  21. }
  22. ]
  23. }),
  24. site = getCurrentSite(),
  25. commentMutationListener = new MutationObserver((mutations) => {
  26. for (const mutation of mutations) {
  27. for (const node of mutation.addedNodes) {
  28. const text = getCommentText(node, site);
  29. if (text) {
  30. if (isCommentLikelyBotComment(text, site)) {
  31. node.style.display = "none";
  32. }
  33. }
  34. }
  35. }
  36. });
  37.  
  38. commentMutationListener.observe(document.body, {
  39. subtree: true,
  40. childList: true
  41. });
  42.  
  43. /**
  44. * Determines whether a comment is likely spam.
  45. *
  46. * @param {String} text The comment's content
  47. * @param {Object} site The website the comment is from
  48. * @return {Boolean}
  49. */
  50. function isCommentLikelyBotComment(text, siteChecks) {
  51. for (const check of siteChecks) {
  52. if (typeof check === "function") {
  53. if (check(text)) {
  54. return true;
  55. }
  56. } else {
  57. // assume regex
  58. if (check.test(text)) {
  59. return true;
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65.  
  66. function getCommentText(node, site) {
  67. switch (site) {
  68. case SITES.YOUTUBE: {
  69. if (node.nodeName === "YTD-COMMENT-RENDERER") {
  70. return node.querySelector("#content-text").textContent;
  71. }
  72. }
  73. }
  74. return null;
  75. }
  76.  
  77. function getCurrentSite() {
  78. switch (location.hostname) {
  79. case "www.youtube.com": {
  80. return SITES.YOUTUBE;
  81. }
  82. }
  83. }