View difference between Paste ID: j64sWkXb and GKNEari9
SHOW: | | - or go back to the newest paste.
1-
// ==UserScript==
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      By Joe: less stuff gets hidden (eg. post-answer box), moved hide/show answers beneath #answers-header,
12
//              fixed English grammar on buttons, show number of comments that have been hidden, stopped a button
13
//              showing if there is nothing for it to reveal (no answers means show-answer button never appears)
14
// 0.3      Buttons made larger. Added auto-update. Merged clean-up by Joe (Puzzling.SE user 2518).
15
// 0.2      Show Everything button added.
16
// 0.1      First version, with show comments/answers buttons only.
17
(function() {
18
19
    var main = function() {
20
21
        var identifiers,
22
            styles,
23
            questionCommentsCount = $('#question').find('.comment').length,
24
            answersCount = $('#answers').find('.answer').length,
25
            commentsButton,
26
            answersButton,
27
            showAllButton;
28
29
        if ($('#question').find('.comments-link.js-show-link b').length) {
30
            questionCommentsCount += parseInt( $('#question').find('.comments-link.js-show-link b').text() );
31
        }
32
33
        identifiers = {
34
            'comments': 'show-comments',
35
            'answers': 'show-answers'
36
        };
37
        identifiers.both = identifiers.comments + ' ' + identifiers.answers
38
39
        styles = {
40
            'hide-comments': [
41
                'body:not(.' + identifiers.comments + ') #question .comments            { display: none; }',
42
                'body:not(.' + identifiers.comments + ') #question .comments-link       { display: none; }',
43
                'body:not(.' + identifiers.comments + ') #question .comments-link ~ *   { display: none; }',
44
                'body:not(.' + identifiers.comments + ') #question .bounty-link         { display: none; }',
45
                'body.' + identifiers.comments + ' .' + identifiers.comments + '        { display: none; }'  // hide the button(s)
46
            ],
47
            'hide-answers': [
48
                'body:not(.' + identifiers.answers + ') #answers .answer                { display: none; }',
49
                'body:not(.' + identifiers.answers + ') #answers-header + .empuzzler    { margin-top: 1em; }',
50
                'body.' + identifiers.answers + ' .' + identifiers.answers + '          { display: none; }'  // hide the button(s)
51
            ],
52
            'empuzzler-misc': [
53
                '.empuzzler button { margin: 0.5em; padding: 0.5em; }',
54
                '.empuzzler button:first-child { margin-left: 0; }'
55
            ]
56
        };
57
58
        // Creates a button with chosen type, text and affecting certain classes
59
        function makeButton(buttonText, classesToAdd) {
60
            return $('<button/>')
61
                .text(buttonText)
62
                .addClass(classesToAdd)
63
                .on('click', function() {
64
                    $('body').addClass(classesToAdd);
65
                });
66
        }
67
68
        // Create a container for the buttons and insert it after the question
69
        if (questionCommentsCount) {
70
            commentsButton = makeButton(
71
                'Show the ' + questionCommentsCount + ' comment' + (~-questionCommentsCount ? 's' : '') + ' on this question',
72
                identifiers.comments
73
            ).on('click', function() {
74
                $('#question').find('.comments-link.js-show-link').trigger('click');
75
            });
76
            showAllButton = makeButton('Show me everything!', identifiers.both)
77
                .addClass('button')
78
                .on('click', function() {
79
                    commentsButton.trigger('click');
80
                });
81
82
            $('<div/>')
83-
                .addClass('empuzzler')
83+
                .addClass('empuzzler ' + identifiers.comments)
84
                .append(
85
                    commentsButton,
86
                    answersCount ? $('<span/>').addClass(identifiers.both).text(' or ') : '',
87
                    answersCount ? showAllButton : ''
88
                )
89
                .insertAfter('#question');
90
            }
91
92
        if (answersCount) {
93
            answersButton = makeButton('Show me the answer' + (~-answersCount ? 's' : ''), identifiers.answers);
94
95
            $('<blockquote/>')
96
                .addClass('empuzzler ' + identifiers.answers)
97
                .append(
98
                    $('<p/>').text(
99
                        (~-answersCount ? 'These answers have' : 'This answer has')
100
                        + ' been hidden by Empuzzler so you don\'t accidentally spoil the question for yourself. To bring '
101
                        + (~-answersCount ? 'them' : 'it')
102
                        + ' back, just click...'
103
                    ),
104
                    answersButton
105
                )
106
                .insertAfter('#answers-header');
107
        }
108
109
        // Add the CSS to the page
110
        for (var i in styles) {
111
            if (styles.hasOwnProperty(i)) {
112
                el = document.createElement('style');
113
                el.id = 'empuzzler-styles';
114
                el.type = 'text/css';
115
                el.textContent = styles[i].join("\n");
116
                (document.head || document.documentElement).appendChild(el);
117
            }
118
        }
119
120
    };
121
122
    // Add the JS to the page
123
    var el = document.createElement('script');
124
    el.type = 'text/javascript';
125
    el.id = 'empuzzler-script'
126
    el.textContent = '(' + main.toString() + ')();';
127
    document.body.appendChild(el);
128
129
})();