forked from OverlordQ/Reddit-Pro-Tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rpt.js
444 lines (350 loc) · 11.1 KB
/
rpt.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
"use strict";
// const printLogs = false;
const printLogs = true;
var settings;
var db;
var users = {};
var domains = [];
// var working = {};
// var startTime = Date.now();
// var redditVersion = 'old';
const day = 60 * 60 * 24; // one day
// const cacheTime = day;
const cacheTime = 0;
//const cacheTime = 60;
let numUsers = 0;
let usersWorking = false;
let domainsWorking = false;
// add google analytics
ga('create', 'UA-124046785-1', 'auto');
ga('send', 'pageview');
$(document).ready(function() {
getSettings();
addRemovedditLink();
addPatreonLink();
// give the settings and the db a extra blink of time to load
setTimeout(function() {
rptMain();
}, 100);
// find new comments
numUsers = getNumUsers();
setTimeout(function() {
setInterval(function() {
checkNewComments();
}, 500)
}, 5000);
});
function rptMain() {
whenFinished(Date.now());
if (!settings) {
console.log('RPT: Settings Loading... ');
setTimeout(function() {
rptMain();
}, 100);
return;
}
if (!db) {
console.log('RPT: DB Loading... ');
setTimeout(function() {
rptMain();
}, 1000);
return;
}
// get list of users on page
let authors = getAuthors();
authors.forEach(function(user) {
if (!users[user]) {
users[user] = new User(user);
}
if (!users[user].working) {
users[user].working = true;
setTimeout(function() {
users[user].addTags();
}, Math.random() * 1000);
} else {
printLog('working:', user);
}
});
addDomainTags();
}
// check if new comments have been loaded onto the page
function checkNewComments() {
let users = getNumUsers();
if (numUsers != users) {
// console.log('new comments loaded');
numUsers = users;
// if so, run the main loop again
rptMain();
}
}
function getNumUsers() {
let userElems = document.evaluate(
'//a[' + userElemEval + ']',
document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
let i = 0;
while (userElems.snapshotItem(i)) {
i++;
}
return i;
}
function whenFinished(timer) {
if (!checkFinished()) {
//console.log('not finished...');
setTimeout(function() {
whenFinished(timer);
}, 100);
return;
}
// let timeElapsed = Math.round((Date.now() - startTime) / 1000);
// let elapsed = Math.round((Date.now() - timer) / 1000);
// console.log('finished:', Date.now() - timer);
// console.log('');
let store = navigator.storage.estimate().then((data) => {
handleQuota(data);
});
}
function checkFinished() {
// if (Object.keys(working).length <= 0) { return false; }
// users.forEach(function(user) {
for (let user in users) {
if (users[user].working) {
return false;
}
}
return true;
}
function numPretty(num) {
num = Math.floor(num).toString();
let nums = num.split('');
for (var i = num.length - 3; i >= 1; i -= 3) {
if (nums[i - 1] == '-') {
continue;
}
nums.splice(i, 0, ',');
}
return nums.join('');
}
function printLog() {
if (!printLogs) {
return;
}
var log = '';
// for (let part in arguments) {
for (let i = 0; i < arguments.length; i++) {
if (i != 0) {
log += ' ';
}
log += arguments[i];
}
console.log(log);
}
////
// add snoopsnoo link to the RES user mouse over div
// event triggered by elements being loaded into the page
$(document).on('DOMNodeInserted', function(e) {
if (typeof e.target.className != 'string') {
return;
}
let classes = e.target.className.split(' ');
// if element loading is the RES user mouseover thingie
if (classes.includes('RESHoverTitle')) {
addToRESHover(e.target);
}
});
function addToRESHover(elem) {
// wait for RES and the Reddit API
if (elem.childNodes[0].childNodes.length == 0) {
setTimeout(function() {
addToRESHover(elem);
}, 10);
return;
}
let user = elem.childNodes[0].textContent.match(/\/u\/(.*)/)[1];
let title = elem.childNodes[0];
let body = elem.parentNode.childNodes[5].childNodes[0];
// let zIndex = elem.parentNode.style.zIndex;
// add SnoopSnoo link
let snoopSnooLink = document.createElement('a');
snoopSnooLink.href = 'https://snoopsnoo.com/u/' + user;
snoopSnooLink.textContent = 'SnoopSnoo';
title.style.whiteSpace = 'nowrap';
title.style.fontSize = 'smaller';
title.childNodes[6].after('(', snoopSnooLink, ')');
// add RPT Stats
let fieldPair = document.createElement('div');
fieldPair.className = 'fieldPair';
let fieldPairLabel = document.createElement('div');
fieldPairLabel.className = 'fieldPair-label';
fieldPairLabel.textContent = 'RPT Stats:';
let fieldPairText = document.createElement('div');
fieldPairText.className = 'fieldPair-text';
let rptPos = users[user].tagSpan('rptStats', 'RPT+');
let rptNeg = users[user].tagSpan('rptStats', 'RPT-');
fieldPairText.append(rptPos, rptNeg);
fieldPair.append(fieldPairLabel, fieldPairText);
body.prepend(fieldPair);
// console.log(elem.parentNode.style.zIndex);
}
function addSnoopSnooTag() {
let elem = $($('.RESHoverTitle > div')[0]);
// the children are rendered from an api call.
// if they haven't been loaded...
if (elem.children().length < 4) {
// do the recursion dance
setTimeout(function() {
addSnoopSnooTag()
}, 10);
return;
}
console.log(elem.parent().parent().attr('class'));
elem.parent().parent().css('width', '530px');
elem.css('white-space', 'nowrap');
let children = elem.children();
let user = children[0].text.replace(/^\/u\//i, '');
let last = children[(children.length - 1)];
children[(children.length - 1)].remove();
last.style.marginLeft = '3px';
let snoopSnoo = $('<span/>').addClass('snoopSnoo').append('(', $('<a/>').attr('href', 'https://snoopsnoo.com/u/' + user).text('SnoopSnoo'), ')');
let rptPos = users[user].tagSpan('rptStats', 'RPT+');
let rptNeg = users[user].tagSpan('rptStats', 'RPT-');
let wrapper = $('<span/>');
// wrapper.css('margin-left', '3px');
wrapper.append(snoopSnoo, rptPos, rptNeg, last);
elem.append(wrapper);
elem.css('font-size', 'smaller');
}
// function addCedditLink() {
// let img = chrome.extension.getURL('images/ceddit.png');
// let url = window.location.href.split('/');
// url[2] = 'www.ceddit.com';
// url = url.join('/');
// $('#header-bottom-right').append('<span class="separator">|</span><a href="' + url + '"><img class="ceddit" src="' + img + '"></a>');
// }
function addRemovedditLink() {
let img = chrome.extension.getURL('images/removeddit.png');
let url = window.location.href.split('/');
url[2] = 'www.removeddit.com';
url = url.join('/');
$('#header-bottom-right').append('<span class="separator">|</span><a href="' + url + '"><img class="removeddit" src="' + img + '"></a>');
}
function addPatreonLink() {
if (!settings) {
setTimeout(function() {
addPatreonLink();
}, 100);
return;
}
if (!settings.settings.patreonLink) {
return;
}
let patreonUrl = 'https://www.patreon.com/join/feeling_impossible/checkout';
let elem;
let topMargin = 22;
let bottomMargin = 0;
// new reddit
if ($('.s10kkmv9-0').length) {
topMargin = 15;
elem = $($('.s10kkmv9-0')[0]);
// old reddit
} else {
// in a subreddit
if (window.location.href.split('/')[3] == 'r') {
if ($('.submit-text').length) {
elem = $($('.submit-text').parent());
} else if ($('.submit-link').length) {
elem = $($('.submit-link').parent());
}
} else {
if ($('a[data-event-action="createsubreddit"]').length) {
topMargin = 0;
bottomMargin = 14;
elem = $($('a[data-event-action="createsubreddit"]').parent().parent().parent());
}
}
}
// user overview
if (!elem) {
return;
}
// old reddit
// if ($('a[data-event-action="createsubreddit"]').length) {
// elem = $($('a[data-event-action="createsubreddit"]').parent().parent().parent());
// } else if ($('.submit-text').length) {
// elem = $('.submit-text');
// } else if ($('.submit-link').length) {
// elem = $('.submit-link');
// new reddit
// } else if ($('.s10kkmv9-0').length) {
// topMargin = 15;
// elem = $($('.s10kkmv9-0')[0]);
// user overview
// } else {
// console.log('no ad');
// return;
// }
// add analytics for Patreon Ad show
ga('send', 'event', 'Patreon Ad', 'show');
let patreonDiv = $('<div>').css({
'margin-top': topMargin + 'px',
'margin-bottom': bottomMargin + 'px',
'text-align': 'center',
'font-weight': 'bold',
'font-size': '13px',
'color': '#336699'
}).text('Support Reddit Pro Tools');
let btnWidth = 176;
let btnHeight = 34;
let patreonBtn = $('<div>').css({
cursor: 'pointer',
margin: '0 auto 3px auto',
'background-color': '#e85b46',
'border-radius': btnHeight / 2 + 'px',
width: btnWidth + 'px',
height: btnHeight + 'px',
'line-height': btnHeight + 'px',
color: '#ffffff',
'font-family': "'America', 'GT America', 'Lato', 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
'font-size': '0.875rem',
'font-smooth': 'never',
'font-weight': '500',
'text-rendering': 'optimizeLegibility',
});
let patreonLogoBar = $('<span>').css({
width: '2px',
height: '12px',
display: 'inline-block',
'vertical-align': 'middle',
'background-color': '#052d49',
'font-size': '.1em',
}).html(' ');
let patreonLogoCircle = $('<span>').css({
width: '9px',
height: '9px',
display: 'inline-block',
'vertical-align': 'middle',
'background-color': '#ffffff',
'font-size': '.1em',
'border-radius': '4px',
'margin-left': '1px',
position: 'relative',
top: '-2px',
}).html(' ');
let patreonLogo = $('<span>').append(patreonLogoBar, patreonLogoCircle);
let patreonText = $('<span>').css({
'margin-left': '12px',
position: 'relative',
top: '1px',
}).text('Become a patron');
patreonBtn.append(patreonLogo, patreonText);
patreonBtn.mousedown(function(e) {
if (e.which == 1) {
ga('send', 'event', 'Patreon Ad', 'click');
window.location = patreonUrl;
} else if (e.which == 2) {
ga('send', 'event', 'Patreon Ad', 'click');
window.open(patreonUrl, '_blank');
}
});
patreonDiv.prepend(patreonBtn);
elem.after(patreonDiv);
}