Guest User

Reddit Hide All

a guest
Feb 9th, 2015
237
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Reddit Hide All
  3. // @namespace http://userscripts.org/users/dbeck
  4. // @description Adds a button next to the logo to Hide All
  5. // @include http://reddit.com/*
  6. // @include http://*.reddit.com/*
  7. // @exclude http://www.reddit.com/comscore-iframe/*
  8. // @exclude http://static.reddit.com/ads/*
  9. // @version 4
  10. // @date 2010.04.20
  11. // @creator reddit@douglasbeck.com
  12. // @homepage http://douglasbeck.com/scripts/reddit/
  13. // ==/UserScript==
  14.  
  15. /*
  16. if you have any comments or suggestions
  17. send a reddit message to: dougletts
  18. */
  19. var code_string = "(" + function() {
  20.  
  21. //helper funciton
  22. function xpath(p, context) {
  23. if(!context) context = document;
  24. var i, arr = [], xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  25. for(i = 0; item = xpr.snapshotItem(i); i++)
  26. arr.push(item);
  27. return arr;
  28. }
  29.  
  30. //grab list at top of page
  31. var list = xpath("//div[@id='header-bottom-left']/ul")[0];
  32. if(typeof list=="undefined"||!list) return;
  33.  
  34. //create link with hide functionality
  35. var link = document.createElement('a');
  36. link.setAttribute('href','#');
  37. link.setAttribute('id','reddit-hide-all');
  38. link.innerHTML = 'hide all';
  39. link.addEventListener('click', function(event){
  40.  
  41. //ajax loading spinner
  42. var spinner = {
  43. lock : 0,
  44. remove : function(){
  45. --spinner.lock;
  46. if(spinner.lock ==0)
  47. $('#reddit-hide-all').css("background", "#EFF7FF");
  48. },
  49. add : function(){
  50. //created ajax spinner with http://www.ajaxload.info/ #EFF7FF and #FF4500 (orangered)
  51. //created data uri with http://www.sveinbjorn.org/dataurlmaker
  52. $('#reddit-hide-all').css("background", 'url("data:image/gif;base64,R0lGODlhEAAQAPIAAO/3//9FAPLMwv'+
  53. 'pzQv9FAPiJYvafgvWqkiH+GkNyZWF0ZWQgd2l0aCBhamF4bG9hZC5pbmZvACH5BAAKAAAAIf8LTkVUU0NBUEUyLjADAQAAACw'+
  54. 'AAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQACgABACwA'+
  55. 'AAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkEAAoAAgAsA'+
  56. 'AAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkEAAoAAw'+
  57. 'AsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkEAAoABAA'+
  58. 'sAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQACgAFACwA'+
  59. 'AAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQACgAGACwAA'+
  60. 'AAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAAKAAcALAAAAA'+
  61. 'AQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==") '+
  62. '#EFF7FF no-repeat center');
  63. }
  64. };
  65.  
  66. // requests are now rate-limited in the default js, so I had to write my own function to process the requests.
  67. // I actually like it better because it hides the link *after* the request is sent/completed, not before.
  68. // I DON'T like how many ajax request this makes so I've submitted a feature request:
  69. // http://code.reddit.com/ticket/576
  70. $.fn.extend({
  71. redditHide: function(op, parameters, link){
  72. $.post(op, parameters, function(){
  73. hide_thing($(link).parents("form"));
  74. spinner.remove();
  75. }, null);
  76. return ++spinner.lock;
  77. }
  78. });
  79.  
  80. //grab & hide all
  81. var links = document.getElementsByTagName('a');
  82. var count = 0;
  83.  
  84. for(var i=0; i<links.length; i++){
  85. if(links[i].innerHTML === 'hide' && $(links[i]).thing().css('display') === 'block'){
  86.  
  87. //add spinner background image
  88. spinner.add();
  89.  
  90. // using just the onclick no longer works :(
  91. // change_state(links[i], 'hide', hide_thing);
  92.  
  93. // looking at: http://code.reddit.com/browser/r2/r2/public/static/
  94. // js/jquery.reddit.js?rev=77e51a304d1b4034614d75c5bf4c07b216400a42#L141
  95. var form = $(links[i]).parents("form");
  96. var id = $(links[i]).thing_id();
  97. var op = '/api/hide';
  98. var parameters = get_form_fields(form, {id: id});
  99. if (reddit.logged) parameters.uh = reddit.modhash;
  100.  
  101. $().redditHide(op, parameters, links[i]);
  102.  
  103. ++count;
  104. }
  105. }
  106. if(count===0){
  107. alert('None Found.');
  108. }
  109.  
  110. // stop default click action
  111. event.stopPropagation();
  112. event.preventDefault();
  113. }, true);
  114.  
  115. //create new list item with link
  116. var item = document.createElement('li');
  117. item.appendChild(link);
  118.  
  119. //insert at top of the list
  120. var topItem = list.getElementsByTagName('li')[0];
  121. list.insertBefore(item, topItem);
  122.  
  123. } + ")()";
  124.  
  125. // workaround for Google Chrome
  126. // I realize this is NOT the nice way to do such things but from what I
  127. // read there's no other way to access Reddit's native JS code
  128. // (maybe one day 'hiding' will be added to the API)
  129. var script = document.createElement('script');
  130. script.type = 'text/javascript';
  131. script.appendChild( document.createTextNode( code_string ) );
  132. document.body.appendChild(script);
RAW Paste Data