KAT - Sort Translators

Groups translators by language (only sorts by default)

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

  1. // ==UserScript==
  2. // @name KAT - Sort Translators
  3. // @namespace SortTranslators
  4. // @version 2.01
  5. // @description Groups translators by language (only sorts by default)
  6. // @match http://kat.cr/people/translators/
  7. // @match https://kat.cr/people/translators/
  8. // ==/UserScript==
  9.  
  10. var info = [];
  11. $('.firstColl:first > ul > li').each(function()
  12. {
  13. var all = $(this).html();
  14. var span = $(this).children("span");
  15. $(span).remove();
  16. var language = $(this).html();
  17. info.push({"html":all, "lang":language});
  18. $(this).remove();
  19. });
  20.  
  21. var columnLimit = info.length;
  22. var current = 0;
  23.  
  24. $('.secondColl:first > ul > li').each(function()
  25. {
  26. var all = $(this).html();
  27. var span = $(this).children("span");
  28. $(span).remove();
  29. var language = $(this).html();
  30. info.push({"html":all, "lang":language});
  31. $(this).remove();
  32. });
  33.  
  34. info.sort(function(a, b) {
  35. return a.lang.localeCompare(b.lang);;
  36. });
  37.  
  38. for (i = 0; i < info.length; i++)
  39. {
  40. if (current < columnLimit) { $('.firstColl:first > ul').append('<li>' + info[i].html + '</li>'); }
  41. else { $('.secondColl:first > ul').append('<li>' + info[i].html + '</li>'); }
  42. current++;
  43. }
  44.  
  45. info = [];
  46. $('.thirdColl:first > ul > li').each(function()
  47. {
  48. var all = $(this).html();
  49. var span = $(this).children("span");
  50. $(span).remove();
  51. var language = $(this).html();
  52. info.push({"html":all, "lang":language});
  53. $(this).remove();
  54. });
  55.  
  56. info.sort(function(a, b) {
  57. return a.lang.localeCompare(b.lang);;
  58. });
  59.  
  60. for (i = 0; i < info.length; i++)
  61. {
  62. $('.thirdColl:first > ul').append('<li>' + info[i].html + '</li>');
  63. }
  64.  
  65. var langs = [];
  66. var items = $("div[class$='Coll']:lt(2) > ul > li").contents().filter(function() { return this.nodeType === 3;});
  67. items = items.map(function() {
  68. var text = $(this).text();
  69. text = text.substring(2, text.length - 1);
  70. return text;
  71. });
  72.  
  73. function unique(array) {
  74. return $.grep(array, function(el, index) {
  75. return index === $.inArray(el, array);
  76. });
  77. }
  78.  
  79. items = unique(items);
  80.  
  81. items.sort();
  82.  
  83. $.map(items, function(val)
  84. {
  85. langs[val] = [];
  86. });
  87. langs["All"] = [];
  88.  
  89. var option = '<option value="All">All - ' + $("div[class$='Coll']:lt(2) > ul > li").length + ' translator(s)</option>';
  90.  
  91. $("div[class$='Coll']:lt(2) > ul > li").each(function()
  92. {
  93. var html = '<li><span class="badgeInline">' + $(this).contents().filter(function() { return this.nodeType != 3;}).html() + '</span></li>';
  94. var language = $(this).contents().filter(function() { return this.nodeType === 3;}).text();
  95. language = language.substring(2, language.length - 1);
  96. langs[language].push(html);
  97. });
  98. langs["All"].push($(".firstColl ul").html());
  99. langs["All"].push($(".secondColl").html());
  100.  
  101. console.log(langs);
  102. console.log("There is currently " + $("div[class$='Coll']:lt(2) > ul > li").length + " translators working on " + items.length + " languages");
  103.  
  104. for (var i=0;i<items.length;i++)
  105. {
  106. option += '<option value="'+ items[i] + '">' + items[i] + ' - ' + langs[items[i]].length + ' translator(s)</option>';
  107. }
  108. $('<hr /><select id="translatorLang">' + option + "</select>").prependTo(".firstColl");
  109.  
  110. $("#translatorLang").change(function()
  111. {
  112. var langChosen = $("#translatorLang option:selected").val();
  113. if (langChosen == "All")
  114. {
  115. $(".firstColl ul").html(langs["All"][0]);
  116. $('<div class="secondColl"><hr style="margin-bottom:27px">' + langs["All"][1] + '</p></div>').insertAfter(".firstColl");
  117. }
  118. else
  119. {
  120. $(".firstColl ul").html(langs[langChosen]);
  121. $(".secondColl").remove();
  122. }
  123. });
  124.  
  125. $(".secondColl").remove();
  126. $("#translatorLang").trigger("change");
  127.  
  128. // FORMER TRANSLATORS
  129.  
  130. var langs_former = [];
  131. var items_former = $(".thirdColl > ul > li").contents().filter(function() { return this.nodeType === 3;});
  132. items_former = items_former.map(function() {
  133. var text = $(this).text();
  134. text = text.substring(2, text.length - 1);
  135. return text;
  136. });
  137.  
  138. function unique(array) {
  139. return $.grep(array, function(el, index) {
  140. return index === $.inArray(el, array);
  141. });
  142. }
  143.  
  144. items_former = unique(items_former);
  145. items_former.sort();
  146.  
  147. $.map(items_former, function(val)
  148. {
  149. langs_former[val] = [];
  150. });
  151.  
  152. var option_former = '<option value="All">All - ' + $(".thirdColl > ul > li").length + ' translator(s)</option>';
  153.  
  154. $(".thirdColl > ul > li").each(function()
  155. {
  156. var html = '<li><span class="badgeInline">' + $(this).contents().filter(function() { return this.nodeType != 3;}).html() + '</span></li>';
  157. var language = $(this).contents().filter(function() { return this.nodeType === 3;}).text();
  158. language = language.substring(2, language.length - 1);
  159. langs_former[language].push(html);
  160. });
  161.  
  162. langs_former["All"] = $(".thirdColl ul").html();
  163.  
  164. console.log(langs_former);
  165. console.log($(".thirdColl > ul > li").length + " translators previously worked on " + items_former.length + " languages");
  166.  
  167. for (var i=0;i<items_former.length;i++)
  168. {
  169. option_former += '<option value="'+ items_former[i] + '">' + items_former[i] + ' - ' + langs_former[items_former[i]].length + ' translator(s)</option>';
  170. }
  171. $('<hr /><select id="formerTranslatorLang">' + option_former + "</select>").prependTo(".thirdColl");
  172.  
  173. $("#formerTranslatorLang").change(function()
  174. {
  175. var langChosen = $("#formerTranslatorLang option:selected").val();
  176. $(".thirdColl ul").html(langs_former[langChosen]);
  177. });
  178.  
  179. $("#formerTranslatorLang").trigger("change");