Toggle YouTube Heart Comments

Toggle YouTube heart comments.

当前为 2021-05-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Toggle YouTube Heart Comments
  3. // @namespace https://github.com/gslin/toggle-youtube-heart-comments-userscript
  4. // @match https://www.youtube.com/*
  5. // @grant none
  6. // @version 0.20210524.0
  7. // @author Gea-Suan Lin <gslin@gslin.com>
  8. // @description Toggle YouTube heart comments.
  9. // ==/UserScript==
  10.  
  11. (() => {
  12. 'use strict;'
  13.  
  14. let ob = new window.MutationObserver(mutations => {
  15. mutations.forEach(mutation => {
  16. mutation.addedNodes.forEach(() => {
  17. // Only filter pages whose path are "/watch".
  18. if (document.location.pathname !== '/watch') {
  19. return;
  20. }
  21.  
  22. let sheet = document.querySelector('.has-creator-heart-style');
  23. if (!sheet) {
  24. sheet = document.createElement('style');
  25. sheet.classList.add('has-creator-heart-style');
  26. document.getElementsByTagName('head')[0].appendChild(sheet);
  27. }
  28.  
  29. // Wait for ytd-comments element ready.
  30. let comments_el = document.querySelector('ytd-comments');
  31. if (!comments_el) {
  32. return;
  33. }
  34.  
  35. // Simulate "ytd-comment-thread-renderer:has(ytd-creator-heart-renderer)" selector.
  36. let comments_heart = document.querySelectorAll('ytd-comment-thread-renderer');
  37. comments_heart.forEach(el => {
  38. if (el.querySelector('ytd-creator-heart-renderer')) {
  39. el.classList.add('has-creator-heart');
  40. }
  41. });
  42.  
  43. // Don't install twice.
  44. if (document.getElementById('toggle_youtube_star_comments')) {
  45. return;
  46. }
  47.  
  48. let toggle_el = document.createElement('div');
  49. toggle_el.innerHTML = '<button id="toggle_youtube_star_comments">Toggle star comments</button>';
  50. toggle_el.addEventListener('click', () => {
  51. if ('' === sheet.innerHTML) {
  52. sheet.innerHTML = '.has-creator-heart { display: none; }';
  53. } else {
  54. sheet.innerHTML = '';
  55. }
  56. });
  57.  
  58. comments_el.insertAdjacentElement('beforebegin', toggle_el);
  59. console.debug('Toggle YouTube Heart Comments installed.');
  60. });
  61. });
  62. });
  63.  
  64. ob.observe(document, {
  65. childList: true,
  66. subtree: true,
  67. });
  68. })();