YouTube Enhancer (Secret Stats)

Integrating "Secret Stats" and "Stream Stats" buttons into the channel page, directing users to detailed analytics pages for insights into the channel.

当前为 2024-11-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Enhancer (Secret Stats)
  3. // @description Integrating "Secret Stats" and "Stream Stats" buttons into the channel page, directing users to detailed analytics pages for insights into the channel.
  4. // @icon https://raw.githubusercontent.com/exyezed/youtube-enhancer/refs/heads/main/extras/youtube-enhancer.png
  5. // @version 1.2
  6. // @author exyezed
  7. // @namespace https://github.com/exyezed/youtube-enhancer/
  8. // @supportURL https://github.com/exyezed/youtube-enhancer/issues
  9. // @license MIT
  10. // @match https://www.youtube.com/*
  11. // @grant none
  12. // @run-at document-idle
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. function getChannelIdentifier() {
  19. const url = window.location.href;
  20. let identifier = '';
  21. if (url.includes('/channel/')) {
  22. identifier = url.split('/channel/')[1].split('/')[0];
  23. } else if (url.includes('/@')) {
  24. identifier = url.split('/@')[1].split('/')[0];
  25. }
  26. return identifier;
  27. }
  28.  
  29. function createStatsButton(text, className, idName, svgPath, statsType) {
  30. const containerDiv = document.createElement('div');
  31. containerDiv.className = `yt-flexible-actions-view-model-wiz__action ${className}-container`;
  32.  
  33. const buttonViewModel = document.createElement('button-view-model');
  34. buttonViewModel.className = `yt-spec-button-view-model ${className}-view-model`;
  35.  
  36. const button = document.createElement('button');
  37. button.className = `yt-spec-button-shape-next yt-spec-button-shape-next--outline yt-spec-button-shape-next--mono yt-spec-button-shape-next--size-m yt-spec-button-shape-next--enable-backdrop-filter-experiment ${className}-button`;
  38. button.setAttribute('aria-disabled', 'false');
  39. button.setAttribute('aria-label', `View ${text}`);
  40. button.id = idName;
  41. button.style.display = 'flex';
  42. button.style.alignItems = 'center';
  43. button.style.justifyContent = 'center';
  44. button.style.gap = '8px';
  45.  
  46. button.addEventListener('click', () => {
  47. const identifier = getChannelIdentifier();
  48. if (identifier) {
  49. const url = `https://exyezed.vercel.app/stats/${statsType}/${identifier}`;
  50. window.open(url, '_blank');
  51. }
  52. });
  53.  
  54. const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  55. svg.setAttribute("viewBox", "0 0 576 512");
  56. svg.style.width = "20px";
  57. svg.style.height = "20px";
  58. svg.style.fill = "currentColor";
  59. svg.style.flexShrink = "0";
  60. svg.style.display = "flex";
  61. svg.style.alignItems = "center";
  62.  
  63. const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
  64. path.setAttribute("d", svgPath);
  65. svg.appendChild(path);
  66.  
  67. const buttonText = document.createElement('div');
  68. buttonText.className = `yt-spec-button-shape-next__button-text-content ${className}-text`;
  69. buttonText.textContent = text;
  70. buttonText.style.display = 'flex';
  71. buttonText.style.alignItems = 'center';
  72.  
  73. const touchFeedback = document.createElement('yt-touch-feedback-shape');
  74. touchFeedback.style.borderRadius = 'inherit';
  75. touchFeedback.className = `${className}-feedback-shape`;
  76.  
  77. const touchFeedbackDiv = document.createElement('div');
  78. touchFeedbackDiv.className = `yt-spec-touch-feedback-shape yt-spec-touch-feedback-shape--touch-response ${className}-feedback-response`;
  79. touchFeedbackDiv.setAttribute('aria-hidden', 'true');
  80.  
  81. const strokeDiv = document.createElement('div');
  82. strokeDiv.className = `yt-spec-touch-feedback-shape__stroke ${className}-feedback-stroke`;
  83.  
  84. const fillDiv = document.createElement('div');
  85. fillDiv.className = `yt-spec-touch-feedback-shape__fill ${className}-feedback-fill`;
  86.  
  87. touchFeedbackDiv.appendChild(strokeDiv);
  88. touchFeedbackDiv.appendChild(fillDiv);
  89. touchFeedback.appendChild(touchFeedbackDiv);
  90. button.appendChild(svg);
  91. button.appendChild(buttonText);
  92. button.appendChild(touchFeedback);
  93. buttonViewModel.appendChild(button);
  94. containerDiv.appendChild(buttonViewModel);
  95.  
  96. return containerDiv;
  97. }
  98.  
  99. function createStatsButtons() {
  100. if (document.querySelector('.secret-stats-container') || document.querySelector('.stream-stats-container')) {
  101. return;
  102. }
  103.  
  104. const secretStatsSvgPath = "M224 16c-6.7 0-10.8-2.8-15.5-6.1C201.9 5.4 194 0 176 0c-30.5 0-52 43.7-66 89.4C62.7 98.1 32 112.2 32 128c0 14.3 25 27.1 64.6 35.9c-.4 4-.6 8-.6 12.1c0 17 3.3 33.2 9.3 48l-59.9 0C38 224 32 230 32 237.4c0 1.7 .3 3.4 1 5l38.8 96.9C28.2 371.8 0 423.8 0 482.3C0 498.7 13.3 512 29.7 512l388.6 0c16.4 0 29.7-13.3 29.7-29.7c0-58.5-28.2-110.4-71.7-143L415 242.4c.6-1.6 1-3.3 1-5c0-7.4-6-13.4-13.4-13.4l-59.9 0c6-14.8 9.3-31 9.3-48c0-4.1-.2-8.1-.6-12.1C391 155.1 416 142.3 416 128c0-15.8-30.7-29.9-78-38.6C324 43.7 302.5 0 272 0c-18 0-25.9 5.4-32.5 9.9c-4.8 3.3-8.8 6.1-15.5 6.1zm56 208l-12.4 0c-16.5 0-31.1-10.6-36.3-26.2c-2.3-7-12.2-7-14.5 0c-5.2 15.6-19.9 26.2-36.3 26.2L168 224c-22.1 0-40-17.9-40-40l0-14.4c28.2 4.1 61 6.4 96 6.4s67.8-2.3 96-6.4l0 14.4c0 22.1-17.9 40-40 40zm-88 96l16 32L176 480 128 288l64 32zm128-32L272 480 240 352l16-32 64-32z";
  105.  
  106. const streamStatsSvgPath = "M108.2 71c13.8 11.1 16 31.2 5 45C82.4 154.4 64 203 64 256s18.4 101.6 49.1 140c11.1 13.8 8.8 33.9-5 45s-33.9 8.8-45-5C23.7 386.7 0 324.1 0 256S23.7 125.3 63.2 76c11.1-13.8 31.2-16 45-5zm359.7 0c13.8-11.1 33.9-8.8 45 5C552.3 125.3 576 187.9 576 256s-23.7 130.7-63.2 180c-11.1 13.8-31.2 16-45 5s-16-31.2-5-45c30.7-38.4 49.1-87 49.1-140s-18.4-101.6-49.1-140c-11.1-13.8-8.8-33.9 5-45zM232 256a56 56 0 1 1 112 0 56 56 0 1 1 -112 0zm-27.5-74.7c-17.8 19.8-28.5 46-28.5 74.7s10.8 54.8 28.5 74.7c11.8 13.2 10.7 33.4-2.5 45.2s-33.4 10.7-45.2-2.5C129 342.2 112 301.1 112 256s17-86.2 44.8-117.3c11.8-13.2 32-14.3 45.2-2.5s14.3 32 2.5 45.2zm214.7-42.7C447 169.8 464 210.9 464 256s-17 86.2-44.8 117.3c-11.8 13.2-32 14.3-45.2 2.5s-14.3-32-2.5-45.2c17.8-19.8 28.5-46 28.5-74.7s-10.8-54.8-28.5-74.7c-11.8-13.2-10.7-33.4 2.5-45.2s33.4-10.7 45.2 2.5z";
  107.  
  108. const joinButton = document.querySelector('.yt-flexible-actions-view-model-wiz__action');
  109. if (joinButton) {
  110. const secretStatsButton = createStatsButton('Secret Stats', 'secret-stats', 'secret-stats-button', secretStatsSvgPath, 'secret');
  111. joinButton.parentNode.insertBefore(secretStatsButton, joinButton.nextSibling);
  112.  
  113. const streamStatsButton = createStatsButton('Stream Stats', 'stream-stats', 'stream-stats-button', streamStatsSvgPath, 'stream');
  114. secretStatsButton.parentNode.insertBefore(streamStatsButton, secretStatsButton.nextSibling);
  115. }
  116. }
  117.  
  118. function checkAndAddButtons() {
  119. const joinButton = document.querySelector('.yt-flexible-actions-view-model-wiz__action');
  120. const secretStatsButton = document.querySelector('.secret-stats-container');
  121. const streamStatsButton = document.querySelector('.stream-stats-container');
  122. if (joinButton && (!secretStatsButton || !streamStatsButton)) {
  123. createStatsButtons();
  124. }
  125. }
  126.  
  127. const observer = new MutationObserver((mutations) => {
  128. checkAndAddButtons();
  129. });
  130.  
  131. observer.observe(document.body, {
  132. childList: true,
  133. subtree: true
  134. });
  135.  
  136. checkAndAddButtons();
  137. console.log('YouTube Enhancer (Secret Stats) is running');
  138. })();