Mixer Audio Only

Set streams to audio-only depending on the state of a checkbox/cookie

当前为 2019-10-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Mixer Audio Only
  3. // @namespace https://github.com/antisocialian/MixerAudioOnly
  4. // @version 0.4
  5. // @description Set streams to audio-only depending on the state of a checkbox/cookie
  6. // @author antisocialian
  7. // @match *mixer.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var m;
  15.  
  16. //initialize global variables
  17. var audioOnly = false;
  18. var intervalvariable, intervalnav;
  19.  
  20. //get the cookie that stores true/false for the page
  21. checkCookie();
  22.  
  23. //hook into the window's load event to check that the video is in the correct state. doing this as an interval DOES mean it is running constantly while the checkbox is checked,
  24. //but ALSO means it will catch when the channel hosts or goes offline & online again. This was the only workaround I could get to catch whent he channel had hosted someone else(or went offline&online)
  25. window.addEventListener('load', function() {
  26. intervalnav = setInterval(navLoad, ((Math.random()) + 2) *1000);
  27. intervalvariable = setInterval(cAO, ((Math.random()) + 3) *1000);
  28. }, false);
  29.  
  30. function navLoad() {
  31. clearInterval(intervalnav);
  32.  
  33. //add the checkbox along the top menu on the page and add hook to its onClick() event
  34. var navbar = document.getElementsByClassName("left_31xse");
  35. console.error(navbar.length);
  36. navbar[0].insertAdjacentHTML("beforeend", "<label _ngcontent-c5 class='nav-link'><input type='checkbox' id='chkaudioOnly' value='audioOnly'> Audio Only</label>");
  37. m = document.getElementById("chkaudioOnly");
  38. m.addEventListener('click', tglAO, false);
  39. }
  40.  
  41. //this is the event for the checkbox onClick() hook
  42. //it should set the global variable to the state of the checkbox, then set the cookie, then redo the audio-only button, and finally stop/start the interval function again
  43. function tglAO() {
  44. //var tmpAO = m.checked;
  45. if(m){
  46. audioOnly = m.checked;
  47. }
  48. setCookie("audioOnly", audioOnly, 30);
  49. cAO();
  50. if (m.checked == false) {
  51. clearInterval(intervalvariable);
  52. } else {
  53. intervalvariable = setInterval(cAO, ((Math.random()) + 3) *1000);
  54. }
  55. }
  56.  
  57. //this is the function to check/click the audio-only button
  58. function cAO(){
  59. //get the list of elements with the correct class.
  60. //**NOTE** this may need to be updated, should the site change the layout of the page
  61. var x = document.getElementsByClassName("_2YmB_I5OliPyB7_rs748W3 _1kIlUXtgizhBW5Drjbvqmm");
  62. var i,j;
  63.  
  64. for (i = 0; i < x.length; i++) {
  65. //since the button element uses an aria-label and not a name/id we have to check the attributes of each element to find the audio-only button
  66. for (j = 0; j < x[i].attributes.length; j++) {
  67. if (x[i].attributes[j].value == "Audio Only") {
  68. //comparing the inner <div> HTML of the button to check the state of the audio only.
  69. //this was the only waay I could find to get the state of the button from the page so that we could properly click the button
  70. //the problem is that the button looks like a checkbox, but is in fact a button that changes the icon of a checkbox depending
  71. //on whether it has been clicked on/off
  72. var p = x[i].innerHTML;
  73. var r = p.includes("check_box_outline_blank");
  74. if (r == true) {
  75. if (audioOnly == true) {
  76. if(m){
  77. m.checked = true;
  78. }
  79. x[i].click();
  80. }
  81. } else {
  82. if (audioOnly == false) {
  83. if(m){
  84. m.checked = false;
  85. }
  86. x[i].click();
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93.  
  94. //copied from w3schools.com/js/js_cookies.asp
  95. //i didn't make this function
  96. function setCookie(cname, cvalue, exdays) {
  97. var d = new Date();
  98. d.setTime(d.getTime() + (exdays*24*60*60*1000));
  99. var expires = "expires="+ d.toUTCString();
  100. document.cookie = cname + "=" + cvalue + ";" + expires;
  101. }
  102.  
  103. //copied from w3schools.com/js/js_cookies.asp
  104. //i didn't make this function
  105. function getCookie(cname) {
  106. var name = cname + "=";
  107. var decodedCookie = decodeURIComponent(document.cookie);
  108. var ca = decodedCookie.split(';');
  109. for(var i = 0; i <ca.length; i++) {
  110. var c = ca[i];
  111. while (c.charAt(0) == ' ') {
  112. c = c.substring(1);
  113. }
  114. if (c.indexOf(name) == 0) {
  115. return c.substring(name.length, c.length);
  116. }
  117. }
  118. return "";
  119. }
  120.  
  121. //check the cookie and set the global variable & checkbox accordingly
  122. function checkCookie() {
  123. var tmpCookie = getCookie("audioOnly");
  124. if (tmpCookie == 'true') {
  125. audioOnly = true;
  126. if(m){
  127. m.checked = true;
  128. }
  129. } else {
  130. audioOnly = false;
  131. if(m){
  132. m.checked = false;
  133. }
  134. }
  135. }
  136. }
  137. )();