网易云下载

网易云音乐下载

  1. // ==UserScript==
  2. // @name 网易云下载
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 网易云音乐下载
  6. // @author You
  7. // @match *://music.163.com/*
  8. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js
  9.  
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. ; (() => {
  14. let flag;
  15. let timer = null;
  16.  
  17. const down = () => {
  18. // console.clear();
  19. let dom = document.getElementById('downloadMusic')
  20. if (dom) return;
  21. const iframe = document.getElementById('g_iframe')?.contentWindow?.document || top.frames[0];
  22. const table = iframe.body.querySelectorAll('.m-table')[0]
  23. const tBody = table.tBodies[0];
  24. const rows = Array.from(tBody.rows);
  25. flag = true;
  26. clearTimeout(timer);
  27. const style = document.createElement('style');
  28. style.innerHTML = `
  29. tr .left{
  30. padding: 0;
  31. }
  32. tr .left .hd {
  33. text-align: center;
  34. }
  35. #downloadMusic{
  36. cursor: pointer;
  37. }
  38. #downloadMusic:hover{
  39. text-decoration: underline;
  40. color: blue;
  41. }
  42. `;
  43. iframe.head.appendChild(style);
  44. rows.forEach(row => {
  45. const playBox = row.cells[0].children[0];
  46. const span = document.createElement('span');
  47. span.id = 'downloadMusic';
  48.  
  49. const musicName = row.cells[1].innerHTML.match(/(?<=title=")[^"]+/)[0].replaceAll('&nbsp;', ' ');
  50. let id = row.cells[1].innerHTML.match(/(?<=song\?id\=)\d+/)[0]
  51.  
  52. span.setAttribute('data', id)
  53.  
  54. span.setAttribute('musicName', musicName);
  55. span.innerHTML = '下载';
  56.  
  57. playBox.appendChild(span);
  58.  
  59. })
  60.  
  61. tBody.addEventListener('click', (e) => {
  62. const target = e.target || e.srcElement;
  63. if (target.id === 'downloadMusic') {
  64.  
  65. $.ajax(`/song/media/outer/url?id=${target.getAttribute('data')}`, {
  66. xhrFields: { responseType: "blob" },
  67. success(data) {
  68. try {
  69. const a = document.createElement('a');
  70. a.download = target.getAttribute('musicName');
  71. a.href = URL.createObjectURL(data);
  72. a.click();
  73. setTimeout(() => {
  74. a.remove();
  75. }, 1000);
  76.  
  77. } catch (e) {
  78. target.innerHTML = '失败';
  79. }
  80. }
  81. })
  82. }
  83. })
  84. }
  85.  
  86. function fn() {
  87. clearTimeout(timer);
  88. if (!flag) {
  89.  
  90. try { down() } catch { }
  91.  
  92. clearTimeout(timer);
  93. let dom = document.getElementById('downloadMusic')
  94. if (!dom) timer = setTimeout(fn, 2000)
  95. } else {
  96. flag = true;
  97. }
  98. }
  99.  
  100. window.addEventListener('load', () => {
  101. const iframe = top.document.getElementById('g_iframe')?.contentWindow?.document || top.frames[0];
  102. const search = location.search.match(/id=\d+/)
  103. if(search){
  104. const box = iframe.getElementsByClassName('hd')[0]
  105. const id = search[0].replace('id=','')
  106. let div = document.createElement('div')
  107. div.innerHTML = '下载'
  108. div.style.cssText = `
  109. float: right;
  110. cursor: pointer;
  111. position: relative;
  112. top: -2em;
  113. font-size: 18px;
  114. border: 1px solid #ccc;
  115. padding: 2px 10px;
  116. border-radius: 5px;
  117. transition: all .5s;
  118. `
  119. div.onmouseenter = function(){
  120. this.style.background = 'pink'
  121. this.style.color = '#fff'
  122. }
  123. div.onmouseleave = function(){
  124. this.style.background = '#fff'
  125. this.style.color = '#000'
  126. }
  127. div.onclick = function(){
  128. $.ajax(`/song/media/outer/url?id=${id}`, {
  129. xhrFields: { responseType: "blob" },
  130. success(data) {
  131. try {
  132. const a = document.createElement('a');
  133. a.download = box.getElementsByTagName('em')[0].innerText;
  134. a.href = URL.createObjectURL(data);
  135. a.click();
  136. setTimeout(() => {
  137. a.remove();
  138. }, 1000);
  139.  
  140. } catch (e) {
  141. div.innerHTML = '失败';
  142. }
  143. }
  144. })
  145. }
  146. box.appendChild(div)
  147. return;
  148. }
  149.  
  150. let dom = document.getElementById('downloadMusic')
  151. if (!dom) fn();
  152. window.addEventListener('hashchange', () => {
  153. let [timer1,timer2,timer3,timer4] = [null,null,null,null]
  154. const change = () => {
  155. let dom = document.getElementById('downloadMusic')
  156. console.log(dom);
  157. clearTimeout(timer1)
  158. clearTimeout(timer2)
  159. clearTimeout(timer3)
  160. clearTimeout(timer4)
  161. if (!dom) {
  162. flag = false;
  163. fn();
  164. }
  165. }
  166. timer1 = setTimeout(change, 1500)
  167. timer2 = setTimeout(change, 3000)
  168. timer3 =setTimeout(change, 5000)
  169. timer4 =setTimeout(change, 1000)
  170. })
  171. });
  172.  
  173. })()

QingJ © 2025

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