Advertisement
Guest User

Empuzzler 0.5

a guest
Dec 5th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.5
  8. // ==/UserScript==
  9.  
  10. // Changelog:
  11. // 0.5      By Joe: Added the option to only enable Empuzzler on specific tags, and the option to automatically
  12. //                  show all spoilers.
  13. // 0.4.1    By Joe: Don't bother hiding the comments section when there are none, and same for answers.
  14. // 0.4      By Joe: Less stuff gets hidden (eg. post-answer box), moved hide/show answers beneath #answers-header,
  15. //                  fixed English grammar on buttons, show number of comments that have been hidden, stopped a button
  16. //                  showing if there is nothing for it to reveal (no answers means show-answer button never appears)
  17. // 0.3      Buttons made larger. Added auto-update. Merged clean-up by Joe (Puzzling.SE user 2518).
  18. // 0.2      Show Everything button added.
  19. // 0.1      First version, with show comments/answers buttons only.
  20.  
  21. (function() {
  22.  
  23.     /**
  24.      * If you'd like to only enable Empuzzler if a question has a particular tag, then
  25.      * just fill up this array with the exact tags you want to match (case-sensitive):
  26.      *     var tags = [ 'riddle', 'challenge' ];
  27.      * If you instead choose to leave this empty:
  28.      *     var tags = [ ];
  29.      * then Empuzzler will be enabled on every question on Puzzling.SE
  30.      */
  31.     var tags = [ ];
  32.  
  33.     /**
  34.      * If you'd like to remove spoiler-tags, enable this setting. It will affect every
  35.      * single spoiler tag on the whole site, and just turns them into standard quotes
  36.      * ie. if someone made this post:
  37.      *     >! Spoilers in here!
  38.      * it will appear as if they'd made this post:
  39.      *     > Spoilers in here!
  40.      */
  41.     var despoil = false;
  42.  
  43.     /****************************************************************************************
  44.      * DON'T TOUCH ANYTHING BELOW THIS LINE :-)                                             *
  45.      ***************************************************************************************/
  46.  
  47.     var main = function() {
  48.  
  49.         var identifiers,
  50.             styles,
  51.             questionCommentsCount = $('#question').find('.comment').length,
  52.             answersCount = $('#answers').find('.answer').length,
  53.             commentsButton,
  54.             answersButton,
  55.             showAllButton;
  56.  
  57.         if ($('#question').find('.comments-link.js-show-link b').length) {
  58.             questionCommentsCount += parseInt( $('#question').find('.comments-link.js-show-link b').text() );
  59.         }
  60.  
  61.         identifiers = {
  62.             'comments': 'show-comments',
  63.             'answers': 'show-answers'
  64.         };
  65.         identifiers.both = identifiers.comments + ' ' + identifiers.answers
  66.  
  67.         styles = {
  68.             'hide-comments': [
  69.                 'body:not(.' + identifiers.comments + ') #question .comments            { display: none; }',
  70.                 'body:not(.' + identifiers.comments + ') #question .comments-link       { display: none; }',
  71.                 'body:not(.' + identifiers.comments + ') #question .comments-link ~ *   { display: none; }',
  72.                 'body:not(.' + identifiers.comments + ') #question .bounty-link         { display: none; }',
  73.                 'body.' + identifiers.comments + ' .' + identifiers.comments + '        { display: none; }'  // hide the button(s)
  74.             ],
  75.             'hide-answers': [
  76.                 'body:not(.' + identifiers.answers + ') #answers .answer                { display: none; }',
  77.                 'body:not(.' + identifiers.answers + ') #answers-header + .empuzzler    { margin-top: 1em; }',
  78.                 'body.' + identifiers.answers + ' .' + identifiers.answers + '          { display: none; }'  // hide the button(s)
  79.             ],
  80.             'empuzzler-misc': [
  81.                 '.empuzzler button { margin: 0.5em; padding: 0.5em; }',
  82.                 '.empuzzler button:first-child { margin-left: 0; }'
  83.             ]
  84.         };
  85.  
  86.         // Creates a button with chosen type, text and affecting certain classes
  87.         function makeButton(buttonText, classesToAdd) {
  88.             return $('<button/>')
  89.                 .text(buttonText)
  90.                 .addClass(classesToAdd)
  91.                 .on('click', function() {
  92.                     $('body').addClass(classesToAdd);
  93.                 });
  94.         }
  95.  
  96.         // Create a container for the buttons and insert it after the question
  97.         if (questionCommentsCount) {
  98.             commentsButton = makeButton(
  99.                 'Show the ' + questionCommentsCount + ' comment' + (~-questionCommentsCount ? 's' : '') + ' on this question',
  100.                 identifiers.comments
  101.             ).on('click', function() {
  102.                 $('#question').find('.comments-link.js-show-link').trigger('click');
  103.             });
  104.             showAllButton = makeButton('Show me everything!', identifiers.both)
  105.                 .addClass('button')
  106.                 .on('click', function() {
  107.                     commentsButton.trigger('click');
  108.                 });
  109.  
  110.             $('<div/>')
  111.                 .addClass('empuzzler ' + identifiers.comments)
  112.                 .append(
  113.                     commentsButton,
  114.                     answersCount ? $('<span/>').addClass(identifiers.both).text(' or ') : '',
  115.                     answersCount ? showAllButton : ''
  116.                 )
  117.                 .insertAfter('#question');
  118.         } else {
  119.             $('body').addClass( identifiers.comments );
  120.         }
  121.  
  122.         if (answersCount) {
  123.             answersButton = makeButton('Show me the answer' + (~-answersCount ? 's' : ''), identifiers.answers);
  124.  
  125.             $('<blockquote/>')
  126.                 .addClass('empuzzler ' + identifiers.answers)
  127.                 .append(
  128.                     $('<p/>').text(
  129.                         (~-answersCount ? 'These answers have' : 'This answer has')
  130.                         + ' been hidden by Empuzzler so you don\'t accidentally spoil the question for yourself. To bring '
  131.                         + (~-answersCount ? 'them' : 'it')
  132.                         + ' back, just click...'
  133.                     ),
  134.                     answersButton
  135.                 )
  136.                 .insertAfter('#answers-header');
  137.         } else {
  138.             $('body').addClass( identifiers.answers );
  139.         }
  140.  
  141.         // Add the CSS to the page
  142.         for (var i in styles) {
  143.             if (styles.hasOwnProperty(i)) {
  144.                 el = document.createElement('style');
  145.                 el.id = 'empuzzler-styles';
  146.                 el.type = 'text/css';
  147.                 el.textContent = styles[i].join("\n");
  148.                 (document.head || document.documentElement).appendChild(el);
  149.             }
  150.         }
  151.  
  152.     };
  153.  
  154.     // Always kick in if tags is empty
  155.     var kickIn = ! tags.length;
  156.    
  157.     if ( ! kickIn) {
  158.         // Find out what tags are on the post
  159.         var postTags = [];
  160.         $('#mainbar')
  161.             .find('.post-taglist a')
  162.             .each( function() {
  163.                 postTags.push( $(this).text() );
  164.             } );
  165.  
  166.         // Should we hide answers, based on target tags?
  167.         $.each(tags, function(i, tag) {
  168.             if (~$.inArray(tag, postTags)) {
  169.                 kickIn = true;
  170.                 return false;
  171.             }
  172.         } );
  173.     }
  174.  
  175.     if (kickIn) {
  176.         // Add the JS to the page
  177.         var el = document.createElement('script');
  178.         el.type = 'text/javascript';
  179.         el.id = 'empuzzler-script'
  180.         el.textContent = '(' + main.toString() + ')();';
  181.         document.body.appendChild(el);
  182.     }
  183.  
  184.     // Unhide spoiler tags?
  185.     if (despoil) {
  186.         $('.answer').find('blockquote.spoiler').removeClass('spoiler');
  187.     }
  188.  
  189. } )();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement