Advertisement
Guest User

CurseForge Modbuilder Fix

a guest
Dec 14th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        CurseForge Modpack Builder
  3. // @namespace   ZeldoKavira
  4. // @description Modpack Builder
  5. // @include     http://minecraft.curseforge.com/mc-mods/*
  6. // @include     http://minecraft.curseforge.com/mc-mods
  7. // @include     http://minecraft.curseforge.com/projects/*
  8. // @require     http://code.jquery.com/jquery-1.11.2.min.js
  9. // @require     http://cdn.jsdelivr.net/qtip2/2.2.1/jquery.qtip.min.js
  10. // @version     10
  11. // @grant       GM_getValue
  12. // @grant       GM_setValue
  13. // @grant       GM_listValues
  14. // @grant       GM_deleteValue
  15. // ==/UserScript==
  16.  
  17. var manifest = '{  \n\t"manifestType":"minecraftModpack",\n\t"manifestVersion":1,\n\t"name":"%name%",\n\t"version":"%version%",\n\t"author":"%author%",\n\t"description":"%description%",\n\t"files":[  \n%files%\n\t],\n\t"overrides":"overrides",\n\t"minecraft":{  \n\t\t"version":"%mcversion%",\n\t\t"libraries":"libraries",\n\t\t"additionalJavaArgs":{  \n\t\t\t"1.6":"-XX:JavaArgsAreSoMuchFun",\n\t\t\t"1.7":"-XX:DifferentArgsHereBecauseWhyNot"\n\t\t},\n\t\t"modLoaders":[  \n\t\t\t{  \n\t\t\t\t"id":"%forgeversion%",\n\t\t\t\t"primary":true\n\t\t\t}\n\t\t]\n\t}\n}';
  18. var editImage = 'https://modpacktool.cursecdn.com/imgs/editField.png';
  19. var deleteImage = 'https://modpacktool.cursecdn.com/imgs/deleteProject.png';
  20. var saveImage = 'https://modpacktool.cursecdn.com/imgs/exportProject.png';
  21. var loadImage = 'https://modpacktool.cursecdn.com/imgs/importProject.png';
  22. var checkMark = 'https://modpacktool.cursecdn.com/imgs/required.png';
  23. var noCheckmark = 'https://modpacktool.cursecdn.com/imgs/notRequired.png';
  24. var deleteFile = 'https://modpacktool.cursecdn.com/imgs/deleteFile.png';
  25.  
  26. var mainDivStyleSmall = "p-feedback p-feedback-a roundBorder";
  27. var mainDivStyleFull = mainDivStyleSmall + " fullStyling";
  28.  
  29. if (document.getElementsByClassName('details-versions').length > 0) {
  30.     //We are on a files page.
  31.     document.getElementsByClassName('details-actions') [0].innerHTML += '<a id="zzaddButton" style="text-align:center" class="e-button">Add</a>';
  32.     $('#zzaddButton').click(addFile);
  33. }
  34. addSideBar();
  35. updateSideBar();
  36.  
  37.  
  38. function addSideBar() {
  39.    
  40.     //CSS Overrides and additions
  41.     //Try and build off the sites CSS so that it flows with any changes as much as possible.
  42.     var head = document.getElementsByTagName("head")[0];
  43.     var style = document.createElement("style");
  44.     style.setAttribute('type', 'text/css');
  45.     style.innerText = ".fullStyling {min-width:258px;overflow:auto;} a.zProjectLink {margin-left: 5px;} span.zHeaders { font-weight:bold;} img.removeFile {margin-left: 6px;} .zProjectLink {margin-top:1px;} #zzSideBarDiv {left:-8px;z-index:3;transform: translateY(-50%);max-height:80%;min-height:36px;} #zzSideBarInnerDiv {text-align:left;cursor:auto;background-color: #5d5d5d;border:none;} #zzSideBarSpan {font-weight: normal;} .mainImgs { width:18px; height: 18px; position: absolute; cursor: pointer;} .infoSpan {background: url('https://dl.dropboxusercontent.com/u/27292492/scripts/imgs/editField.png');background-repeat: no-repeat;background-position: left center;background-size: 15px 15px; background-position-x: 6px;cursor: pointer;} .roundBorder {background-color: rgb(93, 93, 93);border-bottom-color: rgba(0, 0, 0, 0.2);border-bottom-style: solid;border-bottom-width: 4px;border-image-outset: 0px;border-image-repeat: stretch;border-image-slice: 100%;border-image-source: none;border-image-width: 1;border-left-color: rgba(0, 0, 0, 0.2);border-left-style: solid;border-left-width: 4px;border-right-color: rgba(0, 0, 0, 0.2);border-right-style: solid;border-right-width: 4px;border-top-color: rgba(0, 0, 0, 0.2);border-top-style: solid;border-top-width: 4px;-webkit-border-bottom-right-radius:8px;-moz-border-bottom-right-radius:8px;-ms-border-bottom-right-radius:8px;-o-border-bottom-right-radius:8px;border-bottom-right-radius:8px;-webkit-border-top-right-radius:8px;-moz-border-top-right-radius:8px;-ms-border-top-right-radius:8px;-o-border-top-right-radius:8px;border-top-right-radius:8px;} .zLinks a:visited,.zLinks a:link {color:#A9A9A9;}";
  46.     head.appendChild(style);
  47.     style = document.createElement("link");
  48.     style.setAttribute('rel', 'stylesheet');
  49.     style.setAttribute('type', 'text/css');
  50.     style.setAttribute('href', 'http://cdn.jsdelivr.net/qtip2/2.2.1/jquery.qtip.min.css');
  51.     head.appendChild(style);
  52.    
  53.     //Add the divs needed to create the sidebar
  54.     var parentDiv = document.getElementById('site-main');
  55.    
  56.     var node = document.createElement('DIV');
  57.     node.setAttribute('class', mainDivStyleFull);
  58.     node.setAttribute("id", "zzSideBarDiv");
  59.    
  60.     var innerDiv = document.createElement("DIV");
  61.     innerDiv.setAttribute("id", "zzSideBarInnerDiv");
  62.     innerDiv.setAttribute("class", "p-feedback-tab zLinks");
  63.    
  64.     var spn = document.createElement('SPAN');
  65.     spn.setAttribute("class", "p-feedback-link j-feedback-link-text");
  66.     spn.setAttribute("id", "zzSideBarSpan");
  67.    
  68.     node.appendChild(innerDiv);
  69.     innerDiv.appendChild(spn);
  70.     parentDiv.insertBefore(node, document.getElementById('footer-push'));
  71.    
  72.    
  73.     //Stop the scrolling of the entire page if we are in the modpack div
  74.     $('#zzSideBarDiv').on('DOMMouseScroll mousewheel', function(ev) {
  75.         var $this = $(this),
  76.             scrollTop = this.scrollTop,
  77.             scrollHeight = this.scrollHeight,
  78.             height = $this.height(),
  79.             delta = (ev.type == 'DOMMouseScroll' ?
  80.                      ev.originalEvent.detail * -40 :
  81.                      ev.originalEvent.wheelDelta),
  82.             up = delta > 0;
  83.        
  84.         var prevent = function() {
  85.             ev.stopPropagation();
  86.             ev.preventDefault();
  87.             ev.returnValue = false;
  88.             return false;
  89.         }
  90.        
  91.         if (!up && -delta > scrollHeight - height - scrollTop) {
  92.             $this.scrollTop(scrollHeight);
  93.             return prevent();
  94.         } else if (up && delta > scrollTop) {
  95.             $this.scrollTop(0);
  96.             return prevent();
  97.         }
  98.             });
  99.    
  100. }
  101.  
  102. function updateSideBar() {
  103.     var parentDiv = document.getElementById('zzSideBarSpan');
  104.     parentDiv.innerHTML="";
  105.    
  106.     if (GM_listValues().length == 0) {
  107.         var img = document.createElement("img");
  108.         img.setAttribute("src", loadImage);
  109.         img.setAttribute("id", "imgLoad");
  110.         img.setAttribute("class", "mainImgs");
  111.         img.setAttribute("data-qtip", "Load Modpack");
  112.         parentDiv.appendChild(img);
  113.         parentDiv.parentNode.parentNode.setAttribute('class', mainDivStyleSmall);
  114.         return;
  115.     }
  116.     parentDiv.parentNode.parentNode.setAttribute('class', mainDivStyleFull);
  117.    
  118.     appendText(parentDiv, "Modpack Information:", "zHeaders");
  119.    
  120.     var img = document.createElement("img");
  121.     img.setAttribute("src", deleteImage);
  122.     img.setAttribute("id", "imgDelete");
  123.     img.setAttribute("class", "mainImgs");
  124.     img.setAttribute("style", "right:8px;");
  125.     img.setAttribute("data-qtip", "Delete Modpack");
  126.     parentDiv.appendChild(img);
  127.     img = document.createElement("img");
  128.     img.setAttribute("src", saveImage);
  129.     img.setAttribute("id", "imgGen");
  130.     img.setAttribute("class", "mainImgs");
  131.     img.setAttribute("style", "right:28px;");
  132.     img.setAttribute("data-qtip", "Generate Modpack");
  133.     parentDiv.appendChild(img);
  134.     img = document.createElement("img");
  135.     img.setAttribute("src", loadImage);
  136.     img.setAttribute("id", "imgLoad");
  137.     img.setAttribute("class", "mainImgs");
  138.     img.setAttribute("style", "right:48px;");
  139.     img.setAttribute("data-qtip", "Load Modpack");
  140.     parentDiv.appendChild(img);
  141.    
  142.     appendInfoSpan(parentDiv, "maniName", "Name");
  143.     appendInfoSpan(parentDiv, "maniVersion", "Version");
  144.     appendInfoSpan(parentDiv, "maniAuthor", "Author Name");
  145.     appendInfoSpan(parentDiv, "maniDesc", "Desc");
  146.     appendInfoSpan(parentDiv, "maniMinecraft", "MC Ver");
  147.     appendInfoSpan(parentDiv, "maniForge", "Forge Ver");
  148.    
  149.    
  150.     var currentFiles = GM_listValues();
  151.     var fileCount = 0;
  152.     for (var i = 0; i < currentFiles.length; i++) {
  153.         if (!isNaN(currentFiles[i].charAt(0))) {
  154.             fileCount++;
  155.         }
  156.     }
  157.     appendNewLine(parentDiv);
  158.     appendNewLine(parentDiv);
  159.     appendText(parentDiv, "Modpack Files (" + fileCount + "):", "zHeaders");
  160.     for (var i = 0; i < currentFiles.length; i++) {
  161.         if (!isNaN(currentFiles[i].charAt(0))) {
  162.             appendNewLine(parentDiv);
  163.            
  164.             img = document.createElement("img");
  165.             img.setAttribute("src", deleteFile);
  166.             img.setAttribute("dataid", currentFiles[i]);
  167.             img.setAttribute("style", "width:18px");
  168.             img.setAttribute("class", "removeFile");
  169.             img.setAttribute("data-qtip", "Remove file from project");
  170.             parentDiv.appendChild(img);
  171.            
  172.             img = document.createElement("img");
  173.             img.setAttribute("src", GM_getValue('required' + currentFiles[i], false) ? checkMark : noCheckmark);
  174.             img.setAttribute("datareq", GM_getValue('required' + currentFiles[i], false));
  175.             img.setAttribute("dataid", currentFiles[i]);
  176.             img.setAttribute("style", "width:18px");
  177.             img.setAttribute("class", "setRequired");
  178.             img.setAttribute("data-qtip", GM_getValue('required' + currentFiles[i], false) ? "Click to mark not required" : "Click to mark required");
  179.             parentDiv.appendChild(img);
  180.            
  181.             //parentDiv.innerHTML += "&nbsp;&nbsp;<a class='zProjectLink' href='/mc-mods/" + currentFiles[i] + "'>" + (GM_getValue('fileName' + currentFiles[i]) == null ? "Unknown filename: (" + currentFiles[i] + ")" : GM_getValue('fileName' + currentFiles[i])) + "</a>";
  182.             appendProjectLink(parentDiv, currentFiles[i]);
  183.         }
  184.     }
  185.    
  186.     $('span.infoSpan').click(infoSpanClicked);
  187.     $('img.setRequired').click(imgSetRequired);
  188.     $('img.removeFile').click(imgRemoveFile);
  189.     $('#imgLoad').click(parseModPack);
  190.     $('#imgDelete').click(deleteModPack);
  191.     $('#imgGen').click(generateModPack);
  192.    
  193.     $('[data-qtip!=""]').qtip({
  194.         content: {
  195.             attr: 'data-qtip'
  196.         },
  197.         style: {
  198.             classes: 'qtip-tipsy'
  199.         },
  200.         position: {
  201.             target: 'mouse',
  202.             adjust: {
  203.                 x: 10,
  204.                 y: 10
  205.             }
  206.         }
  207.     });
  208. }
  209.  
  210. function appendInfoSpan(parent, valName, text) {
  211.     var tSpan = document.createElement("span");
  212.     tSpan.setAttribute("class", "infoSpan");
  213.     tSpan.setAttribute("data-qtip", "Set " + text);
  214.     tSpan.setAttribute("id", valName);
  215.     tSpan.innerHTML = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + text + ": " + (GM_getValue(valName) == null ? "Not set" : trun(GM_getValue(valName), 45));
  216.     parent.appendChild(tSpan);
  217.     //document.getElementById(valName).addEventListener('click', infoSpanClicked, true);
  218. }
  219.  
  220. function appendText(parent, string, classes) {
  221.     var tSpan = document.createElement("span");
  222.     tSpan.setAttribute("class", classes);
  223.     tSpan.innerHTML = string;
  224.     parent.appendChild(tSpan);
  225. }
  226.  
  227. function appendNewLine(parent) {
  228.     parent.appendChild(document.createElement("br"));
  229. }
  230.  
  231. function appendProjectLink(parent, currentFile) {
  232.     //<a class='zProjectLink' href='/mc-mods/" + currentFiles[i] + "'>" + (GM_getValue('fileName' + currentFiles[i]) == null ? "Unknown filename: (" + currentFiles[i] + ")" : GM_getValue('fileName' + currentFiles[i])) + "</a>"
  233.     var tSpan = document.createElement("a");
  234.     tSpan.setAttribute("class", "zProjectLink");
  235.     tSpan.setAttribute("href", '/mc-mods/' + currentFile);
  236.     tSpan.innerHTML =  (GM_getValue('fileName' + currentFile) == null ? "Unknown filename: (" + currentFile + ")" : GM_getValue('fileName' + currentFile));
  237.     parent.appendChild(tSpan);
  238. }
  239.  
  240. function trun(text, maxLength) {
  241.     var ret = text;
  242.     if (ret.length > maxLength) {
  243.         ret = ret.substr(0,maxLength-3) + "...";
  244.     }
  245.     return ret;
  246. }
  247.  
  248. function imgSetRequired(e) {
  249.     switch(e.target.getAttribute("datareq")) {
  250.         case "false":
  251.             GM_setValue("required" + e.target.getAttribute("dataid"), true);
  252.             break;
  253.         case "true":
  254.             GM_setValue("required" + e.target.getAttribute("dataid"), false);
  255.             break;
  256.     }
  257.     updateSideBar();
  258. }
  259.  
  260. function imgRemoveFile(e) {
  261.     if (confirm('Are you sure you want to remove this file?')) {
  262.         GM_deleteValue("required" + e.target.getAttribute("dataid"));
  263.         GM_deleteValue("fileName" + e.target.getAttribute("dataid"));
  264.         GM_deleteValue(e.target.getAttribute("dataid"));
  265.         updateSideBar();
  266.     }
  267. }
  268.  
  269. function infoSpanClicked(e) {
  270.     switch (e.target.id) {
  271.         case "maniName":
  272.             getPrompt('maniName', 'Insert Modpack Name:', false, '', true);
  273.             break;
  274.         case "maniVersion":
  275.             getPrompt('maniVersion', 'Insert Version:', false, '', true);
  276.             break;
  277.         case "maniAuthor":
  278.             getPrompt('maniAuthor', 'Insert Author:', false, '', true);
  279.             break;
  280.         case "maniDesc":
  281.             getPrompt('maniDesc', 'Insert Modpack Description:', false, '', true);
  282.             break;
  283.         case "maniForge":
  284.             getPrompt('maniForge', 'Insert Forge Version:', false, 'forge-10.13.2.1277', true);
  285.             break;
  286.         case "maniMinecraft":
  287.             getPrompt('maniMinecraft', 'Insert Minecraft Version:', false, '1.7.10', true);
  288.             break;
  289.     }
  290.     updateSideBar();
  291. }
  292.  
  293. function getPrompt(varname, promptmsg, isBoolean, defaultAn, doSetVar) {
  294.     if (GM_getValue(varname) != null) {
  295.         defaultAn = GM_getValue(varname);  
  296.     }
  297.     if (doSetVar) {
  298.         if (isBoolean) {
  299.             GM_setValue(varname, confirm(promptmsg));
  300.         } else {
  301.             GM_setValue(varname, prompt(promptmsg, defaultAn));
  302.         }
  303.         return GM_getValue(varname);
  304.     } else {
  305.         return isBoolean ? confirm(promptmsg)  : prompt(promptmsg, defaultAn);
  306.     }
  307. }
  308.  
  309. function getValueOrPrompt(varname, promptmsg, isBoolean, defaultAn, doSetVar) {
  310.     if (GM_getValue(varname) != null) {
  311.         return GM_getValue(varname);
  312.     } else {
  313.         if (doSetVar) {
  314.             if (isBoolean) {
  315.                 GM_setValue(varname, confirm(promptmsg));
  316.             } else {
  317.                 GM_setValue(varname, prompt(promptmsg, defaultAn));
  318.             }
  319.             return GM_getValue(varname);
  320.         } else {
  321.             return isBoolean ? confirm(promptmsg)  : prompt(promptmsg, defaultAn);
  322.         }
  323.     }
  324. }
  325.  
  326. function deleteModPack() {
  327.     if (confirm('You are about to delete the current modpack. Are you sure?')) {
  328.         var currentFiles = GM_listValues();
  329.         for (var i = 0; i < currentFiles.length; i++) {
  330.             GM_deleteValue(currentFiles[i]);
  331.         }
  332.         updateSideBar();
  333.         alert('Modpack Deleted');
  334.     }
  335. }
  336.  
  337. function addFile() {
  338.     var curWorking = window.location.toString().split('mc-mods/') [1];
  339.     var projectID = curWorking.substr(0, curWorking.indexOf('-'));
  340.     var fileID = curWorking.substr(curWorking.lastIndexOf('/') + 1);
  341.     if (fileID.indexOf("#") > -1) {
  342.         fileID = fileID.substr(0, fileID.indexOf("#"));
  343.     }
  344.     if ((GM_getValue(projectID) != null && confirm('This project already has a file in the modpack, are you sure you want to overwrite it?')) || GM_getValue(projectID) == null) {
  345.         if (confirm('Make this file required to run the modpack? (OK=Yes, Cancel=No)')) {
  346.             GM_setValue('required' + projectID, true);
  347.         } else {
  348.             GM_setValue('required' + projectID, false);
  349.         }
  350.         GM_setValue(projectID, fileID);
  351.         GM_setValue('fileName' + projectID, document.getElementsByClassName("overflow-tip")[1].innerText);        
  352.         updateSideBar();
  353.     }
  354. }
  355.  
  356. function generateModPack() {
  357.     var filesList = '';
  358.     var currentFiles = GM_listValues();
  359.     for (var i = 0; i < currentFiles.length; i++) {
  360.         if (!isNaN(currentFiles[i].charAt(0))) {
  361.             console.log('Found mod: ' + currentFiles[i] + ' - Required: ' + GM_getValue('required' + currentFiles[i], false));
  362.             if (filesList != '') {
  363.                 filesList += ',\n';
  364.             }
  365.             filesList += '\t\t{\n\t\t\t"projectID": ' + currentFiles[i] + ',\n\t\t\t"fileID": ' + GM_getValue(currentFiles[i]) + (GM_getValue('fileName' + currentFiles[i]) == null ? "" : ',\n\t\t\t"fileName": "' + GM_getValue('fileName' + currentFiles[i]) + '"') + (GM_getValue('required' + currentFiles[i], false) ? ',\n\t\t\t"required": true' : '') + '\n\t\t}';
  366.         }
  367.     }
  368.     var man = manifest.replace('%name%', getValueOrPrompt('maniName', 'Insert Modpack Name:', false, '', true));
  369.     man = man.replace('%version%', getValueOrPrompt('maniVersion', 'Insert Version:', false, '', true));
  370.     man = man.replace('%author%', getValueOrPrompt('maniAuthor', 'Insert Author:', false, '', true));
  371.     man = man.replace('%description%', getValueOrPrompt('maniDesc', 'Insert Modpack Description:', false, '', true));
  372.     man = man.replace('%files%', filesList);
  373.     man = man.replace('%mcversion%', getValueOrPrompt('maniMinecraft', 'Insert Minecraft Version:', false, '1.7.10', true));
  374.     man = man.replace('%forgeversion%', getValueOrPrompt('maniForge', 'Insert Forge Version:', false, 'forge-10.13.2.1277', true));
  375.     console.log('Manifest: ' + man);
  376.     updateSideBar();
  377.     //prompt("Your new manifest: (Ctrl+C to copy)", man);
  378.     download("manifest.json", man);
  379. }
  380.  
  381. function parseModPack() {
  382.     var stuff = prompt("Paste in the manifest from a previous pack: ");
  383.     if (stuff == null || stuff == '')
  384.         return;
  385.     try {
  386.         var prevPack = JSON.parse(stuff);
  387.     } catch (ex) {
  388.         alert("That JSON is invalid!");
  389.         return;
  390.     }
  391.     if (confirm('You are about to delete the current modpack. Are you sure?')) {
  392.         var currentFiles = GM_listValues();
  393.         for (var i = 0; i < currentFiles.length; i++) {
  394.             GM_deleteValue(currentFiles[i]);
  395.         }
  396.     } else {
  397.         return;
  398.     }
  399.    
  400.     var fileCount = 0;
  401.     console.log("Starting file process");
  402.     for (var i = 0; i < prevPack.files.length; i++) {
  403.         if (prevPack.files[i] != null && prevPack.files[i].projectID != null) {
  404.             fileCount++;
  405.             GM_setValue(prevPack.files[i].projectID, prevPack.files[i].fileID);
  406.             if (prevPack.files[i].required != null) {
  407.                 GM_setValue("required" + prevPack.files[i].projectID, prevPack.files[i].required);
  408.             } else {
  409.                 GM_setValue("required" + prevPack.files[i].projectID, false);
  410.             }
  411.             if (prevPack.files[i].fileName != null) {
  412.                 GM_setValue("fileName" + prevPack.files[i].projectID, prevPack.files[i].fileName);
  413.             }
  414.         }
  415.     }
  416.     console.log("Files done");
  417.     setIfDefined("maniName", prevPack.name);
  418.     setIfDefined('maniVersion', prevPack.version);
  419.     setIfDefined('maniAuthor', prevPack.author);
  420.     setIfDefined('maniDesc', prevPack.description);
  421.     setIfDefined('maniMinecraft', prevPack.minecraft == null ? null : prevPack.minecraft.version);
  422.     setIfDefined('maniForge', prevPack.minecraft == null ? null : prevPack.minecraft.modLoaders == null ? null : prevPack.minecraft.modLoaders[0].id);
  423.     updateSideBar();
  424.     alert("Pack Loaded.\nName: " + GM_getValue('maniName') + "\nAuthor: " + GM_getValue('maniAuthor') + "\nPack Version: " + GM_getValue('maniVersion') + "\nDescription: " + GM_getValue('maniDesc') + "\nMinecraft Version: " + GM_getValue('maniMinecraft') + "\nForge Version: " + GM_getValue('maniForge') + "\nFound " + fileCount + " files.");
  425. }
  426.  
  427. function setIfDefined(varName, toSave) {
  428.     if (toSave != null) {
  429.         GM_setValue(varName, toSave);
  430.     }
  431. }
  432.  
  433. function download(filename, text) {
  434.     var pom = document.createElement('a');
  435.     console.log("Saving: " + text);
  436.     pom.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(text));
  437.     pom.setAttribute('download', filename);
  438.     pom.click();
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement