Twitch Multi-Redirect, but for streamsniper.tv

Redirect Twitch with options

当前为 2020-11-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitch Multi-Redirect, but for streamsniper.tv
  3. // @namespace RexOmni
  4. // @version 1.2
  5. // @description Redirect Twitch with options
  6. // @author RexOmniFurtim
  7. // @match https://www.twitch.tv/*
  8. // @exclude https://www.twitch.tv/directory/*
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @run-at document-start
  12. // @noframes
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // if not top frame, stop
  19. if (frameElement) {
  20. return;
  21. }
  22.  
  23. ///////////
  24. // SETUP //
  25. ///////////
  26. let tmr = { };
  27. tmr.intervals = [];
  28. tmr.elements = [];
  29. tmr.options = [
  30. 'TwitchLS'
  31. ];
  32. tmr.redirects = { // Redirect functions
  33. 'TwitchLS' : ()=> { window.location.replace('https://www.streamsniper.tv/live/t/' + tmr.channel); }
  34. };
  35.  
  36. tmr.settings = { };
  37. // load settings
  38. tmr.settings.redirectTarget = GM_getValue("tmr-redirectTarget", 'TwitchLS');
  39. tmr.settings.redirectTimer = GM_getValue("tmr-redirectTimer", 0);
  40.  
  41. tmr.channel = window.location.pathname.substring(1, window.location.pathname.length);
  42.  
  43. ///////////
  44. // START //
  45. ///////////
  46.  
  47. // redirect immediately if redirect set to 0
  48. if(tmr.settings.redirectTimer === 0 && shouldRedirect()){
  49. console.log('redirecting early');
  50. redirect();
  51. return;
  52. }
  53. else{
  54. document.addEventListener("DOMContentLoaded", ()=> {
  55. createBlock();
  56. // If the url says not to redirect, stop the redirect
  57. if( !urlParamRedirect() ){
  58. tmr.elements.container.click();
  59. }
  60. });
  61. }
  62.  
  63. ///////////////
  64. // FUNCTIONS //
  65. ///////////////
  66.  
  67. // redirect to the target
  68. function redirect(){
  69. tmr.redirects[tmr.settings.redirectTarget]();
  70. }
  71.  
  72. function setRedirectTimeout(){
  73. tmr.intervals.redirect = setTimeout(()=>{
  74.  
  75. // if the redirect target function is found is found
  76. if( tmr.redirects[tmr.settings.redirectTarget] !== null ){
  77.  
  78. // let the user know navigation has begun
  79. tmr.elements.warning.style.background = 'black';
  80.  
  81. // activate naviagtion function
  82. redirect();
  83. }
  84. else{
  85. console.log("TMR: redirection target funciton not found");
  86. cancelRedirect();
  87. }
  88. }, tmr.settings.redirectTimer);
  89. }
  90.  
  91. function cancelRedirect(){
  92. clearTimeout(tmr.intervals.redirect);
  93. tmr.intervals.redirect = null;
  94.  
  95. tmr.elements.container.onclick = null;
  96. console.log('%cStopping Redirect', 'background: #3333ff; color: #fff; font-size: 22px;');
  97.  
  98. tmr.elements.warning.style.display = 'none';
  99. tmr.elements.container.style.display = 'none';
  100. tmr.elements.settings.style.display = 'block';
  101.  
  102. addGear();
  103. }
  104.  
  105. function createBlock(){
  106. // create and add div
  107. tmr.elements.container = document.createElement('div');
  108. tmr.elements.container.innerHTML = ' <div style="width:100%;height:100%;position:absolute;background:rgba(255,0,0,0.5);z-index:99999;text-align:center;cursor:pointer;"> <div style="background:white;padding: 2rem;margin: 2rem;"> <p style="font-size:6rem;">Twitch Multi-Redirect</p> <hr style="margin-top: 3rem;"> <div id="TMR-Warning" style="margin-top:2rem;display:block;"> <p style="font-size: 2rem; background: rgba(0,0,255,.4); padding-bottom: 0.5rem;">Click anywhere to stop redirecting</p> </div> <div id="TMR-Settings" style="margin-top:2rem;display:none"> <p style="font-size:4rem;background:rgba(0,125,125,.5);padding-bottom:2rem;padding-top:1rem;">Settings</p> <div style="width: 100%;margin-top: 2rem;"> <span style="padding-right:.7rem">Redirection target</span><select id="TMR-Select"> </select><br><span style="padding-right:.7rem">Wait (ms)</span><input type=Number value=850 id="TMR-Timer"/> <br> <input id="TMR-Savebtn" type="button" value="Save"> </div> </div> </div> </div>';
  109. document.body.insertBefore(tmr.elements.container, document.body.firstChild);
  110.  
  111. tmr.elements.warning = document.getElementById("TMR-Warning");
  112. tmr.elements.settings = document.getElementById("TMR-Settings");
  113. tmr.elements.select = document.getElementById("TMR-Select");
  114. tmr.elements.timer = document.getElementById('TMR-Timer');
  115. tmr.elements.savebtn = document.getElementById('TMR-Savebtn');
  116.  
  117. // populate the drop-down, select
  118. let numOptions = tmr.options.length;
  119. for (let i = 0; i < numOptions; i++) {
  120. let option = document.createElement("option");
  121. option.text = tmr.options[i];
  122. option.value = tmr.options[i];
  123. //option.setAttribute("id", "TMR-" + tmr.options[i]);
  124. tmr.elements.select.add(option);
  125. }
  126.  
  127. // set the setting elements to setting values
  128. // TODO: Get the value of select to equal the right element
  129. //tmr.elements.select.value = document.getElementById("TMR-" + tmr.settings.redirectTarget);
  130. //console.log(document.getElementById("TMR-" + tmr.settings.redirectTarget));
  131. tmr.elements.select.selectedIndex = tmr.options.indexOf(tmr.settings.redirectTarget);
  132. tmr.elements.timer.value = Number(tmr.settings.redirectTimer);
  133.  
  134. // save the selection
  135. tmr.elements.savebtn.onclick = ()=>{
  136. //save the data
  137. GM_setValue("tmr-redirectTarget", tmr.elements.select.value);
  138. GM_setValue("tmr-redirectTimer", tmr.elements.timer.value);
  139. tmr.elements.container.style.display = 'none';
  140. };
  141.  
  142. // start the redirect Timeout
  143. setRedirectTimeout();
  144.  
  145. // on click anywhere stop redirect
  146. tmr.elements.container.onclick = ()=>{cancelRedirect();};
  147. }
  148.  
  149. // Return the redirect url param as bool
  150. function urlParamRedirect(){
  151. let pathArray = /redirect=([^&]+)/.exec(window.location.href);
  152.  
  153. // deault behavior
  154. if (pathArray === null){
  155. return true;
  156. }
  157.  
  158. return ( pathArray[1] === '0' ) ? false : true;
  159. }
  160.  
  161. // Returns whether the page should redirect
  162. // Keep for inclusion of other checks when they are ported.
  163. function shouldRedirect(){
  164. return urlParamRedirect();
  165. }
  166.  
  167. // add the gear when the hcat popup is available
  168. function addGear(){
  169. // wait until chat popup available
  170. let gearInterval = setInterval(()=>{
  171. let chatpopup = document.getElementsByClassName('chat-settings chat-menu dropmenu');
  172. if(chatpopup.length > 0){
  173. clearInterval(gearInterval);
  174. gearInterval = null;
  175.  
  176. tmr.elements.gear = document.createElement('div');
  177. tmr.elements.gear.innerHTML = '<div style="cursor:pointer;background: rgba(237,237,237,0.7);padding: 0.5rem 10rem 0.2rem 1.3rem;"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAMAAAC8EZcfAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAeUExURQAAADAwMDg4ODAwMC8vLzAwMDMzMzAwMDAwMDMzM5MpzFMAAAAJdFJOUwAEAeiXxSFuRDq7DUMAAAX9SURBVHja3V3r0uQoCI0oat7/hdeYvqS/joZLR5n1x9RU7SZ9AhxEQGZZ9AuWnEJAv+7Le48YQsrlP9hYsOB6shCMIITF+TOAPpoBGNfTZUXHbsktgM4IwHQOMJkBGM4BBiMAGyQuNLZhg4Uj/hygdyYQNjmyrtEIwNQCaIPGTY5YoXGTI0Zo3NroaDQG7RdAWcKNjkLj8nIAHT7nLtTUIfFluFDenVWuqLwAsfwBMhJf0LioN+IanNxQ6wtWn7sIOyTu07i8tH4aRiHC8oF5N/8A7VcU6NgG2KbxLr7dEPoS6OB76W77SOBudDuNXVN82R8FDRp85SNTwyFAjyNb1A9nP12ecuGPKQAb36dphROubD/e5chO4y+Mn+J72gKo8H1bSv1ZiAk7Gi5PYYrl/3OHR7/EJ0AIp9Q8qrn+raBbCatgXN5idO5LfGyE0HAdL65U3dLQPZ7c/HEV+an4uBt307Vtat5/KQa/sham+mARnz606Nn9zpUYVv7yBWKR+ypz69S9qyhLBm9XQOybBSnAha5jK7/CVS5nEY76/Z3h7kXIlsCC68R1TZS+Bd6+SDoO8/BReDzTCGme0F3QeCpFrmPkew2QtpWUfRbtKniekpFxAJ2hZFZGe4aSeYmm8UrmpnFGbyjITjAADFUy//A+dkORJBJHKlmWRxwXeAmT7eOULE1lj/I14kx2L7k73wJH0kSo4nE2iDKSDIwYRFbYy96b8DNDQy6BCEcKUNTeMDhmZYtw9OGTTWSR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P1xiko4uqxoI8eLxXgB5lRSqXJHhe1Z70+UQw+6MJML8GCcfQVgQxuuv651hCJsXaEYkAtAGwPysgCRBqMnEGpn43D7nG5mZ3aaxkXHFbRonKwDzLI5oAUbjA5+NzHtu36VFK5P5W+GClZnjTT+T7ABMDRKbUXG2zJEmjdHQv17hvGWO1A4MX9ZTtY9//+Mnbvo/TV/m1yK+SaAAAAAASUVORK5CYII=" style=" width: 15%;"> <span style="">TMR Settings</span> </div>';
  178.  
  179. chatpopup[0].insertBefore(tmr.elements.gear, chatpopup[0].firstChild);
  180.  
  181. // Gear onclick function;
  182. tmr.elements.gear.addEventListener('click', ()=>{
  183. tmr.elements.container.style.display = 'block';
  184. });
  185. }
  186. }, 100);
  187. }
  188.  
  189. console.log('%c AutotwitchLS loaded! ', 'background: #333; color: #fff; font-size: 2em;');
  190. })();