1337x - Default descriptions for categories and more

let's you set up default descriptions for categories and other defaults like title, tags etc.

当前为 2018-01-13 提交的版本,查看 最新版本

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