forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HideVoteCounts.user.js
119 lines (92 loc) · 3.53 KB
/
HideVoteCounts.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// ==UserScript==
// @name Hide Vote Counts
// @description Hides post score until voted
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author @samliew
// @version 1.1
//
// @include https://*stackoverflow.com/*
// @include https://*serverfault.com/*
// @include https://*superuser.com/*
// @include https://*askubuntu.com/*
// @include https://*mathoverflow.net/*
// @include https://*.stackexchange.com/*
//
// @exclude */c/*
// @exclude */admin/*
// @exclude *chat.*
// @exclude *blog.*
//
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
function doPageload() {
// Validation. If rep is too low, do nothing. Rep for guests is 0, so this covers not logged-in users.
if(StackExchange.options.user.rep < 125) return;
// Hide all vote counts immediately on page load
$('.js-voting-container').addClass('js-score-hidden');
// Show vote counts for these scenarios
const showVotes = $('.question, .answer').filter(function() {
const postScore = $(this).find('.js-vote-count').data('value');
return (
// extreme score
postScore <= -200 || postScore >= 200
// deleted posts
|| $(this).hasClass('deleted-answer')
// locked posts (dispute or hist sig)
|| ($(this).find('.question-status').last().text().includes('locked') || $(this).find('.js-vote-up-btn').length == 0)
// own posts
|| $(this).find(`.user-details[itemprop="author"] > a[href^="/users/${StackExchange.options.user.userId}/"]`).length > 0
);
}).find('.js-voting-container').removeClass('js-score-hidden');
// Function to check for votes again when Q&A has initialized
StackExchange.ready(function() {
// If we have voted, show post votes
$('.js-voting-container .fc-theme-primary').parent().removeClass('js-score-hidden');
});
// Show vote counts on double-click
$(document).on('dblclick', '.js-score-hidden', function() {
$(this).removeClass('js-score-hidden');
});
// Listen for events
listenToPageUpdates();
}
function listenToPageUpdates() {
// On any page update
$(document).ajaxComplete(function(event, xhr, settings) {
// Ignore unsuccessful ajax responses
if(xhr.status != 200 && xhr.status != 204) return;
// On vote
if(/\/posts\/\d+\/vote\/\d/.test(settings.url)) {
const pid = Number(settings.url.match(/\/(\d+)\//)[1]);
const votetype = Number(settings.url.match(/\d+$/)[0]);
// Show if voted, hide if unvoted successfully
if(xhr.responseJSON.Success) $(`.js-voting-container[data-post-id="${pid}"]`).toggleClass('js-score-hidden', votetype === 0);
}
});
}
function appendStyles() {
const styles = `
<style>
.js-voting-container.js-score-hidden .js-vote-count {
position: relative;
font-size: 0 !important;
pointer-events: none;
}
.js-voting-container.js-score-hidden .js-vote-count .vote-count-separator {
display: none;
}
.js-voting-container.js-score-hidden .js-vote-count:after {
content: '?';
display: inline-block;
font-size: 1.61538462rem !important;
}
</style>
`;
$('body').append(styles);
}
// On page load
appendStyles();
doPageload();
})();