ai-challenge Exporter

Helps you export all the files from ai-challenge.com into zip archive

当前为 2015-03-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ai-challenge Exporter
  3. // @id ai-challenge_exporter
  4. // @namespace ai-challenge_exporter
  5. // @version 0.1
  6. // @author KOLANICH
  7. // @copyright KOLANICH, 2014
  8. // @description Helps you export all the files from ai-challenge.com into zip archive
  9. // @license GNU GPL v3
  10. // @noframes 1
  11. // @icon http://ai-challenge.com/favicon.ico
  12. // @website https://github.com/KOLANICH/ai-challenge-Exporter
  13. // @include http://ai-challenge.com/ru/profile/*
  14. // @resource jszip https://raw.github.com/Stuk/jszip/master/jszip.js
  15. // @run-at document-end
  16. // @contributionURL https://github.com/KOLANICH/ai-challenge-Exporter/fork
  17. // @contributionAmount feel free to fork and contribute
  18. // ==/UserScript==
  19. //var unpackCodesRx=/([a-f\d]{32})(\w+\.\w+)\1/ig;
  20. const signature="89c4e347cdca08508a415e959f2a9b5c";
  21. const unpackCodesRx=new RegExp("(?://)?(?:\\r?\\n)*"+signature+"(\\w+\\.\\w+)"+signature+"(?:\\r?\\n)*","ig");
  22. var fileEl= document.createElement("INPUT");
  23. fileEl.type="file";
  24. fileEl.accept="application/javascript, text/javascript";
  25. function checkInjection(){
  26. if(typeof JSZip==="undefined")
  27. eval(GM_getResourceText('jszip').replace("var JSZip","JSZip"));
  28. }
  29. function unpackCodes(str){
  30. let tokenz=unescape(str).split(unpackCodesRx);
  31. let filez={};
  32. let j=0;
  33. /*for(var i=1;i<tokenz.length;i+=3){
  34. let hash=tokenz[i];
  35. let code=tokenz[i+2];
  36. f(hash!=GM_cryptoHash(code, "MD5").toLowerCase()){
  37. i-=2;
  38. continue;
  39. }
  40. filez[tokenz[i+1]]={};
  41. filez[tokenz[i+1]].hash=hash;
  42. filez[tokenz[i+1]].code=code;
  43. j++;
  44. }*/
  45. for(let i=1;i<tokenz.length;i+=2){
  46. filez[tokenz[i]]={};
  47. filez[tokenz[i]].code=tokenz[i+1];
  48. j++;
  49. }
  50. return filez;
  51. }
  52. function packCodes(filez,pretty){
  53. if(typeof pretty=="undefined")pretty=true;
  54. let str="";
  55. for(let name in filez){
  56. str+=(pretty?"//":"")+signature+name+signature+(pretty?"\n"+filez[name].code+"\n":encodeURIComponent(filez[name].code));
  57. }
  58. return str;
  59. }
  60.  
  61. function exportToZip(filez){
  62. checkInjection();
  63. var zip = new JSZip();
  64. console.log(zip);
  65. for(let fileName in filez){
  66. zip.file(fileName, filez[fileName].code);
  67. }
  68. var blob=zip.generate({type:"blob",compression:"STORE"});
  69. console.log(blob);
  70. return URL.createObjectURL(blob);
  71. }
  72.  
  73. function importFromString(codes){
  74. codes=unpackCodes(codes);
  75. codes=packCodes(codes,false);
  76. var ret = GM_xmlhttpRequest({
  77. method: "POST",
  78. data: "data="+encodeURIComponent(codes),
  79. url : "/upload",
  80. headers: {
  81. "Content-Type": "application/x-www-form-urlencoded"
  82. },
  83. onload: function(res) {
  84. if (res.responseText == "ok") {
  85. window.location.reload();
  86. GM_notification("Код сохранен!");
  87. } else {
  88. GM_notification("Код не сохранен! Ошибка: " + res.responseText);
  89. }
  90. }
  91. });
  92. }
  93.  
  94. GM_registerMenuCommand("export to clipboard",function(){
  95. let filez=unpackCodes(decodeURIComponent(unsafeWindow["tournamentView"].model.attributes.code));
  96. GM_setClipboard(packCodes(filez),"text");
  97. alert("Exported to clipboard");
  98. });
  99.  
  100. GM_registerMenuCommand("export",function(){
  101. let filez=unpackCodes(decodeURIComponent(unsafeWindow["tournamentView"].model.attributes.code));
  102. GM_openInTab(exportToZip(filez));
  103. });
  104. GM_registerMenuCommand("export from editor",function(){
  105. alert("NOT IMPLEMENTED!");
  106. GM_openInTab("https://github.com/KOLANICH/ai-challenge-Exporter/issues/1");
  107. /*var spl = this.get("splitCode");
  108. if (!$(btn).hasClass("disabled")) {
  109. $(btn).addClass("disabled").html("Отправка...");
  110. for (var name in spl) {
  111. data += "89c4e347cdca08508a415e959f2a9b5c" + name + "89c4e347cdca08508a415e959f2a9b5c" + encodeURIComponent(spl[name].cm.getValue());
  112. }
  113. let codePacked=decodeURIComponent(unsafeWindow["tournamentView"].model.attributes.code);
  114. console.log(unpackCodes(codePacked));*/
  115. });
  116.  
  117. GM_registerMenuCommand("import from string",function(){
  118. let codes=prompt("Input string");
  119. if(!codes)return;
  120. importFromString(codes);
  121. });
  122.  
  123. var reader = new FileReader();
  124. reader.onload =function(evt){
  125. let codes=evt.target.result;
  126. if(!codes)return;
  127. importFromString(codes);
  128. };
  129. function handleFileSelect(evt) {
  130. for (let f of evt.target.files) {
  131. reader.readAsText(f);
  132. break;
  133. }
  134. }
  135.  
  136. fileEl.addEventListener('change', handleFileSelect, false);
  137.  
  138. GM_registerMenuCommand("import from file",function(){
  139. var evt = document.createEvent("MouseEvents");
  140. evt.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
  141. fileEl.dispatchEvent(evt);
  142. });