// ==UserScript== // @name eduCoder项目文件打包下载 // @namespace http://tampermonkey.net/ // @version 1.0.1.1 // @description 将eduCoder的项目文件打包成ZIP下载 // @author YiJie // @match https://www.educoder.net/tasks/* // @require https://cdn.jsdelivr.net/npm/notiflix@2.1.2/dist/AIO/notiflix-aio-2.1.2.min.js // @require https://cdn.jsdelivr.net/npm/jszip@3.4.0/dist/jszip.min.js // @require https://cdn.jsdelivr.net/npm/jszip@3.4.0/vendor/FileSaver.js // @grant none // @downloadURL https://update.greasyfork.icu/scripts/403244/eduCoder%E9%A1%B9%E7%9B%AE%E6%96%87%E4%BB%B6%E6%89%93%E5%8C%85%E4%B8%8B%E8%BD%BD.user.js // @updateURL https://update.greasyfork.icu/scripts/403244/eduCoder%E9%A1%B9%E7%9B%AE%E6%96%87%E4%BB%B6%E6%89%93%E5%8C%85%E4%B8%8B%E8%BD%BD.meta.js // ==/UserScript== (function(){ var log = { isDev:false, debug: function(temp){ if(this.isDev) console.log(temp); }, info: function(temp){ console.log(temp); } } var MD5 = function(d){result = M(V(Y(X(d),8*d.length)));return result.toLowerCase()};function M(d){for(var _,m="0123456789ABCDEF",f="",r=0;r>>4&15)+m.charAt(15&_);return f}function X(d){for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<>5]>>>m%32&255);return _}function Y(d,_){d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n>16)+(_>>16)+(m>>16)<<16|65535&m}function bit_rol(d,_){return d<<_|d>>>32-_} function first_stamp(fun){ $.ajax({ url:"https://www.educoder.net/api/main/first_stamp.json", async:true, type:"GET", success:function(result){ log.debug(result); fun(result.message); } }); } function getPro(){ first_stamp(function(message){ var g = "79e33abd4b6588941ab7622aed1e67e8"; var client_key = MD5(g+message); var identifier = ""; var directoryDict = { directory:{}, directory_list:{}, isfinish:false } var rep_content = function(filePath,file){ var h = location.href.split('tasks/'); var url = h[0]+'api/tasks/'+h[1]+'/rep_content.json'; $.ajax({ url:url+"?path="+filePath+"&" +"status=1&" +"retry=0&" +"randomcode="+message+"&" +"client_key="+client_key, async:true, type:"GET", success:function(result){ var file_path_section = filePath.split('/'); (function(i,dict){ if(i===file_path_section.length) return; if(!dict[file_path_section[i]]){ dict[file_path_section[i]] = result.content; } arguments.callee(i+1,dict[file_path_section[i]]); })(0,directoryDict.directory); directoryDict.directory_list[filePath] = result.content; directoryDict.isfinish = true; for (var key in directoryDict.directory_list){ if(directoryDict.directory_list[key]==="") directoryDict.isfinish = false; } if(directoryDict.isfinish){ log.debug(directoryDict); log.info(directoryDict.directory); if(JSZip){ var zip = new JSZip(); (function(dictx,dir){ for(var key in dictx){ if(typeof(dictx[key]) === "object"){ var tempDir = dir.folder(key); arguments.callee(dictx[key],tempDir); } else if(typeof(dictx[key]) === "string"){ dir.file(key, dictx[key]); } } })(directoryDict.directory,zip); zip.generateAsync({type:"blob"}) .then(function(content) { saveAs(content, $('title').html()+".zip"); }); } } } }); } var repository = function(path){ $.ajax({ url:"https://www.educoder.net/api/myshixuns/"+identifier+"/repository.json?" +"randomcode="+message+"&" +"client_key="+client_key, async:true, traditional: true, contentType: 'application/json; charset=utf-8', data: JSON.stringify({ path:path, }), type:"POST", success:function(result){ const trees = result.trees; for(let i = 0;i < trees.length;i++){ log.debug(trees[i]); var filePath = ""; if(trees[i].type === "tree"){ if(path===""){ filePath = trees[i].name; directoryDict.directory[trees[i].name] = {} } else{ filePath = path+'/'+trees[i].name; var file_path_section = filePath.split('/'); (function(i,dict){ if(i===file_path_section.length) return; if(!dict[file_path_section[i]]){ dict[file_path_section[i]]={}; } arguments.callee(i+1,dict[file_path_section[i]]); })(0,directoryDict.directory); } repository(filePath); }else if(trees[i].type === "blob"){ if(path==="") filePath = trees[i].name; else filePath = path+'/'+trees[i].name; log.debug(filePath); directoryDict.directory_list[filePath] = "" rep_content(filePath); }else{ log.debug("未知类型文件"); } } } }); } var h = location.href.split('tasks/'); var url = h[0]+'api/tasks/'+h[1]+'.json'; $.ajax({ url:url+"?randomcode="+message+"&" +"client_key="+client_key, async:true, type:"GET", success:function(result){ identifier = result['myshixun']['identifier']; log.debug(identifier); repository(""); } }); }); } if(Notiflix) Notiflix.Confirm.Show( '提示', '是否下载当前项目zip文件?', '下载', '不了', function(){ getPro(); }, function(){ } ); else getPro(); })();