forked from Onyx47/TDWTF-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masterscript.js
248 lines (222 loc) · 8.21 KB
/
masterscript.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// ==UserScript==
// @name TDWTF - Userscript masterscript
// @namespace TDWTF
// @match http://what.thedailywtf.com/*
// @description Unifies various userscripts designed for what.thedailywtf.com and adds a menu to manage them. WARNING: Uses cookies. If you're bothered by it, well, tough tits.
// @version 1
// ==/UserScript==
// Globals
userScripts = {
raw: {
active: 'false',
functionName: 'appendRawButton'
},
userstats: {
active: 'false',
functionName: 'appendUserStats'
}
removeheatmap: {
active: 'false',
functionName: 'removePostCountHeatmap'
}
};
// Utility functions
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') { c = c.substring(1); }
if (c.indexOf(name) != -1) { return c.substring(name.length,c.length); }
}
return "";
}
// Script functions
// Gets the raw representation of a post
function getRawPost(e)
{
var topicID = e.data.topicID;
var postID = e.data.postID;
var callingButton = $(e.target);
// Fuck your inconsistency Discourse! Why don't post numbers match??? Now I have to do this shit...
var postArea = $('button[data-post-number="' + postID + '"][data-action="share"]') .closest('.contents');
callingButton.toggleClass('active');
if (callingButton.hasClass('active')) {
//Prettify
callingButton.css({backgroundColor: '#08C', color: '#FFF'});
if (postArea.children('.tdwtf-raw-area') .length === 0 || postArea.children('.tdwtf-raw-area') .html() === '') {
$.get('/raw/' + topicID + '/' + postID) .done(function (content) {
if (postArea.children('.tdwtf-raw-area') .length === 0)
{
postArea.children('.cooked') .after('<pre class="tdwtf-raw-area"></pre>');
postArea.children('.cooked') .hide();
}
postArea.children('.tdwtf-raw-area').css("white-space", "pre-wrap") .text(content);
});
}
else {
postArea.children('.cooked') .hide();
postArea.children('.tdwtf-raw-area') .show();
}
}
else {
//Unprettify :(
callingButton.css({backgroundColor: 'transparent', color: '#A7A7A7'});
postArea.children('.cooked') .show();
postArea.children('.tdwtf-raw-area') .hide();
}
}
// Appends the "Show raw" button
function appendRawButton(postData) {
if(userScripts.raw.active == 'true') {
// This thing triggers for any render, so we check if a post has been rendered
if (postData.post) {
// Get post ID
var postID = postData.post.post_number;
// Get topic ID
var topicID = postData.post.topic_id;
// Find action area for the post
var actionArea = $('button[data-post-number="' + postID + '"][data-action="share"]') .parent('.actions');
var postArea = actionArea.parents('div.contents') .children('.cooked') [0];
// This triggers for every render, so we check for existence of the button
if (actionArea.children('.tdwtf-view-raw') .length === 0) {
// Add the button!
actionArea.prepend('<button title="view raw post" class="tdwtf-view-raw" data-post-id="' + postID + '"><i class="fa fa-code"></i> #' + topicID + ':' + postID + '</button>') .children('.tdwtf-view-raw') .on('click', {
topicID: topicID,
postID: postID
}, getRawPost);
}
}
}
}
// Appends the user statistics
function appendUserStats(postData) {
if(userScripts.userstats.active == 'true')
{
// This thing triggers for any render, so we check if a post has been rendered
if (postData.post) {
// Get post ID
var postID = postData.post.post_number;
// Get user name
var username = postData.post.username;
// Initialize count variables
var postCount = 0;
var badgeCount = 0;
var userDataJsonUrl = 'http://what.thedailywtf.com/users/' + username + '.json';
$.get(userDataJsonUrl) .done(function (data) {
if (data.user) {
badgeCount = data.user.badge_count;
for (var i in data.user.stats) {
if (data.user.stats.hasOwnProperty(i)) {
// Security through obscurity!
if (data.user.stats[i].action_type === 5) {
postCount = data.user.stats[i].count;
}
}
}
$('.tdwtf-post-count[data-username="' + data.user.username + '"]') .html(postCount);
$('.tdwtf-badge-count[data-username="' + data.user.username + '"]') .html(badgeCount);
}
});
// Find avatar area for the post
var avatarArea = $('article[id="post_' + postID + '"]') .children('.row') .children('.topic-avatar');
// Check if the container has already been appended
if (avatarArea.children('.tdwtf-user-stats') .length === 0) {
// Append stats elements
avatarArea.append('<span class="tdwtf-user-stats" data-username="' + username + '" style="font: normal normal 400 10px Arial; color: #A7A7A7;"><i class="fa fa-envelope"></i> <span class="tdwtf-post-count" data-username="' + username + '">' + postCount + '</span><br><i class="fa fa-certificate"></i> <span class="tdwtf-badge-count" data-username="' + username + '">' + badgeCount + '</span>');
}
}
}
}
// Removes the heatmap from post count in topic list
function removePostCountHeatmap() {
$('td.num.posts').attr('class', 'num posts');
}
// Appends the TDWTF script menu
function appendManagementMenu() {
if($('.tdwtf-manager-menu').length === 0) {
$(' <li class="tdwtf-manager-menu"></li>').insertBefore('li.notifications');
$('div.panel.clearfix').append(menu);
$('.tdwtf-manager-toggle').each(function() {
var targetScript = $(this).data('userscript');
var scriptOn = getCookie(targetScript);
if(scriptOn === '') { scriptOn = 'false'; }
//userScriptStatus[targetScript] = scriptOn;
userScripts[targetScript].active = scriptOn;
if(scriptOn === 'false') {
$(this).find('i').removeClass('fa-check-circle').addClass('fa-circle');
}
});
$('.tdwtf-manager-menu').append('<a class="icon tdwtf-manager-menuicon" href="#" title="TDWTF userscripts manager">\
<i class="fa fa-cogs tdwtf-manager-icon"></i>\
</a>').on('click', function(e)
{
e.preventDefault();
$(this).toggleClass('active');
$('#tdwtf-manager-dropdown').toggle();
});
$('.tdwtf-manager-toggle').on('click', function(e) {
e.preventDefault();
var menuItem = $(this).find('.fa');
var targetScript = menuItem.closest('a').data('userscript');
menuItem.toggleClass('fa-circle').toggleClass('fa-check-circle');
if(menuItem.hasClass('fa-check-circle')) {
document.cookie = targetScript + "=true";
userScripts[targetScript].active = 'true';
}
else
{
document.cookie = targetScript + "=false";
userScripts[targetScript].active = 'false';
}
});
}
$('body').on('click', function(e)
{
if($(e.target).attr('class')) {
if( $(e.target).attr('class').indexOf('tdwtf-manager') == -1 ) {
$('#tdwtf-manager-dropdown').hide();
$('.tdwtf-manager-menu').removeClass('active');
}
}
});
}
// Templates
menu = '<section class="d-dropdown" id="tdwtf-manager-dropdown" style="display: none;">\
<ul>\
<li>\
<a href="#" data-userscript="raw" class="tdwtf-manager-toggle">\
<p style="margin: 0;"><i class="fa fa-check-circle"></i> Show raw </p>\
</a>\
</li>\
<li>\
<a href="#" data-userscript="userstats" class="tdwtf-manager-toggle">\
<p style="margin: 0;"><i class="fa fa-check-circle"></i> Show userstats </p>\
</a>\
</li>\
<li>\
<a href="#" data-userscript="removeheatmap" class="tdwtf-manager-toggle">\
<p style="margin: 0;"><i class="fa fa-check-circle"></i> Remove heatmap from post count </p>\
</a>\
</li>\
</ul>\
</section>';
// Event hooks
Ember.View.reopen({
didInsertElement: function () {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.insertManagementMenu);
Ember.run.scheduleOnce('afterRender', this, this.insertRawButton);
Ember.run.scheduleOnce('afterRender', this, this.insertUserStats);
Ember.run.scheduleOnce('afterRender', this, this.removePostHeatmap);
},
insertManagementMenu: function () { appendManagementMenu(); },
insertRawButton: function () {},
insertUserStats: function () {},
removePostHeatmap: function() {}
});
Discourse.View.reopen({
insertRawButton: function () { appendRawButton(this); },
insertUserStats: function () { appendUserStats(this); },
removePostHeatmap: function () { removePostCountHeatmap(); }
});