Filter Ideabox

Sort ideabox ideas by different methods

  1. // ==UserScript==
  2. // @name Filter Ideabox
  3. // @namespace pxgamer
  4. // @version 0.3
  5. // @description Sort ideabox ideas by different methods
  6. // @author pxgamer
  7. // @include *kat.cr/ideabox/*
  8. // @grant none
  9. // ==/UserScript==
  10. /*jshint multistr: true */
  11.  
  12. (function() {
  13. 'use strict';
  14. var rows = [];
  15.  
  16. $('.buttonsline.floatleft').append('<select id="sortIdeas-select" style="letter-spacing: 0px; width: 220px !important;"><option value="all">Show all</option><option value="az">Sort Alphabetically (A-Z)</option><option value="no">Sort by Age (Newest to Oldest)</option><option value="views">Sort by Views (High to Low)</option></select> <button class="siteButton bigButton" id="sortIdeas"><span>Sort</span></button>');
  17.  
  18. $('.ideaBox').each(function(){
  19. var html = $(this).html();
  20. var title = $('.ideaBoxBody h3 a', $(this)).text();
  21. var age = $('.ideaBoxBody div.lightgrey:last', $(this)).html().split('</span> ')[6].trim();
  22. var views = parseInt($('.ideaBoxBody div.lightgrey:last', $(this)).html().split('</span> ')[2].split(' views')[0], 10);
  23. rows.push({"title":title,"age":age,"views":views,"html":html});
  24. });
  25.  
  26. console.log(rows);
  27. $('#sortIdeas').on('click', function() {
  28. var ideaSortType = $('#sortIdeas-select').val();
  29.  
  30. switch (ideaSortType) {
  31. case 'all':
  32. $('div.ideaBox').show();
  33. break;
  34. case 'az': // Sort alphabetically (A-Z)
  35. $('div.ideaBox').show();
  36. sortIdeas('title', 'desc');
  37. break;
  38. case 'no': // Sort by age (Newest to Oldest)
  39. $('div.ideaBox').show();
  40. sortIdeas('age', 'desc');
  41. break;
  42. case 'views': // Sort by views (High to Low)
  43. $('div.ideaBox').show();
  44. sortIdeas('views', 'desc');
  45. break;
  46. default:
  47. $('div.ideaBox').show();
  48. break;
  49. }
  50. });
  51.  
  52. function sortByKey(array, key) {
  53. return array.sort(function(a, b) {
  54. var x = a[key];
  55. var y = b[key];
  56. if (typeof x == "string") {
  57. x = x.toLowerCase();
  58. y = y.toLowerCase();
  59. }
  60. return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  61. });
  62. }
  63.  
  64. function sortIdeas(sortName, sortType) {
  65. sortByKey(rows, sortName);
  66. if (sortName !== 'views') { rows.reverse(); }
  67. $('div.ideaBox').remove();
  68. for (var i=0;i<rows.length;i++) {
  69. $('.buttonsline.floatleft').after('<div class="ideaBox">'+rows[i].html+'</div>');
  70. }
  71.  
  72. }
  73. })();