1337X - Upload page improved

Improves the upload page. Let's you set up default descriptions for categories and other defaults like title, tags etc. and more

目前為 2018-03-07 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name 1337X - Upload page improved
  3. // @namespace NotNeo
  4. // @author NotNeo
  5. // @description Improves the upload page. Let's you set up default descriptions for categories and other defaults like title, tags etc. and more
  6. // @include http*://1337x.*/upload
  7. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  8. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  9. // @version 2.1.2
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @grant GM.setValue
  13. // @grant GM.getValue
  14. // ==/UserScript==
  15.  
  16. //============================//
  17. //============================//
  18. // YOU SHOULD NO LONGER TOUCH //
  19. // ANYTHING INSIDE THE SCRIPT //
  20. // ALL SETTINGS ARE NOW //
  21. // AVAILABLE ON THE PAGE! //
  22. //============================//
  23. //============================//
  24.  
  25.  
  26. //setting defaults
  27. var dName = "";
  28. var dLanguage = "English";
  29. var dDesc = "";
  30. var dTags = "";
  31. var showOnlyUpload = true;
  32. var descHeight = "400";
  33. var reminder = true;
  34. var catDelay = 2;
  35.  
  36.  
  37.  
  38. (async function() { // Getting the runtime variables from local storage
  39. if( (await GM.getValue("reminder")) != null ) {
  40. reminder = await GM.getValue("reminder");
  41. }
  42. if( (await GM.getValue("dDesc")) != null ) {
  43. dDesc = await GM.getValue("dDesc");
  44. }
  45. if( (await GM.getValue("dName")) != null ) {
  46. dName = await GM.getValue("dName");
  47. }
  48. if( (await GM.getValue("dLanguage")) != null ) {
  49. dLanguage = await GM.getValue("dLanguage");
  50. }
  51. if( (await GM.getValue("dTags")) != null ) {
  52. dTags = await GM.getValue("dTags");
  53. }
  54. if( (await GM.getValue("showOnlyUpload")) != null ) {
  55. showOnlyUpload = await GM.getValue("showOnlyUpload");
  56. }
  57. if( (await GM.getValue("descHeight")) != null ) {
  58. descHeight = await GM.getValue("descHeight");
  59. }
  60. if( (await GM.getValue("catDelay")) != null ) {
  61. catDelay = await GM.getValue("catDelay");
  62. }
  63.  
  64. var savCatNamArr = [];
  65. var savCatDescArr = [];
  66.  
  67.  
  68. if( (await GM.getValue("savCatNamArrStr")) != null ) {
  69. var savCatNamArrStr = GM.getValue("savCatNamArrStr"); //get array from storage as string
  70. if(savCatNamArrStr) { savCatNamArr = JSON.parse(savCatNamArrStr); } //parse the string to an array
  71. }
  72. if( (await GM.getValue("savCatDescArrStr")) != null ) {
  73. var savCatDescArrStr = GM.getValue("savCatDescArrStr"); //get array from storage as string
  74. if(savCatDescArrStr) { savCatDescArr = JSON.parse(savCatDescArrStr); } //parse the string to an array
  75. }
  76. mainScript();
  77. })();
  78.  
  79.  
  80. function checkForAndLoadSavedCatDesc() {
  81. var currentCat = $("#category").parent().find(".trigger").text();
  82. var currentSubCat = $("#type").parent().find(".trigger").text();
  83. //alert(currentCat + ": " + currentSubCat);
  84.  
  85. for (i = 0; i < savCatNamArr.length; i++) {
  86. if(savCatNamArr[i][0] == currentCat) { //if saved description for current main cat is found...
  87. for (j = 1; j < savCatNamArr[i].length; j++) {
  88. if(savCatNamArr[i][j] == currentSubCat) { //if saved description for current main SubCat is found...
  89. //alert(savCatDescArr[i][j-1]);
  90. $(".sceditor-toolbar").next("iframe").next("textarea").val(savCatDescArr[i][j-1]);
  91. }
  92. }
  93. }
  94. }
  95. }
  96.  
  97. function alertDesc(cat, subCat) {
  98. for(i = 0; i < savCatNamArr.length; i++) {
  99. if(savCatNamArr[i][0] == cat) {
  100. for(j = 1; j < savCatNamArr[i].length; j++) {
  101. if(savCatNamArr[i][j] == subCat) {
  102. alert("Description:\n\n" + savCatDescArr[i][j-1] + "\n\nAdded for " + savCatNamArr[i][0] + ": " + savCatNamArr[i][j]);
  103. }
  104. }
  105. }
  106. }
  107. }
  108.  
  109. function saveArrays() {
  110. savCatNamArrStr = JSON.stringify(savCatNamArr); //turn array into a single string
  111. GM.setValue("savCatNamArrStr", savCatNamArrStr); //save that string to local storage
  112. savCatDescArrStr = JSON.stringify(savCatDescArr); //turn array into a single string
  113. GM.setValue("savCatDescArrStr", savCatDescArrStr); //save that string to local storage
  114. alert(savCatNamArrStr + "\n\n" + savCatDescArrStr);
  115. }
  116.  
  117. function addToArrays(cat, subCat, desc) {
  118. for(i = 0; i < savCatNamArr.length; i++) {
  119. if(savCatNamArr[i][0] == cat) {
  120. for(j = 1; j < savCatNamArr[i].length; j++) {
  121. if(savCatNamArr[i][j] == subCat) { //if already exists, replace
  122. savCatDescArr[i][j] = desc;
  123. saveArrays();
  124. return;
  125. }
  126. }
  127. //main cat exist, but subCat doesn't
  128. savCatNamArr[i].push(subCat);
  129. savCatDescArr[i].push(desc);
  130. saveArrays();
  131. return;
  132. }
  133. }
  134. //sub nor main cat exists
  135. var newCats = [ cat, subCat ];
  136. savCatNamArr.push(newCats);
  137. var newDescForNewCats = [ desc ];
  138. savCatDescArr.push(newDescForNewCats);
  139. saveArrays();
  140. return;
  141.  
  142. /* Remember!!!
  143. descriptions are stored in an index one lower than their descName counterparts!
  144. i.e.
  145. if Other -> Ebooks is in savCatNamArr[3][2], then it's description is in savCatDescArr[3][1]
  146. because the first item in each sub-array within the savCatNamArr is the name of the main category.
  147. i.e.
  148. if Other -> Ebooks is in savCatNamArr[3][2] then savCatNamArr[3][0] is "Other", while savCatDescArr[3][0] would be the description for savCatNamArr[3][1]
  149. */
  150. }
  151.  
  152. function mainScript() {
  153. $(".box-info-heading.bordered").append(' <div style="float: right;"><label for="showUpCheckB">Show only upload</label><input type="checkbox" id="showUpCheckB"></div> '); //showOnlyUpload setting UI
  154. $("#category").parent().before(' <a href="#" id="reminderOp" style="float: right; font-size: 10px; margin-top: 10px;">Reminder?</a> '); //reminder option UI
  155. $("#type").parent().before(' <a href="#" id="delayOp" style="float: right; font-size: 10px; margin-top: 10px;">Delay?</a> '); //delay option UI
  156.  
  157. $("#delayOp").click(function(e) {
  158. e.preventDefault();
  159. var catDelayTemp = prompt("When you change the main category, there is a delay before the subcategory loads.\nFor some reason this delay can be suprisingly long.\n" +
  160. "The script will check if the current main + sub category has a saved description. Here you can set the delay before that check.\n" +
  161. "i.e. if the subcategory delay is around 2 seconds, then set this to 2.5 for example.\nThis is not a problem when changing the subcategory. The delay is only used when you change the main category." +
  162. "\nCurrent delay is " + catDelay);
  163. if(catDelayTemp != null && catDelayTemp != "" && !(isNaN(catDelayTemp))) {
  164. catDelay = catDelayTemp;
  165. GM.setValue("catDelay", catDelay);
  166. }
  167. });
  168.  
  169. if (reminder) {
  170. $("#category").parent().before("<span id='theReminder' style='color:red; font-size: 15px; margin-left: 5px;'>Choose before editing the description!</span>"); //setting reminder, if true
  171. }
  172.  
  173. $("#reminderOp").click(function(e) { //reminder option changer
  174. e.preventDefault();
  175. var reminderTemp = confirm("Remind me to choose the category before editing the description?\n(Red text next to category)\nWhy? \nBecause changing the category causes the description to change to the saved description for the category you choose.\nMeaning you'll lose any changes you made to the description prior.\n\nPress \"OK\" to show it, \"Cancel\" to not.");
  176. if(!reminderTemp) {
  177. reminder = false;
  178. $("#theReminder").remove();
  179. } else {
  180. reminder = true;
  181. $("#theReminder").remove();
  182. $("#category").parent().before("<span id='theReminder' style='color:red; font-size: 15px; margin-left: 5px;'>Choose before editing the description!</span>");
  183. }
  184. GM.setValue("reminder", reminder);
  185. });
  186.  
  187. if (showOnlyUpload) { //hiding stuff if showOnlyUpload is true
  188. $("h1:contains(' Want to upload a torrent? No problem! Please use the announce URLs: ')").parent().parent().hide();
  189. $("h1:contains('New upload categories')").parent().parent().hide();
  190. $(".upload-info").parent().hide();
  191. $("#showUpCheckB").attr("checked", true);
  192. }
  193.  
  194. $("#showUpCheckB").change(function() { //showOnlyUpload option changer
  195. $("h1:contains(' Want to upload a torrent? No problem! Please use the announce URLs: ')").parent().parent().toggle();
  196. $("h1:contains('New upload categories')").parent().parent().toggle();
  197. $(".upload-info").parent().toggle();
  198. $("#showUpCheckB").attr("checked", !($("#showUpCheckB").attr("checked")));
  199. showOnlyUpload = !showOnlyUpload;
  200. GM.setValue("showOnlyUpload", showOnlyUpload);
  201. });
  202.  
  203. $(document).ready(function() {
  204. $("[name='title']").val(dName); //set default title
  205. $("[name='tags']").val(dTags); //set default tags
  206.  
  207. //description saver UI
  208. $("[name='upload']").parent().append('<input id="savDescDef" style="float: right; margin: 0px 5px;" value="Save Desc (Default)" title="Saves the current description as the global default. (Loaded with the page. Overriden by category specific descriptions.)" class="btn btn-green" type="submit">');
  209. $("[name='upload']").parent().append('<input id="savDescCat" style="float: right;" value="Save Desc (for category)" title="Saves the current description as the default for the currently selected category." class="btn btn-green" type="submit">');
  210.  
  211. //default title, language and tags UI
  212. $("[name='title']").before(' <a href="#" id="savDefTit" style="float: right; margin-top: 10px; font-size: 10px;">Save as default Title</a> ');
  213. $("#language").parent().before(' <a href="#" id="savDefLan" style="float: right; margin-top: 10px; font-size: 10px;">Save as default Language</a> ');
  214. $("[name='tags']").before(' <a href="#" id="savDefTags" style="float: right; margin-top: 10px; font-size: 10px;">Save as default Tags</a> ');
  215.  
  216. $("#savDefTit").click(function(e){
  217. e.preventDefault();
  218. dName = $("[name='title']").val();
  219. GM.setValue("dName", dName);
  220. alert("New default Title saved:\n\n" + dName);
  221. });
  222. $("#savDefLan").click(function(e){
  223. e.preventDefault();
  224. dLanguage = $("#language").parent().find(".trigger").text();
  225. GM.setValue("dLanguage", dLanguage);
  226. alert("New default Language saved:\n\n" + dLanguage);
  227. });
  228. $("#savDefTags").click(function(e){
  229. e.preventDefault();
  230. dTags = $("[name='tags']").val();
  231. GM.setValue("dTags", dTags);
  232. alert("New default Tags saved:\n\n" + dTags);
  233. });
  234.  
  235. setTimeout(function(){ //everything below is done with a delay of 1s, to allow the description iframe to load
  236.  
  237. //$(".sceditor-toolbar").next("iframe").hide();
  238. $(".sceditor-toolbar").next("iframe").next("textarea").attr("style", "width: 100%; height: " + descHeight + "px;"); //setting custom height for text box
  239. $(".sceditor-toolbar").next("iframe").next("textarea").val(dDesc); //setting default description
  240.  
  241. $("[name='description']").before(' <a href="#" id="setDescH" style="float: right; margin-top: 5px; font-size: 10px;">Set description box height</a> ');
  242.  
  243. $("#setDescH").click(function(e) {
  244. e.preventDefault();
  245. var descHeightTemp = prompt("Give a new height for the description box. (in pixels)\nThe current height is: "+descHeight);
  246. if(descHeightTemp != null && descHeightTemp != "") {
  247. descHeight = descHeightTemp;
  248. GM.setValue("descHeight", descHeight);
  249. $(".sceditor-toolbar").next("iframe").next("textarea").attr("style", "width: 100%; height: " + descHeight + "px;");
  250. }
  251. });
  252.  
  253. //setting default Language ---------------------------------------------------
  254. $("#language").parent().find(".options").find("li").each(function() {
  255. $(this).removeAttr("class");
  256. });
  257. $("#language").parent().find(".trigger").text(dLanguage);
  258. $("#language").parent().find(".options").find("li:contains('" + dLanguage + "')").attr("class", "selected");
  259. //----------------------------------------------------------------------------
  260.  
  261. $("#savDescDef").click(function(e) {
  262. e.preventDefault();
  263. //dDesc = $(".sceditor-toolbar").next("iframe").contents().find("body").html();
  264. dDesc = $(".sceditor-toolbar").next("iframe").next("textarea").val();
  265. GM.setValue("dDesc", dDesc);
  266. alert("Description:\n\n"+dDesc+"\n\nSaved as the global default description.");
  267. });
  268. $("#savDescCat").click(function(e) {
  269. e.preventDefault();
  270. var currCat = $("#category").parent().find(".trigger").text();
  271. var currSubCat = $("#type").parent().find(".trigger").text();
  272. var currDesc = $(".sceditor-toolbar").next("iframe").next("textarea").val();
  273. //alert(currCat + ", " + currSubCat + ", \n" + currDesc);
  274. addToArrays(currCat, currSubCat, currDesc);
  275. //alertDesc(currCat, currSubCat);
  276. });
  277. $(".scroll-top").click(function() {
  278. //alert("click");
  279.  
  280. });
  281.  
  282. $("#category").parent().find(".options").click(function() {
  283. setTimeout(checkForAndLoadSavedCatDesc, catDelay*1000); // loading the subcategory after changing main categories takes a while so have to use a delay here.
  284. });
  285.  
  286. $("#type").parent().find(".options").click(checkForAndLoadSavedCatDesc);
  287.  
  288. }, 1000);
  289. });
  290. }
  291. //var isFirefox = typeof InstallTrigger !== 'undefined';
  292.