View difference between Paste ID: 9QNzrRnX and tk7YSNYe
SHOW: | | - or go back to the newest paste.
1
2
// ==UserScript==
3
// @name        Puzzling.SE Empuzzler
4
// @namespace   https://greasyfork.org/users/5615-doppelgreener
5
// @description Hide answers and comments on Puzzling.SE questions until you want to see them.
6
// @grant       none
7
// @include     http://puzzling.stackexchange.com/questions/*
8
// @version     0.2
9
// ==/UserScript==
10
11
// Changelog:
12
// 0.3      Cleaned up :-)
13
// 0.2      Show Everything button added.
14
// 0.1      First version, with show comments/answers buttons only.
15
(function() {
16
17
    var main = function() {
18
19
        var identifiers,
20
            styles,
21
            commentsButton,
22
            answersButton,
23
            showAllButton;
24
25
        identifiers = {
26
            'comments': 'show-comments',
27
            'answers': 'show-answers'
28
        };
29
30
        styles = {
31
            'hide-comments': [
32-
                'body:not(.' + identifiers.comments + ') #question .comments        { display: none; }',
32+
                'body:not(.' + identifiers.comments + ') #question .comments            { display: none; }',
33-
                'body:not(.' + identifiers.comments + ') #question .comments-link   { display: none; }',
33+
                'body:not(.' + identifiers.comments + ') #question .comments-link       { display: none; }',
34-
                'body:not(.' + identifiers.comments + ') #question .bounty-link     { display: none; }',
34+
                'body:not(.' + identifiers.comments + ') #question .comments-link ~ *   { display: none; }',
35-
                'body.' + identifiers.comments + ' .' + identifiers.comments + '    { display: none; }'  // hide the button(s)
35+
                'body:not(.' + identifiers.comments + ') #question .bounty-link         { display: none; }',
36
                'body.' + identifiers.comments + ' .' + identifiers.comments + '        { display: none; }'  // hide the button(s)
37
            ],
38-
                'body:not(.' + identifiers.answers + ') #answers                    { display: none; }',
38+
39-
                'body.' + identifiers.answers + ' .' + identifiers.answers + '      { display: none; }'  // hide the button(s)
39+
                'body:not(.' + identifiers.answers + ') #answers                        { display: none; }',
40
                'body.' + identifiers.answers + ' .' + identifiers.answers + '          { display: none; }'  // hide the button(s)
41
            ],
42
            'empuzzler-misc': [
43
                '#empuzzler-revealers button { margin: 0.5em 0.5em 0.5em 0; padding: 0.5em; }',
44
                '#empuzzler-revealers input { margin: 0.5em 0.5em 0.5em 0; }',
45
                '#empuzzler-revealers p { margin: 0.5em; }',
46
            ]
47
        };
48
49
        // Creates a button with chosen type, text and affecting certain classes
50
        function makeButton(buttonType, buttonText, classesToAdd) {
51
            return (
52
                'submit' === buttonType
53
                    ? $('<input/>').attr('type', 'submit').val(buttonText)
54
                    : $('<button/>').text(buttonText)
55
                ).addClass(classesToAdd)
56
                .on('click', function() {
57
                    $('body').addClass(classesToAdd);
58
                });
59
        }
60
61
        // Create the 3 "show x" buttons
62
        commentsButton = makeButton('button', 'Show comments on question', identifiers.comments);
63
        answersButton = makeButton('button', 'Show answers', identifiers.answers);
64
        showAllButton = makeButton('submit', 'Show everything!', identifiers.comments + ' ' + identifiers.answers);
65
66
        // Create a container for the buttons and insert it after the question
67
        $('<div/>')
68
            .attr('id', 'empuzzler-revealers')
69
            .append(
70
                commentsButton,
71
                answersButton,
72
                '<p class="' + identifiers.comments + ' ' + identifiers.answers + '">or</p>',
73
                showAllButton
74
            ).insertAfter('#question');
75
76
        // Add the CSS to the page
77
        for (var i in styles) {
78
            if (styles.hasOwnProperty(i)) {
79
                el = document.createElement('style');
80
                el.id = 'empuzzler-styles';
81
                el.type = 'text/css';
82
                el.textContent = styles[i].join("\n");
83
                (document.head || document.documentElement).appendChild(el);
84
            }
85
        }
86
87
    };
88
89
    // Add the JS to the page
90
    var el = document.createElement('script');
91
    el.type = 'text/javascript';
92
    el.id = 'empuzzler-script'
93
    el.textContent = '(' + main.toString() + ')();';
94
    document.body.appendChild(el);
95
96
})();