duifene-download

duifene 对分易课程资源下载

目前为 2024-06-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name duifene-download
  3. // @version 0.1.1
  4. // @description duifene 对分易课程资源下载
  5. // @author WiSHao
  6. // @match https://www.duifene.com/_FileManage/PC/StFileManage.aspx
  7. // @icon https://www.duifene.com/favicon.ico
  8. // @run-at document-body
  9. // @license ISC
  10. // @grant none
  11. // @namespace https://greasyfork.org/users/1103837
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. // 每100毫秒检查一次,确保要操作的元素真加载出来了,之后再执行某回调操作
  18. function waitForElement(selector, callback) {
  19. const interval = setInterval(function () {
  20. const element = document.querySelector(selector);
  21. if (element) {
  22. clearInterval(interval);
  23. callback(element);
  24. }
  25. }, 100);
  26. }
  27.  
  28. // 下载函数
  29. function download(url, name) {
  30. fetch(url)
  31. .then((response) => response.blob())
  32. .then((blob) => {
  33. const blobUrl = window.URL.createObjectURL(blob);
  34.  
  35. console.log(url.split('/'));
  36. // 提取 URL 中的文件名
  37. let filename = name || url.split('/').pop();
  38.  
  39. const a = document.createElement('a');
  40. a.href = blobUrl;
  41. a.download = filename;
  42. document.body.appendChild(a);
  43. a.click();
  44. document.body.removeChild(a);
  45. window.URL.revokeObjectURL(blobUrl); // 清理资源
  46. })
  47. .catch((e) => console.error('下载失败:', e));
  48. }
  49.  
  50. // 改所有禁止下载为下载的函数
  51. function todo(ele) {
  52. if (ele) console.log('所检查元素存在,如下:', ele);
  53.  
  54. const fileListNode = document.querySelectorAll('#FilesList > div');
  55. const fileListArray = Array.from(fileListNode);
  56. let noDownloading = false;
  57. fileListArray.forEach((item) => {
  58. // 获取带下载功能那块的div
  59. const actionDOM = item.querySelector('.action');
  60. if (!actionDOM || !item.dataset.path) {
  61. noDownloading = false;
  62. return;
  63. }
  64. noDownloading = setDownLoadButton(actionDOM, item.dataset.path);
  65. });
  66.  
  67. if (!noDownloading) {
  68. alert('没有禁止下载,不用改啦');
  69. return;
  70. }
  71. alert('确实有禁止下载,已解开');
  72. }
  73.  
  74. function setDownLoadButton(actionDOM, downloadPath) {
  75. // 遍历其中的所有元素
  76. const elements = actionDOM.querySelectorAll('*');
  77. for (let i = 0; i < elements.length; i++) {
  78. let element = elements[i];
  79. // 检查所有属性
  80. for (let j = 0; j < element.attributes.length; j++) {
  81. let attr = element.attributes[j];
  82. if (attr.value.includes('禁止')) {
  83. console.log(`找到属性包含'禁止'的元素`, element);
  84. element.innerHTML = '草泥马我就要下载';
  85. element.style.color = 'red';
  86. element.style.cursor = 'pointer';
  87. element.addEventListener('mouseenter', function () {
  88. element.style.textDecoration = 'underline';
  89. });
  90. element.addEventListener('mouseleave', function () {
  91. element.style.textDecoration = 'none';
  92. });
  93. element.addEventListener('click', () => download(downloadPath));
  94. return true;
  95. }
  96. }
  97. // 检查文本内容
  98. if (element.textContent.includes('禁止')) {
  99. console.log(`找到属性包含'禁止'的元素`, element);
  100. element.innerHTML = '草泥马我就要下载';
  101. element.style.color = 'red';
  102. element.style.cursor = 'pointer';
  103. element.addEventListener('mouseenter', function () {
  104. element.style.textDecoration = 'underline';
  105. });
  106. element.addEventListener('mouseleave', function () {
  107. element.style.textDecoration = 'none';
  108. });
  109. element.addEventListener('click', () => download(downloadPath));
  110. return true;
  111. }
  112. }
  113.  
  114. return false;
  115. }
  116.  
  117. // 批量下载函数
  118. function autoDownload() {
  119. try {
  120. const fileListNode = document.querySelectorAll('#FilesList > div');
  121. const fileListArray = Array.from(fileListNode);
  122. if (fileListArray.length === 0) {
  123. alert('没有可下载的文件');
  124. return;
  125. }
  126. alert('开始批量下载文件');
  127. fileListArray.forEach((item, index) => {
  128. // 获取带下载功能那块的div
  129. const actionDOM = item.querySelector('.action');
  130. if (!actionDOM || !item.dataset.path) {
  131. return;
  132. }
  133. setTimeout(() => {
  134. console.log(`${index}号文件已开始下载`);
  135. download(item.dataset.path);
  136. }, index * 1000);
  137. });
  138. } catch (error) {
  139. alert('批量下载出错', error);
  140. }
  141. }
  142.  
  143. // 点击重设所有下载的按钮
  144. function setDownLoad(ele) {
  145. ele.style.position = 'relative';
  146.  
  147. const btn = document.createElement('div');
  148. btn.textContent = '设置下载按钮';
  149. btn.style.width = '120px';
  150. btn.style.height = '30px';
  151. btn.style.borderRadius = '5px';
  152. btn.style.lineHeight = '30px';
  153. btn.style.textAlign = 'center';
  154. btn.style.fontSize = '14px';
  155. btn.style.fontWeight = 'bold';
  156. btn.style.backgroundColor = '#8CF9FB';
  157. btn.style.color = 'white';
  158. btn.style.cursor = 'pointer';
  159. btn.style.position = 'absolute';
  160. btn.style.left = '200px';
  161. btn.addEventListener('mouseenter', function () {
  162. btn.style.backgroundColor = '#4BC4D0';
  163. });
  164. btn.addEventListener('mouseleave', function () {
  165. btn.style.backgroundColor = '#8CF9FB';
  166. });
  167. ele.appendChild(btn);
  168. btn.addEventListener('click', () => todo(ele));
  169. }
  170.  
  171. // 批量下载按钮
  172. function allDownLoad(ele) {
  173. const btn = document.createElement('div');
  174. btn.textContent = '批量下载文件';
  175. btn.style.width = '120px';
  176. btn.style.height = '30px';
  177. btn.style.borderRadius = '5px';
  178. btn.style.lineHeight = '30px';
  179. btn.style.textAlign = 'center';
  180. btn.style.fontSize = '14px';
  181. btn.style.fontWeight = 'bold';
  182. btn.style.backgroundColor = '#8CF9FB';
  183. btn.style.color = 'white';
  184. btn.style.cursor = 'pointer';
  185. btn.style.position = 'absolute';
  186. btn.style.left = '340px';
  187. btn.addEventListener('mouseenter', function () {
  188. btn.style.backgroundColor = '#4BC4D0';
  189. });
  190. btn.addEventListener('mouseleave', function () {
  191. btn.style.backgroundColor = '#8CF9FB';
  192. });
  193. ele.appendChild(btn);
  194. btn.addEventListener('click', autoDownload);
  195. }
  196.  
  197. // 页面加载完毕后执行入口
  198. window.addEventListener('load', () => {
  199. waitForElement('.btnGroup', setDownLoad);
  200. waitForElement('.btnGroup', allDownLoad);
  201. waitForElement('#FilesList > div:nth-child(1) > div.fileName > a', todo);
  202. });
  203. })();