HUYA Stream URL

获取虎牙直播流地址

  1. // ==UserScript==
  2. // @name HUYA Stream URL
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 获取虎牙直播流地址
  6. // @author 游客
  7. // @include http://www.huya.com/*
  8. // @include https://www.huya.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. function getElementsByClassName(className){
  15. var elems = [];
  16. if(!document.getElementsByClassName){
  17. var dom = document.getElementsByTagName("*");
  18. for(var i =0 ;i<dom.length;i++){
  19. if(dom[i].className){
  20. var classs = dom[i].className.split(" ");
  21. for(var c = 0;c<classs.length;c++){
  22. if(classs[c]==className){
  23. elems.push(dom[i]);
  24. }
  25. }
  26. }
  27. }
  28. }else{
  29. var dom1 = document.getElementsByClassName(className);
  30. for(var m =0 ;m<dom1.length;m++){
  31. elems.push(dom1[m]);
  32. }
  33. }
  34. return elems;
  35. }
  36.  
  37. function deleteInvalidChar(fn){
  38. //:< > / | : " * ?
  39. fn=fn.replace(":","");
  40. fn=fn.replace("<","");
  41. fn=fn.replace(">","");
  42. fn=fn.replace("/","");
  43. fn=fn.replace("\"","");
  44. fn=fn.replace("*","");
  45. fn=fn.replace("?","");
  46. fn=fn.replace("\\","");
  47. return fn;
  48. }
  49.  
  50. var title=deleteInvalidChar(document.getElementById("J_roomTitle").title);
  51. var name=deleteInvalidChar(document.getElementsByClassName('host-name')[0].title);
  52. var roomid=document.getElementsByClassName('host-rid')[0].innerText;
  53. var strNIT=name+'-'+roomid+'-'+title;
  54. //alert(strNIT);
  55.  
  56. function heredoc(fn) {
  57. return fn.toString().split('\n').slice(1,-1).join('\n') + '\n'
  58. }
  59.  
  60. var boxHtml = '';
  61.  
  62. if(window.TT_ROOM_DATA.state==='OFF'){
  63. boxHtml = '状态: 未开播<br>';
  64. }
  65.  
  66. if(window.TT_ROOM_DATA.state==='ON'){
  67. boxHtml = '状态: 正在直播 目前 tx 可用<br>';
  68. boxHtml += '<div class="flv-url-item"><label>信息</label>';
  69. boxHtml += '<input id="flv-url-'+0+'" value="'+strNIT+'"/>'
  70. boxHtml += '<a onclick="copyFlvUrl(0)">复制</a></div>';
  71. try{
  72. var streamInfoList = window.hyPlayerConfig.stream.data[0].gameStreamInfoList;
  73. var streamInfo = null;
  74. var index = 10;
  75. for(var i=0; i<streamInfoList.length; i++){
  76. console.log(streamInfoList[i]);
  77. streamInfo = streamInfoList[i];
  78. //if(streamInfoList[i].sCdnType==='TX' || streamInfoList[i].sCdnType==='JS')
  79. {
  80. var url = streamInfo.sFlvUrl + '/' + streamInfo.sStreamName + '.' + streamInfo.sFlvUrlSuffix + '?' + streamInfo.sFlvAntiCode;
  81. var ratioList = window.hyPlayerConfig.stream.vMultiStreamInfo;
  82.  
  83. for(var j=0; j<ratioList.length; j++){
  84. var label = ratioList[j].sDisplayName;
  85. var absUrl = url;
  86. if(ratioList[j].iBitRate != 0){
  87. absUrl = url+"&ratio="+ratioList[j].iBitRate;
  88. }
  89. boxHtml += '<div class="flv-url-item"><label>'+label+'</label>';
  90. boxHtml += '<input id="flv-url-'+index+'" value="'+absUrl+'"/>'
  91. boxHtml += '<a onclick="copyFlvUrl('+index+')">复制</a></div>';
  92. //<a onclick="openFlvUrl('+i+')">VLC播放</a>
  93. index++;
  94. }
  95. }
  96. }
  97. }catch(e){
  98. boxHtml += '解析流数据错误';
  99. console.error(e);
  100. }
  101. }
  102.  
  103. if(window.TT_ROOM_DATA.state==='REPLAY'){
  104. boxHtml = '状态: 重播<br>';
  105. boxHtml += '当前处于重播状态,不能解析流数据';
  106. }
  107.  
  108.  
  109. window.toggleFlvUrlBox = function() {
  110. var flvUrlBoxBtn = document.getElementById('flv-url-box-btn');
  111. var flvUrlBox = document.getElementById('flv-url-box');
  112. if(flvUrlBox.style.display==='none'){
  113. flvUrlBox.style.display='block';
  114. }else{
  115. flvUrlBox.style.display='none';
  116. }
  117. }
  118. window.copyFlvUrl = function(index) {
  119. var input = document.getElementById('flv-url-'+index);
  120. input.select();
  121. document.execCommand("Copy");
  122. }
  123. window.openFlvUrl = function(index) {
  124. var input = document.getElementById('flv-url-'+index);
  125. var url = input.value;
  126. location.href = "vlc://"+url
  127. }
  128. var wrapper = document.createElement("div");
  129. wrapper.style.display = 'inline-block';
  130. wrapper.innerHTML = heredoc(function(){/*
  131. <style>
  132. #flv-url-box-btn {
  133. width: 32px;
  134. height: 32px;
  135. cursor: pointer;
  136. background-color: #ffffff;
  137. top: 9px;
  138. right: 40px;
  139. position: fixed;
  140. z-index: 1000000;
  141. border-radius: 4px;
  142. border: 0px solid #cccccc;
  143. }
  144. #flv-url-box-btn:hover{
  145. box-shadow: 0 0 8px #0ca4d4;
  146. }
  147. #flv-url-box {
  148. top: 54px;
  149. right: 40px;
  150. border: 1px solid #CCC;
  151. border-radius: 6px;
  152. background-color: #ffffff;
  153. padding: 8px;
  154. position: fixed;
  155. z-index: 1000000;
  156. }
  157. #flv-url-box .flv-url-item{
  158. margin: 4px 0;
  159. }
  160. #flv-url-box .flv-url-item>*{
  161. border: 1px solid #DDD;
  162. margin-left: -1px;
  163. vertical-align: top;
  164. }
  165. #flv-url-box .flv-url-item>*:first-child{
  166. border-top-left-radius: 6px;
  167. border-bottom-left-radius: 6px;
  168. margin-left: 0;
  169. }
  170. #flv-url-box .flv-url-item>*:last-child{
  171. border-top-right-radius: 6px;
  172. border-bottom-right-radius: 6px;
  173. }
  174. #flv-url-box .flv-url-item input{
  175. top 0;
  176. width: 180px;
  177. height: 24px;
  178. padding: 0 4px;
  179. color: #999;
  180. }
  181. #flv-url-box .flv-url-item input:focus{
  182. outline: none;
  183. border-color: #0ca4d4;
  184. }
  185. #flv-url-box .flv-url-item a{
  186. user-select: none;
  187. padding: 0 4px;
  188. cursor: pointer;
  189. font-size: 12px;
  190. line-height: 24px;
  191. height: 24px;
  192. display: inline-block;
  193. background-color: #ffffff;
  194. color: #888;
  195. }
  196. #flv-url-box .flv-url-item label{
  197. user-select: none;
  198. text-align: center;
  199. width: 60px;
  200. font-size: 12px;
  201. line-height: 24px;
  202. height: 24px;
  203. display: inline-block;
  204. background-color: #ffffff;
  205. color: #888;
  206. }
  207. #flv-url-box .flv-url-item a:hover{
  208. border-color: #0ca4d4;
  209. color: #0ca4d4;
  210. }
  211. </style>
  212. <div>
  213. <img id="flv-url-box-btn" onclick="toggleFlvUrlBox()" src="https://huyaimg.msstatic.com/cdnimage/actprop/20279_1__45_1532856204.jpg" />
  214. <div id="flv-url-box" style="display: none;">__box_html__</div>
  215. <div>
  216. */}).replace('__box_html__', boxHtml);
  217.  
  218. document.body.append(wrapper);
  219. })();