Lolz Comment Toggle

Показывать/скрывать комментарии на lolz.live

  1. // ==UserScript==
  2. // @name Lolz Comment Toggle
  3. // @namespace https://lolz.live/unique
  4. // @version 1.0
  5. // @description Показывать/скрывать комментарии на lolz.live
  6. // @author https://lolz.live/unique
  7. // @license MIT
  8. // @match https://lolz.live/*
  9. // @icon https://lolz.live/favicon.ico
  10. // @grant none
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. function waitForElements(selector, callback) {
  18. const observer = new MutationObserver(() => {
  19. const elements = document.querySelectorAll(selector);
  20. if (elements.length) {
  21. callback(elements);
  22. observer.disconnect();
  23. }
  24. });
  25. observer.observe(document.body, { childList: true, subtree: true });
  26. }
  27.  
  28. waitForElements('.secondaryContent.blockLinksList', (menus) => {
  29. menus.forEach((menu) => {
  30. const postContainer = menu.closest('li[id^="profile-post-"], li[id^="post-"]');
  31. if (!postContainer) return;
  32.  
  33. const commentBlock = postContainer.querySelector('.messageResponse');
  34. const commentBlock2 = postContainer.querySelector('.messageSimple');
  35.  
  36. let targetBlock = null;
  37. let commentElements = [];
  38. let commentMore = [];
  39.  
  40. if (commentBlock) {
  41. const comments = commentBlock.querySelectorAll('.commentInfo');
  42. if (comments.length > 0) {
  43. targetBlock = commentBlock;
  44. commentElements = commentBlock.querySelectorAll('.comment');
  45. commentMore = commentBlock.querySelectorAll('.commentMore');
  46. }
  47. }
  48.  
  49. if (!targetBlock && commentBlock2) {
  50. const comments2 = commentBlock2.querySelectorAll('.commentInfo');
  51. if (comments2.length > 0) {
  52. targetBlock = commentBlock2;
  53. commentElements = commentBlock2.querySelectorAll('.comment');
  54. commentMore = commentBlock2.querySelectorAll('.commentMore');
  55. }
  56. }
  57.  
  58. if (!targetBlock || commentElements.length === 0) return;
  59.  
  60. // Скрываем комментарии
  61. commentElements.forEach(comment => comment.style.display = 'none');
  62. commentMore.forEach(comment => comment.style.display = 'none');
  63.  
  64. // Создаём кнопку
  65. const showCommentsLi = document.createElement('li');
  66. const showCommentsBtn = document.createElement('a');
  67. showCommentsBtn.className = 'commentMore CommentLoader';
  68. showCommentsBtn.textContent = 'Показать комментарии';
  69. showCommentsBtn.style.cursor = 'pointer';
  70. if (commentBlock2) {
  71. showCommentsBtn.style.marginLeft = '70px';
  72. }
  73.  
  74. let commentsHidden = true;
  75.  
  76. showCommentsBtn.addEventListener('click', (e) => {
  77. e.preventDefault();
  78. commentsHidden = !commentsHidden;
  79. commentElements.forEach(comment => comment.style.display = commentsHidden ? 'none' : '');
  80. commentMore.forEach(comment => comment.style.display = commentsHidden ? 'none' : '');
  81. showCommentsBtn.textContent = commentsHidden ? 'Показать комментарии' : 'Скрыть комментарии';
  82. });
  83.  
  84. showCommentsLi.appendChild(showCommentsBtn);
  85.  
  86. targetBlock.parentElement.insertBefore(showCommentsLi, targetBlock.nextSibling);
  87. });
  88. });
  89. })();