Guest User

Untitled

a guest
May 3rd, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. function query() {
  2. var
  3. // HN is done with very unsemantic classes.
  4. job_list = Array.prototype.slice.call(document.querySelectorAll('.c5a,.cae,.c00,.c9c,.cdd,.c73,.c88')),
  5. query_list = Array.prototype.slice.call(arguments),
  6. shown = 0, total = job_list.length;
  7.  
  8. // Traverses up the dom stack trying to find a match of a specific class
  9. function up_to(node, klass) {
  10. if (node.className === klass) {
  11. return node;
  12. }
  13. if(node === document.body) {
  14. throw new Exception();
  15. }
  16. return up_to(node.parentNode, klass);
  17. }
  18.  
  19. function display(node, what) {
  20. up_to(node, 'athing').style.display = what;
  21. }
  22.  
  23. // If we have a RegEx, return it
  24. // Otherwise make it a case insensitive regex.
  25. function destring(what) {
  26. return what.test ? what : new RegExp(what.toString(), 'i');
  27. }
  28.  
  29. // Hide all the postings
  30. job_list.forEach(function(node) {
  31. display(node, 'none');
  32. });
  33.  
  34. query_list.forEach(function(query) {
  35. if (query.forEach) {
  36. var and_query_list = query.map(destring);
  37.  
  38. job_list.forEach(function(node) {
  39. var
  40. doesMatch = true,
  41. toTest = node.innerHTML;
  42.  
  43. and_query_list.forEach(function(query) {
  44. doesMatch &= toTest.search(query) > -1;
  45. })
  46.  
  47. if(doesMatch) {
  48. display(node, 'block');
  49. shown ++;
  50. }
  51. });
  52.  
  53. } else {
  54. query = destring(query);
  55.  
  56. job_list.forEach(function(node) {
  57. if(node.innerHTML.search(query) > -1) {
  58. display(node, 'block');
  59. shown ++;
  60. }
  61. });
  62. }
  63. });
  64.  
  65. return {shown: shown, total: total}
  66. }
Add Comment
Please, Sign In to add comment