Twitch Auto Channel Points

auto get Channel Points

目前为 2021-11-13 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Twitch Auto Channel Points
  3. // @name:zh-TW Twitch 自動獲得忠誠點數
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.4
  6. // @description auto get Channel Points
  7. // @description:zh-tw 自動獲得忠誠點數
  8. // @author Long
  9. // @match https://www.twitch.tv/*
  10. // @icon https://www.google.com/s2/favicons?domain=twitch.tv
  11. // @grant GM_addStyle
  12. // @grant GM_setValue
  13. // @grant GM_getValue
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. "use strict";
  18.  
  19. GM_addStyle(`
  20. #total-got-points {
  21. display: inline-flex;
  22. position: relative;
  23. align-items: center;
  24. padding : 0 var(--button-padding-x) 0 var(--button-padding-x);
  25. text-decoration: none;
  26. white-space: nowrap;
  27. user-select: none;
  28. color: var(--color-text-alt-2);
  29. font-weight: var(--font-weight-semibold);
  30. border-radius: var(--border-radius-medium);
  31. font-size: var(--button-text-default);
  32. height: var(--button-size-default);
  33. --point-name: '自動領取忠誠點數';
  34. }
  35.  
  36. #total-got-points:hover {
  37. background-color: var(--color-background-button-text-hover);
  38. }
  39.  
  40. #total-got-points img {
  41. height: 2rem;
  42. width: 2rem;
  43. }
  44.  
  45. #total-got-points img, #total-got-points svg {
  46. margin-right: 5px;
  47. }
  48.  
  49. #total-got-points::before{
  50. content: var(--point-name);
  51. user-select: none;
  52. color: var(--color-text-tooltip);
  53. background-color: var(--color-background-tooltip);
  54. font-size: var(--font-size-6);
  55. border-radius: 0.4rem;
  56. padding: 0.5rem;
  57. max-width: 30rem;
  58. font-weight: var(--font-weight-semibold);
  59. right: 0;
  60. line-height: 1.2;
  61. pointer-events: none;
  62. position: absolute;
  63. text-align: left;
  64. white-space: nowrap;
  65. z-index: 2000;
  66. top: 50%;
  67. transform: translate(calc(100% + 5px), -50%);
  68. transition: all .2s ease-in-out;
  69. opacity: 0;
  70. }
  71.  
  72. #total-got-points:hover::before{
  73. opacity: 1;
  74. }
  75. `);
  76.  
  77. // find button
  78. let findButtonRetry = 100;
  79. // get points
  80. let getPointsLoop = 1000;
  81. let pointsOneTime = 50;
  82. let GMkeyName = location.origin + location.pathname + ':auto-channel-points';
  83. let totalGotPoints = GM_getValue(GMkeyName, 0);
  84. let beforePoints = 0;
  85. console.log("[Twitch-Auto-Channel-Points] totalGotPoints: ", totalGotPoints);
  86.  
  87.  
  88. let getPoints = function() {
  89. // console.debug('[Twitch-Auto-Channel-Points] get points');
  90. let $pointsSummaryButtons = document.querySelectorAll('[data-test-selector="community-points-summary"] button');
  91. let canGetPoints = $pointsSummaryButtons.length > 1;
  92. if(canGetPoints) {
  93. beforePoints = document.querySelector('.chat-input__buttons-container button').innerText;
  94. $pointsSummaryButtons[1].click();
  95. $pointsSummaryButtons[1].remove();
  96. totalGotPoints += pointsOneTime;
  97. GM_setValue(GMkeyName, totalGotPoints);
  98. showTotalGotPoints();
  99. console.log("[Twitch-Auto-Channel-Points] Got points!!");
  100. console.log("[Twitch-Auto-Channel-Points] totalGotPoints: ", totalGotPoints);
  101. }
  102. };
  103.  
  104. let showTotalGotPoints = function() {
  105. let $totalGotPoints = document.querySelector('#total-got-points span');
  106. if(!$totalGotPoints) {
  107. let $totalGotPointsContainer = document.createElement('div');
  108. $totalGotPointsContainer.id = 'total-got-points';
  109. let $buttonDiv = document.querySelectorAll('.chat-input__buttons-container div')[0];
  110. $buttonDiv.appendChild($totalGotPointsContainer);
  111. let $pointImage = document.querySelector('.channel-points-icon__image');
  112. if($pointImage) {
  113. let pointName = $pointImage.alt;
  114. $totalGotPointsContainer.style.setProperty('--point-name', '"自動領取' + pointName + '"');
  115. $totalGotPointsContainer.appendChild($pointImage.cloneNode(true));
  116. } else {
  117. $totalGotPointsContainer.appendChild(document.querySelector('.chat-input__buttons-container button svg').cloneNode(true));;
  118. }
  119. $totalGotPointsContainer.appendChild(document.createElement('span'));
  120. $totalGotPoints = document.querySelector('#total-got-points span');
  121. }
  122. $totalGotPoints.textContent = new Intl.NumberFormat().format(totalGotPoints);;
  123. };
  124.  
  125. let findButtonInterval = setInterval(function () {
  126. // console.debug('[Twitch-Auto-Channel-Points] find button...');
  127. let $pointButton = document.querySelector('[data-test-selector="community-points-summary"] button');
  128. if ($pointButton) {
  129. setInterval(getPoints, getPointsLoop);
  130. showTotalGotPoints();
  131. clearInterval(findButtonInterval);
  132. console.log("[Twitch-Auto-Channel-Points] found point-button. start get points");
  133. } else if (!findButtonRetry) {
  134. clearInterval(findButtonInterval);
  135. console.warn("[Twitch-Auto-Channel-Points] point-button not found.");
  136. }
  137. findButtonRetry--;
  138. }, 500);
  139. })();
  140.