MyDownloader

包含多种下载方法的下载库

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

  1. // ==UserScript==
  2. // @name MyDownloader
  3. // @version 2025.02.27
  4. // @description 包含多种下载方法的下载库
  5. // @author You
  6. // @grant none
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @grant GM_deleteValue
  10. // @grant GM_download
  11. // @require https://update.gf.qytechs.cn/scripts/480132/1476440/Get_all_img_Library.js
  12. // @require https://update.gf.qytechs.cn/scripts/522187/1511410/Kquery.js
  13. // ==/UserScript==
  14. // 2025.01.07 更新了download text
  15. // 2025.01.07.02 更新了download text(src,name)
  16. // 2025.01.07.03 修复了download text(src,name)没有传入name的bug
  17.  
  18.  
  19. /**
  20. * 包含多种下载方法的下载类
  21. * @example
  22. const downloader = new Downloader();
  23. downloader.Download_img(imgs);
  24. */
  25. function Downloader(){
  26. let downloading = 0;
  27. let downloaded = [];
  28. let downloadError = [];
  29. let imgs = null;
  30. let maxDownloadingCounts = 10;
  31. let timeout = null;
  32. let downloadType = ""
  33. let checkSrc = false;
  34. /**
  35. * @example Download_img(imgs)
  36. */
  37. this.Download_img = async (imgs)=>{
  38. Set_download(imgs)
  39. if(downloadType==""){
  40. Download_obo("GM_download");return;
  41. }
  42.  
  43. console.log(downloadType);
  44. if(downloadType=="GM_download"){
  45. Download_obo("GM_download");
  46. }else if(downloadType=="atag"){
  47. Download_obo("atag");
  48. console.log('download atag')
  49. }else if(downloadType=="blob"){
  50. Download_obo("blob");
  51. }else{
  52. alert("no this donwload type");
  53. }
  54. }
  55.  
  56. this.Set_downloadType = v => downloadType = v;
  57.  
  58. this.Set_maxDownloadingCounts = v => maxDownloadingCounts = v;
  59.  
  60. this.Set_timeout = v => timeout = v;
  61. this.Set_checkSrc = v => checkSrc = v;
  62. /**
  63. * @example OneSuccess((success_img)=>{...})
  64. */
  65. function OneSuccess(img){}
  66. this.OneSuccess = foo=>OneSuccess = foo;
  67. /**
  68. * @example OneError((error_img)=>{...})
  69. */
  70. function OneError(img){}
  71. this.OneError = foo=>OneError = foo;
  72. /**
  73. * @example AllComplete(()=>{...})
  74. */
  75. function AllComplete(){}
  76. this.AllComplete = foo=>AllComplete = foo;
  77. this.Set_download = (iimgs)=>{Set_download(iimgs);}
  78. function Set_download(iimgs){
  79. downloaded = [];
  80. downloading = 0;
  81. downloadError = [];
  82. imgs = iimgs;
  83. }
  84. function Download_obo(dtype){
  85. async function Download_one(i){
  86. if(i>=imgs.length){AllComplete();return;}
  87. if(downloading>=maxDownloadingCounts){setTimeout(()=>{Download_one(i)},1000);return;}
  88. const img = imgs.eq(i);
  89. downloading++;
  90. function success(img){
  91. OneSuccess(img);
  92. downloading--;
  93. downloaded.push(img);
  94. ConsoleWrite((downloaded.length+downloadError.length)+"/"+imgs.length)
  95. }
  96. function error(img){
  97. OneError(img);
  98. downloading--;
  99. downloadError.push(img);
  100. }
  101. switch(dtype){
  102. case "GM_download":
  103. Donwload_img_by_GM(img)
  104. .then(img=>success(img))
  105. .catch(img=>{
  106. dtype = "blob"
  107. Download_one(i);
  108. });
  109. break;
  110. case "blob":
  111. Donwload_img_by_blob(img)
  112. .then(img=>success(img))
  113. .catch(img=>{
  114. dtype = "atagIfram";
  115. Download_one(i);
  116. });
  117. break;
  118. case "atagIfram":
  119. Donwload_img_by_atag(img)
  120. .then(img=>success(img))
  121. .catch(img=>{
  122. error(img);
  123. dtype = "GM_download";
  124. });
  125. break;
  126. }
  127. Download_one(++i);
  128. }
  129. Download_one(0);
  130. }
  131. function ConsoleWrite(mass){
  132. if(window.GAIL){
  133. window.GAIL.showmass(mass);
  134. }
  135. }
  136. this.ConsoleWrite = foo=>ConsoleWrite = foo;
  137. this.Donwload_img_by_GM = img=>{Donwload_img_by_GM(img);}
  138. function Donwload_img_by_GM(img){
  139. return new Promise(async(resolve,reject)=>{
  140. if(!img){alert("img is empty");reject(img);return;}
  141. if(!GM_download){alert("GM_download is undefind");reject(img);return;}
  142. let name = '';
  143. let src = '';
  144. try{
  145. await Check_and_get_nameAndsrc({img:img,checkSrc:checkSrc})
  146. .then((m)=>{
  147. name = m.name;
  148. src = m.src;
  149. console.log(m);
  150. });
  151. }catch(error){
  152. reject(img);
  153. return;
  154. }
  155. console.log(name)
  156. console.log(src)
  157. GM_download({
  158. url:src,
  159. name:name,
  160. onload:function(){
  161. resolve(img);
  162. },
  163. onerror:function(){
  164. reject(img);
  165. },
  166. onprogress:function(){
  167. }
  168. });
  169. if(timeout){
  170. setTimeout(()=>{
  171. reject(img);
  172. },timeout);
  173. }
  174. })
  175. }
  176. this.Download_img_by_blob = (img)=>{Download_img_by_blob(img);}
  177. function Download_img_by_blob(img){
  178. return new Promise(async(rs,rj)=>{
  179. if(!img){alert("imgs is empty");rj(img);return;}
  180. let name = '';
  181. let src = '';
  182. try{
  183. await Check_and_get_nameAndsrc({img:img,checkSrc:checkSrc})
  184. .then((m)=>{
  185. name = m.name;
  186. src = m.src;
  187. console.log(m);
  188. });
  189. }catch(error){
  190. rj(img);
  191. return;
  192. }
  193. UrlToBlob({url:src,timeout:timeout})
  194. .then(blob=>{
  195. let a = $('<a></a>').attr({
  196. download:name,
  197. href:blob
  198. })
  199. a[0].click();
  200. rs(img);
  201. })
  202. .catch(er=>{
  203. console.log(er);
  204. rj(img);
  205. });
  206. if(timeout){
  207. setTimeout(()=>{
  208. rj(img);
  209. },timeout)
  210. }
  211. })
  212. }
  213. this.Download_img_by_atag = (img,nowIsImgPage)=>{Download_img_by_atag(img,nowIsImgPage);}
  214. async function Download_img_by_atag(img,nowIsImgPage) {
  215. return new Promise(async(resolve,reject)=>{
  216. if(!img){alert("imgs is empty");reject(img);return;}
  217. if(!GM_setValue){alert("GM_setValue is underfind");reject(img);return;}
  218. if (!nowIsImgPage) {
  219. let name = '';
  220. let src = '';
  221. try{
  222. await Check_and_get_nameAndsrc({img:img,checkSrc:checkSrc})
  223. .then((m)=>{
  224. name = m.name;
  225. src = m.src;
  226. console.log(m);
  227. });
  228. }catch(error){
  229. reject(img);
  230. return;
  231. }
  232. GM_setValue("downloadType", "start");
  233. GM_setValue("downloadName", name);
  234. GM_setValue("downloadSrc", src);
  235. let mi = new My_iframe();
  236. let iframe;
  237. let isTimeout = false;
  238. if(timeout){
  239. setTimeout(()=>{isTimeout = true;},timeout)
  240. }
  241. await mi.Add_iframe(src).then(ifr=>iframe = ifr);
  242. const checkDownload = setInterval(() => {
  243. if (GM_getValue("downloadType") === "end") {
  244. $(iframe).remove();
  245. resolve(img);
  246. clearInterval(checkDownload);
  247. return;
  248. }
  249. if(isTimeout){
  250. reject(img);
  251. }
  252. }, 100);
  253. }else {
  254. const newimg = $("img").attr({
  255. name:GM_getValue("downloadName"),
  256. })
  257. await Check_and_get_nameAndsrc({img:newimg,checkSrc:true})
  258. .then((m)=>{
  259. let name = m.name;
  260. let src = m.src;
  261. $('<a></a>').attr({
  262. 'href': src,
  263. 'download': name,
  264. })[0].click();
  265. });
  266. await new Promise(resolve => setTimeout(resolve, 1000));
  267. GM_setValue("downloadType", "end");
  268. window.close();
  269. }
  270. })
  271. }
  272. this.Listening_Download_by_atag = ()=>{
  273. const locationHref = window.location.href;
  274. const GM_downloadSrc = GM_getValue("downloadSrc");
  275. if(!GM_downloadSrc){return;}
  276. if(GM_downloadSrc == locationHref|| locationHref.indexOf(GM_downloadSrc)>=0 || GM_downloadSrc.indexOf(locationHref)>=0 ){
  277. Download_img_by_atag($("img"),nowIsImgPage);
  278. }
  279. }
  280. /**
  281. * @example Urls({url:url,timeout : 100})
  282. */
  283. this.UrlToBlob = (args)=>{UrlToBlob(args);}
  284. async function UrlToBlob(args) {
  285. return new Promise((resolve,reject)=>{
  286. if(!args.url){reject("no url");}
  287. if(args.timeout){
  288. const timeout = setTimeout(function() {reject("fetch timeout")}, args.timeout);
  289. }
  290. fetch(args.url)
  291. .then(response => {
  292. const contentLength = response.headers.get('Content-Length');
  293. const total = parseInt(contentLength, 10);
  294. let loaded = 0;
  295. // 克隆响应以便分别读取流和获得 Blob
  296. const clonedResponse = response.clone();
  297. const reader = clonedResponse.body.getReader();
  298. // 更新进度的函数
  299. function updateProgress({ done, value }) {
  300. if (done) {
  301. return; // 如果读取完毕,直接返回
  302. }
  303. loaded += value.byteLength; // 累加已加载字节
  304. const progress = (loaded / total) * 100; // 计算进度百分比
  305. console.log(`Loading: ${progress.toFixed(2)}%`);
  306. FetchShowProgress(progress);
  307. // 继续读取下一块数据
  308. return reader.read().then(updateProgress);
  309. }
  310. // 开始读取流以更新进度
  311. return reader.read().then(updateProgress).then(() => {
  312. // 完成后返回原始响应的 Blob
  313. return response.blob();
  314. });
  315. })
  316. .then(blob => {
  317. const blobUrl = URL.createObjectURL(blob);
  318. resolve(blobUrl);
  319. })
  320. .catch(error => {
  321. console.error('Error caching video:', error);
  322. reject(error);
  323. });
  324. });
  325. }
  326. this.FetchShowProgress = (pro)=>{FetchShowProgress(pro);}
  327. function FetchShowProgress(pro){
  328. if(this.maxDownloadingCounts==1 && this.imgs.length==1){
  329. window.GAIL.showmass(pro);
  330. $(".mass_top").css('font-size',"10vmin");
  331. }
  332. }
  333. this.Check_and_get_nameAndsrc = (args)=>{Check_and_get_nameAndsrc(args);}
  334. function Check_and_get_nameAndsrc(args){
  335. return new Promise(async (resolve,reject)=>{
  336. if(!args || !args.img){return reject();}
  337. let src = args.img.attr('big_src')||args.img.attr('big-src');
  338. if(!src){src = args.img.attr('src');}
  339. if(!src){src = args.img.attr('small_src')||args.img.attr('small-src');}
  340. if(!src){reject();}
  341. //console.log("check:"+src)
  342. if(args.checkSrc){
  343. try{
  344. await check_src_is_right(src);
  345. }catch(error){
  346. reject();
  347. }
  348. }
  349. let ext = src.match(/\.jpg|\.png|\.webp|\.gif|\.bmp/g);
  350. if(!ext){ext = '.png';}else{ext = ext[0];}
  351. let name = args.img.attr('name');
  352. if(!name){name = document.title + new Date().getTime() + ext;}
  353. resolve({name:name,src:src,img:args.img});
  354. });
  355. }
  356. this.check_src_is_right = (src)=>{check_src_is_right(src);}
  357. function check_src_is_right(src){
  358. return new Promise((resolve,reject)=>{
  359. let iimg = new Image();
  360. iimg.onload = function(){
  361. if(this.width*this.height*this.naturalWidth*this.naturalHeight==0){reject();}else{resolve();}
  362. }
  363. iimg.onerror = function(){reject();}
  364. iimg.src = src;
  365. setTimeout(function() {reject();}, 2000);
  366. })
  367. }
  368. function downloadText(text,name) {
  369. // 创建 Blob 对象
  370. var blob = new Blob([text], { type: "text/plain" });
  371. // 创建下载链接
  372. var url = URL.createObjectURL(blob);
  373. // 创建下载按钮
  374. var a = document.createElement("a");
  375. a.href = url;
  376. a.download = name?name:"downloaded_text"+new Date().getTime()+".txt"; // 文件名
  377. document.body.appendChild(a);
  378. // 模拟点击下载
  379. a.click();
  380. // 清理
  381. window.URL.revokeObjectURL(url);
  382. document.body.removeChild(a);
  383. }
  384. this.Download_to_text = (url,name)=>{downloadText(url,name);}
  385. }
  386. $(function(){
  387. let dd = new Downloader()
  388. dd.Listening_Download_by_atag()
  389. })
  390.  
  391. window.Downloader = Downloader;

QingJ © 2025

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