Advertisement
Guest User

Empuzzler 0.4.1

a guest
Oct 9th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Puzzling.SE Empuzzler
  3. // @namespace https://greasyfork.org/users/5615-doppelgreener
  4. // @description Hide answers and comments on Puzzling.SE questions until you want to see them.
  5. // @grant none
  6. // @include http://puzzling.stackexchange.com/questions/*
  7. // @version 0.3
  8. // ==/UserScript==
  9.  
  10. // Changelog:
  11. // 0.4.1 By Joe: don't bother hiding the comments section when there are none, and same for answers
  12. // 0.4 By Joe: less stuff gets hidden (eg. post-answer box), moved hide/show answers beneath #answers-header,
  13. // fixed English grammar on buttons, show number of comments that have been hidden, stopped a button
  14. // showing if there is nothing for it to reveal (no answers means show-answer button never appears)
  15. // 0.3 Buttons made larger. Added auto-update. Merged clean-up by Joe (Puzzling.SE user 2518).
  16. // 0.2 Show Everything button added.
  17. // 0.1 First version, with show comments/answers buttons only.
  18. (function() {
  19.  
  20. var main = function() {
  21.  
  22. var identifiers,
  23. styles,
  24. questionCommentsCount = $('#question').find('.comment').length,
  25. answersCount = $('#answers').find('.answer').length,
  26. commentsButton,
  27. answersButton,
  28. showAllButton;
  29.  
  30. if ($('#question').find('.comments-link.js-show-link b').length) {
  31. questionCommentsCount += parseInt( $('#question').find('.comments-link.js-show-link b').text() );
  32. }
  33.  
  34. identifiers = {
  35. 'comments': 'show-comments',
  36. 'answers': 'show-answers'
  37. };
  38. identifiers.both = identifiers.comments + ' ' + identifiers.answers
  39.  
  40. styles = {
  41. 'hide-comments': [
  42. 'body:not(.' + identifiers.comments + ') #question .comments { display: none; }',
  43. 'body:not(.' + identifiers.comments + ') #question .comments-link { display: none; }',
  44. 'body:not(.' + identifiers.comments + ') #question .comments-link ~ * { display: none; }',
  45. 'body:not(.' + identifiers.comments + ') #question .bounty-link { display: none; }',
  46. 'body.' + identifiers.comments + ' .' + identifiers.comments + ' { display: none; }' // hide the button(s)
  47. ],
  48. 'hide-answers': [
  49. 'body:not(.' + identifiers.answers + ') #answers .answer { display: none; }',
  50. 'body:not(.' + identifiers.answers + ') #answers-header + .empuzzler { margin-top: 1em; }',
  51. 'body.' + identifiers.answers + ' .' + identifiers.answers + ' { display: none; }' // hide the button(s)
  52. ],
  53. 'empuzzler-misc': [
  54. '.empuzzler button { margin: 0.5em; padding: 0.5em; }',
  55. '.empuzzler button:first-child { margin-left: 0; }'
  56. ]
  57. };
  58.  
  59. // Creates a button with chosen type, text and affecting certain classes
  60. function makeButton(buttonText, classesToAdd) {
  61. return $('<button/>')
  62. .text(buttonText)
  63. .addClass(classesToAdd)
  64. .on('click', function() {
  65. $('body').addClass(classesToAdd);
  66. });
  67. }
  68.  
  69. // Create a container for the buttons and insert it after the question
  70. if (questionCommentsCount) {
  71. commentsButton = makeButton(
  72. 'Show the ' + questionCommentsCount + ' comment' + (~-questionCommentsCount ? 's' : '') + ' on this question',
  73. identifiers.comments
  74. ).on('click', function() {
  75. $('#question').find('.comments-link.js-show-link').trigger('click');
  76. });
  77. showAllButton = makeButton('Show me everything!', identifiers.both)
  78. .addClass('button')
  79. .on('click', function() {
  80. commentsButton.trigger('click');
  81. });
  82.  
  83. $('<div/>')
  84. .addClass('empuzzler ' + identifiers.comments)
  85. .append(
  86. commentsButton,
  87. answersCount ? $('<span/>').addClass(identifiers.both).text(' or ') : '',
  88. answersCount ? showAllButton : ''
  89. )
  90. .insertAfter('#question');
  91. } else {
  92. $('body').addClass( identifiers.comments );
  93. }
  94.  
  95. if (answersCount) {
  96. answersButton = makeButton('Show me the answer' + (~-answersCount ? 's' : ''), identifiers.answers);
  97.  
  98. $('<blockquote/>')
  99. .addClass('empuzzler ' + identifiers.answers)
  100. .append(
  101. $('<p/>').text(
  102. (~-answersCount ? 'These answers have' : 'This answer has')
  103. + ' been hidden by Empuzzler so you don\'t accidentally spoil the question for yourself. To bring '
  104. + (~-answersCount ? 'them' : 'it')
  105. + ' back, just click...'
  106. ),
  107. answersButton
  108. )
  109. .insertAfter('#answers-header');
  110. } else {
  111. $('body').addClass( identifiers.answers );
  112. }
  113.  
  114. // Add the CSS to the page
  115. for (var i in styles) {
  116. if (styles.hasOwnProperty(i)) {
  117. el = document.createElement('style');
  118. el.id = 'empuzzler-styles';
  119. el.type = 'text/css';
  120. el.textContent = styles[i].join("\n");
  121. (document.head || document.documentElement).appendChild(el);
  122. }
  123. }
  124.  
  125. };
  126.  
  127. // Add the JS to the page
  128. var el = document.createElement('script');
  129. el.type = 'text/javascript';
  130. el.id = 'empuzzler-script'
  131. el.textContent = '(' + main.toString() + ')();';
  132. document.body.appendChild(el);
  133.  
  134. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement