YouTube - Enable Live Chat By Default

Automatically changes the chat view to Live Chat when it loads. (Works with chat replays, too.)

  1. // ==UserScript==
  2. // @name YouTube - Enable Live Chat By Default
  3. // @namespace Violentmonkey Scripts
  4. // @description Automatically changes the chat view to Live Chat when it loads. (Works with chat replays, too.)
  5. // @match https://www.youtube.com/live_chat*
  6. // @include https://*youtube.*/live_chat*
  7. // @include https://www.youtube.*/live_chat*
  8. // @include https://www.youtube.*/live_chat
  9. // @grant none
  10. // @version 1.0.1
  11. // @author Jupiter Liar
  12. // @license CC BY
  13. // @description 3/18/2024, 4:00:00 PM
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. var maxAttempts = 400;
  20. var currentAttempt = 0;
  21.  
  22. // Function to switch chat mode to "Live Chat"
  23. function changeToLiveChat(liveChatSelector) {
  24. //console.log("Live chat selector found.");
  25. // Check if the live chat selector exists
  26. if (liveChatSelector) {
  27. // Click the tp-yt-paper-button to switch to "Live Chat" mode
  28. var paperButton = liveChatSelector.querySelector('tp-yt-paper-button');
  29. if (paperButton) {
  30. paperButton.click();
  31. //console.log("Attempting to switch to Live Chat mode...");
  32. checkPaperMenuItems();
  33. } else {
  34. //console.log("tp-yt-paper-button not found within live chat selector.");
  35. }
  36. }
  37. }
  38.  
  39. // Function to click on the appropriate paper item in the paper menu
  40. function checkPaperMenuItems() {
  41. var paperMenuItems = document.querySelectorAll('tp-yt-paper-item');
  42. var foundLiveChat = false;
  43. var foundUnselected = false;
  44.  
  45. paperMenuItems.forEach(function(item) {
  46. var text = item.innerText.toLowerCase();
  47. if (text.includes("live chat")) {
  48. item.click();
  49. //console.log("Clicked on paper item with 'Live chat'.");
  50. foundLiveChat = true;
  51. } else {
  52. var parentAnchor = item.closest('a');
  53. if (parentAnchor && parentAnchor.getAttribute('aria-selected') === "false") {
  54. item.click();
  55. //console.log("Clicked on unselected paper item.");
  56. foundUnselected = true;
  57. }
  58. }
  59. });
  60.  
  61. if (!foundLiveChat && !foundUnselected) {
  62. //console.log("Unable to find appropriate paper menu item.");
  63. }
  64. }
  65.  
  66. // Function to check if the page contains the live chat selector
  67. function checkLiveChatSelector() {
  68. //console.log("Attempting to run Live Chat changer script.");
  69. var liveChatSelector = document.querySelector('#live-chat-view-selector-sub-menu');
  70. if (liveChatSelector) {
  71. changeToLiveChat(liveChatSelector);
  72. }
  73. }
  74.  
  75. // Execute when the page and its resources are fully loaded
  76. window.onload = checkLiveChatSelector;
  77. })();