Twitch - Mute ads and optionally hide them

Automatically mutes the Twitch player when an advertisement started and unmute it once finished. You can also hide ads by setting disableDisplay to true.

目前为 2019-10-04 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Twitch - Mute ads and optionally hide them
  3. // @namespace TWITCHADS
  4. // @description Automatically mutes the Twitch player when an advertisement started and unmute it once finished. You can also hide ads by setting disableDisplay to true.
  5. // @include https://www.twitch.tv/*
  6. // @include https://twitch.tv/*
  7. // @version 1.11
  8. // @license MIT
  9. // @author Harest
  10. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. var _tmuteVars = { "timerCheck": 1000, // Checking rate of ad in progress (in ms ; EDITABLE)
  14. "playerMuted": false, // Player muted or not
  15. "adsDisplayed": 0, // Number of ads displayed
  16. "disableDisplay": false, // Disable the player display during an ad (true = yes, false = no (default) ; EDITABLE)
  17. "alreadyMuted": false, // Used to check if the player is muted at the start of an ad
  18. "adElapsedTime": undefined, // Used to check if Twitch forgot to remove the ad notice
  19. "adUnlockAt": 150, // Unlock the player if this amount of seconds elapsed during an ad (EDITABLE)
  20. "adMinTime": 15, // Minimum amount of seconds the player will be muted/hidden since an ad started (EDITABLE ; Recommended to really avoid any ad: 30 to 45)
  21. "squadPage": false, // Either the current page is a squad page or not
  22. "playerIdAds": 0, // Player ID where ads may be displayed (default 0, varying on squads page)
  23. "displayingOptions": false, // Either ads options are currently displayed or not
  24. "highwindPlayer": undefined // If you've the Highwind Player or not (automatically checked)
  25. };
  26. // Selectors for the old player and the highwind one
  27. var _tmuteSelectors = { "old": { "player": "player-video", // Player class
  28. "playerVideo": ".player-video", // Player video selector
  29. "muteButton": ".player-button--volume", // (un)mute button selector
  30. "adNotice": "player-ad-notice" // Ad notice class
  31. },
  32. "hw": { "player": "highwind-video-player__container", // Player class
  33. "playerVideo": ".highwind-video-player__container video", // Player video selector
  34. "muteButton": "button[data-a-target='player-mute-unmute-button']", // (un)mute button selector
  35. "adNotice": "tw-absolute tw-c-background-overlay tw-c-text-overlay tw-inline-block tw-left-0 tw-pd-1 tw-top-0" // Ad notice class
  36. }
  37. };
  38. // Current selector (either old or highwind player, automatically set below)
  39. var currentSelector = undefined;
  40. // Check if there's an ad
  41. function checkAd()
  42. {
  43. // Check if you're watching a stream, useless to continue if not
  44. if (_tmuteVars.highwindPlayer === undefined) {
  45. var isOldPlayer = document.getElementsByClassName(_tmuteSelectors.old.player).length;
  46. var isHwPlayer = document.getElementsByClassName(_tmuteSelectors.hw.player).length;
  47. var isViewing = Boolean(isOldPlayer + isHwPlayer);
  48. if (isViewing === false) return;
  49. // We set the type of player currently used (old or highwind one)
  50. _tmuteVars.highwindPlayer = Boolean(isHwPlayer);
  51. currentSelector = (_tmuteVars.highwindPlayer === true) ? _tmuteSelectors.hw : _tmuteSelectors.old;
  52. console.log("You're currently using the " + ((_tmuteVars.highwindPlayer === true) ? "Highwind" : "old") + " player.");
  53. } else {
  54. var isViewing = Boolean(document.getElementsByClassName(currentSelector.player).length);
  55. if (isViewing === false) return;
  56. }
  57. // Initialize the ads options if necessary.
  58. var optionsInitialized = (document.getElementById("_tmads_options") === null) ? false : true;
  59. if (optionsInitialized === false) adsOptions("init");
  60. var advert = document.getElementsByClassName(currentSelector.adNotice);
  61. if (_tmuteVars.adElapsedTime !== undefined)
  62. {
  63. _tmuteVars.adElapsedTime++;
  64. if (_tmuteVars.adElapsedTime >= _tmuteVars.adUnlockAt && advert[0] !== undefined)
  65. {
  66. advert[0].parentNode.removeChild(advert[0]);
  67. console.log("Unlocking Twitch player as Twitch forgot to remove the ad notice after the ad(s).");
  68. }
  69. }
  70. if ((advert.length >= 1 && _tmuteVars.playerMuted === false) || (_tmuteVars.playerMuted === true && advert.length === 0))
  71. {
  72. // Update at the start of an ad if the player is already muted or not
  73. if (advert.length >= 1) _tmuteVars.alreadyMuted = Boolean(document.querySelectorAll(currentSelector.muteButton)[_tmuteVars.playerIdAds].getAttribute("aria-label") === "Unmute (m)");
  74. // Keep the player muted/hidden for the minimum ad time set (Twitch started to remove the ad notice before the end of some ads)
  75. if (advert.length === 0 && _tmuteVars.adElapsedTime !== undefined && _tmuteVars.adElapsedTime < _tmuteVars.adMinTime) return;
  76.  
  77. mutePlayer();
  78. }
  79. }
  80.  
  81. // (un)Mute Player
  82. function mutePlayer()
  83. {
  84. if (document.querySelectorAll(currentSelector.muteButton).length >= 1)
  85. {
  86. if (_tmuteVars.alreadyMuted === false) document.querySelectorAll(currentSelector.muteButton)[_tmuteVars.playerIdAds].click(); // If the player is already muted before an ad, we avoid to unmute it.
  87. _tmuteVars.playerMuted = !(_tmuteVars.playerMuted);
  88.  
  89. if (_tmuteVars.playerMuted === true)
  90. {
  91. _tmuteVars.adsDisplayed++;
  92. _tmuteVars.adElapsedTime = 1;
  93. console.log("Ad #" + _tmuteVars.adsDisplayed + " detected. Player " + (_tmuteVars.alreadyMuted === true ? "already " : "") + "muted.");
  94. if (_tmuteVars.disableDisplay === true) document.querySelectorAll(currentSelector.playerVideo)[_tmuteVars.playerIdAds].style.visibility = "hidden";
  95. } else {
  96. console.log("Ad #" + _tmuteVars.adsDisplayed + " finished (lasted " + _tmuteVars.adElapsedTime + "s)." + (_tmuteVars.alreadyMuted === true ? "" : " Player unmuted."));
  97. _tmuteVars.adElapsedTime = undefined;
  98. if (_tmuteVars.disableDisplay === true) document.querySelectorAll(currentSelector.playerVideo)[_tmuteVars.playerIdAds].style.visibility = "visible";
  99. }
  100. } else {
  101. console.log("No volume button found (class changed ?).");
  102. }
  103. }
  104. // Manage ads options
  105. function adsOptions(changeType = "show")
  106. {
  107. switch(changeType) {
  108. // Manage player display during an ad (either hiding the ads or still showing them)
  109. case "display":
  110. _tmuteVars.disableDisplay = !(_tmuteVars.disableDisplay);
  111. // Update the player display if an ad is supposedly in progress
  112. if (_tmuteVars.playerMuted === true) document.querySelectorAll(currentSelector.playerVideo)[_tmuteVars.playerIdAds].style.visibility = (_tmuteVars.disableDisplay === true) ? "hidden" : "visible";
  113. document.getElementById("_tmads_display").innerText = (_tmuteVars.disableDisplay === true ? "Show" : "Hide") + " player during ads";
  114. break;
  115. // Force a player unlock if Twitch didn't remove the ad notice properly instead of waiting the auto unlock
  116. case "unlock":
  117. var advert = document.getElementsByClassName(currentSelector.adNotice);
  118. if (_tmuteVars.adElapsedTime === undefined && advert[0] === undefined)
  119. {
  120. alert("There's no ad notice displayed. No unlock to do.");
  121. } else {
  122. // We set the elapsed time to the unlock timer to trigger it during the next check.
  123. _tmuteVars.adElapsedTime = _tmuteVars.adUnlockAt;
  124. console.log("Unlock requested.");
  125. }
  126. break;
  127. // Display the ads options button
  128. case "init":
  129. if (document.getElementsByClassName("channel-info-bar__viewers-wrapper")[0] === undefined && document.getElementsByClassName("squad-stream-top-bar__container")[0] === undefined) break;
  130. // Append ads options and events related
  131. var optionsTemplate = document.createElement("div");
  132. optionsTemplate.id = "_tmads_options-wrapper";
  133. optionsTemplate.className = "tw-inline-flex";
  134. optionsTemplate.innerHTML = `
  135. <span id="_tmads_options" style="display: none;">
  136. <button type="button" id="_tmads_unlock" style="padding: 0 2px 0 2px; margin-left: 2px; height: 16px; width: unset;" class="tw-interactive tw-button-icon tw-button-icon--hollow">Unlock player</button>
  137. <button type="button" id="_tmads_display" style="padding: 0 2px 0 2px; margin-left: 2px; height: 16px; width: unset;" class="tw-interactive tw-button-icon tw-button-icon--hollow">` + (_tmuteVars.disableDisplay === true ? "Show" : "Hide") + ` player during ads</button>
  138. </span>
  139. <button type="button" id="_tmads_showoptions" style="padding: 0 2px 0 2px; margin-left: 2px; height: 16px; width: unset;" class="tw-interactive tw-button-icon tw-button-icon--hollow">Ads Options</button>`;
  140. // Normal player page
  141. if (document.getElementsByClassName("channel-info-bar__viewers-wrapper")[0] !== undefined)
  142. {
  143. _tmuteVars.squadPage = false;
  144. _tmuteVars.playerIdAds = 0;
  145. document.getElementsByClassName("channel-info-bar__viewers-wrapper")[0].parentNode.appendChild(optionsTemplate);
  146. // Squad page
  147. } else if (document.getElementsByClassName("squad-stream-top-bar__container")[0] !== undefined)
  148. {
  149. _tmuteVars.squadPage = true;
  150. _tmuteVars.playerIdAds = 0;
  151. // Since the primary player is never at the same place, we've to find it.
  152. for (var i = 0; i < parseInt(document.querySelectorAll(currentSelector.playerVideo).length); i++)
  153. {
  154. if (document.getElementsByClassName("multi-stream-player-layout__player-container")[0].childNodes[i].classList.contains("multi-stream-player-layout__player-primary"))
  155. {
  156. _tmuteVars.playerIdAds = i;
  157. break;
  158. }
  159. }
  160. document.getElementsByClassName("squad-stream-top-bar__container")[0].appendChild(optionsTemplate);
  161. }
  162. document.getElementById("_tmads_showoptions").addEventListener("click", adsOptions, false);
  163. document.getElementById("_tmads_display").addEventListener("click", function() { adsOptions("display"); }, false);
  164. document.getElementById("_tmads_unlock").addEventListener("click", function() { adsOptions("unlock"); }, false);
  165. console.log("Ads options initialized.");
  166. break;
  167. // Display/Hide the ads options
  168. case "show":
  169. default:
  170. _tmuteVars.displayingOptions = !(_tmuteVars.displayingOptions);
  171. document.getElementById("_tmads_options").style.display = (_tmuteVars.displayingOptions === false) ? "none" : "inline-flex";
  172. }
  173. }
  174. // Start the background check
  175. _tmuteVars.autoCheck = setInterval(checkAd, _tmuteVars.timerCheck);
  176. })();