Sort alphabetically

Sorts torrents alphabetically as an option

当前为 2018-02-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Sort alphabetically
  3. // @namespace pxgamer
  4. // @description Sorts torrents alphabetically as an option
  5. // @include *kat.cr/new/*
  6. // @include *kat.cr/movies/*
  7. // @include *kat.cr/games/*
  8. // @include *kat.cr/tv/*
  9. // @include *kat.cr/books/*
  10. // @include *kat.cr/applications/*
  11. // @include *kat.cr/music/*
  12. // @include *kat.cr/anime/*
  13. // @include *kat.cr/other/*
  14. // @include *kat.cr/xxx/*
  15. // @include *kat.cr/usearch/*
  16. // @version 1.4
  17. // @grant none
  18. // ==/UserScript==
  19.  
  20. var rows = [];
  21. $('.data').before('<div class="buttonsline"><select disabled id="field-select" style="letter-spacing: 0px; width: 160px !important;"><option value="title">Sort by Torrent Name</option></select><select id="field-sort" style="letter-spacing: 0px; width: 150px !important;"><option value="asc">Ascending</option><option value="desc">Descending</option></select> <button class="siteButton bigButton" id="sortTable"><span>Sort</span></button></div>');
  22. $(document).delegate('#sortTable', 'click', function() {
  23. var sortName = $('#field-select').val();
  24. var sortType = ($('#field-sort').val()=="asc");
  25. sortTable(sortName, sortType);
  26. });
  27. $('.data tr[id^="torrent_"]').each(function() {
  28. var title = $('.cellMainLink', $(this)).text();
  29. var html = $(this).html();
  30. rows.push({"title":title, "html":html});
  31. });
  32. function sortByKey(array, key) {
  33. return array.sort(function(a, b) {
  34. var x = a[key];
  35. var y = b[key];
  36. if (typeof x == "string") {
  37. x = x.toLowerCase();
  38. y = y.toLowerCase();
  39. }
  40. return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  41. });
  42. }
  43. function sortTable(sortName, sortType) {
  44. sortByKey(rows, sortName);
  45. if (!sortType) { // Not sortType / is false
  46. rows.reverse();
  47. }
  48. $('.data tr[id^="torrent_"]').remove();
  49. for (var i=0;i<rows.length;i++) {
  50. $('.data').append('<tr id="torrent_'+i+'">'+rows[i].html+'</tr>');
  51. }
  52.  
  53. }