ZeroCruft

Removes the cruft from the new zerohedge

  1. // ==UserScript==
  2. // @name ZeroCruft
  3. // @version 2017.01.17
  4. // @namespace 1bae28cfed0eddb302b3ac9b578412a3
  5. // @author Dan Garthwaite <dan@garthwaite.org>
  6. // @description Removes the cruft from the new zerohedge
  7. // @include https://*zerohedge.com/*
  8. // @run-at document-end
  9. // @require https://code.jquery.com/jquery-2.1.4.min.js
  10.  
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. document.getElementsByClassName('sidebar-left-wrapper')[0].remove();
  16. document.getElementsByClassName('sidebar-right-wrapper')[0].remove();
  17. var articles = document.querySelectorAll('#block-zerohedge-content .view-articles .views-row');
  18.  
  19. // Delete all '.custom-flex' nodes even if added dynamically
  20. document.body.addEventListener('DOMSubtreeModified', function(event) {
  21. var elements = document.getElementsByClassName('ad-mobile-frontpage');
  22. while (elements.length > 0) elements[0].remove();
  23. elements = document.getElementsByClassName('custom-flex');
  24. while (elements.length > 0) elements[0].remove();
  25. articles = document.querySelectorAll('#block-zerohedge-content .view-articles .views-row');
  26. });
  27.  
  28. /* Do jk navigation */
  29. document.querySelector('head').innerHTML += '<style>.jk-current {outline: 1px solid navy;}</style>';
  30.  
  31. const jk = function(inc) {
  32. if (!articles.length) { return;}
  33. for (var i = 0, len = articles.length; i <= len; i++) {
  34. if (articles[i].classList.contains('jk-current')) {
  35. var next = (i + inc) % len;
  36. next = (next > 0) ? next : 0;
  37. articles[i].classList.toggle('jk-current');
  38. articles[next].classList.toggle('jk-current');
  39. break;
  40. }
  41. }
  42. };
  43.  
  44. var foundSticky = false;
  45. for (var i=0, len=articles.length; i<len; i++) {
  46. var article = articles[i];
  47. if (!foundSticky && article.firstElementChild.classList.contains('node--sticky')) {
  48. article.className += ' jk-current';
  49. jk(i);
  50. foundSticky = true;
  51. break;
  52. }
  53. }
  54.  
  55. if (!foundSticky) {
  56. articles[0].className += ' jk-current';
  57. jk(0);
  58. }
  59.  
  60. var open_article = function() {
  61. $(".jk-current").find('a')[0].click();
  62. };
  63.  
  64. const keyDownTextField = function (event) {
  65. // empty article list or keypresses in input fields
  66. if (document.activeElement.tagName == 'INPUT' || articles.length === 0) {
  67. return;
  68. }
  69.  
  70. let key = String.fromCharCode(event.which).toUpperCase();
  71. if (event.which == "13")
  72. key = "13";
  73.  
  74. switch (key) {
  75. case "J":
  76. case " ":
  77. jk(1);
  78. break;
  79. case "K":
  80. jk(-1);
  81. break;
  82. case "O":
  83. case "13":
  84. open_article();
  85. break;
  86. }
  87.  
  88. //scroll .jk-current to top of window
  89. var current = $(".jk-current");
  90. if (current) {
  91. $('html, body').animate({
  92. scrollTop: $(".jk-current").offset().top - 100
  93. }, 200);
  94. } else {
  95. console.log('No jk-current found');
  96. }
  97. };
  98. document.addEventListener("keydown", keyDownTextField, false);
  99. })();