-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
366 lines (314 loc) · 16.2 KB
/
main.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
// Necessary to perform any API actions
var modhash = $("form.logout input[name=uh]").val();
function checkNightMode() {
function doModeLogic() {
if($('body').hasClass('res-nightmode')) {
// Stop if we've already added the stylesheet
if($('link#rgo_nightmode').length) { return; }
$('<link/>', {
rel: 'stylesheet',
type: 'text/css',
href: chrome.runtime.getURL('style_nightmode_overrides.css'),
id: 'rgo_nightmode'
}).appendTo('head');
} else {
$('link#rgo_nightmode').remove();
}
}
// hook changes to body's classes to handle night mode changes
var observer = new MutationObserver(function(mutations) {
doModeLogic();
});
observer.observe(document.body, {
attributes: true,
attributeFilter: ['class']
});
// invoke nightmode logic once to process current state
doModeLogic();
}
function addQuickFlair() {
// Add our special button to all flatlists under threads
$('.link .flat-list.buttons').each(function (i, e) {
// Skip elements we've already added the dropdown to
if ($(e).has('.rgo-qf-dropdown').length) { return; }
// Create selected option/trigger div
var quickFlair = document.createElement('DIV');
quickFlair.className = 'dropdown lightdrop rgo-qf-dropdown';
var qfDrop = document.createElement('SPAN');
qfDrop.className = 'selected';
$(qfDrop).text('quick flair');
quickFlair.appendChild(qfDrop);
// Create flair selections div
var flairChoices = document.createElement('DIV');
flairChoices.className = 'drop-choices';
var linkFlairs = {
'AMA': 'ama',
'Discussion': 'discussion',
'Discussion | Esports': 'discussion esports',
'Feedback': 'feedback',
'Fluff': 'fluff',
'Fluff | Esports': 'fluff esports',
'Game Update': 'update',
'Gameplay': 'gameplay',
'Gameplay | Esports': 'gameplay esports',
'Help': 'help',
'News/Events': 'news',
'News/Events | Esports': 'news esports',
'Scheduled Sticky': 'sticky',
'Tips/Guides': 'guides',
'UGC': 'ugc',
'Workshop Skin': 'workshop skin'
};
for (var lf in linkFlairs) {
var linkFlair = document.createElement('A');
$(linkFlair).text(lf);
if (lf === 'UGC') {
linkFlair.title = 'User Generated Content';
} else {
linkFlair.title = lf;
}
linkFlair.className = 'choice';
$(linkFlair).click(function (ev) {
ev.preventDefault();
var flairText = ev.target.innerHTML;
var flairClicked = linkFlairs[flairText];
if (flairText.includes('/')) {
flairText = flairText.replace('/', ' & ');
}
var id = $(ev.target).closest('.thing').attr('data-fullname');
if (flairText === 'UGC') {
flairText = 'User Generated Content';
}
chrome.runtime.sendMessage({
contentScriptQuery: 'editFlair',
thing: id,
flairCSS: flairClicked,
text: flairText,
mod: modhash
});
//editFlair(id, flairClicked, flairText);
var dd = $(ev.target.parentNode).siblings('.rgo-qf-dropdown');
dd.html('flaired!');
dd.removeClass('rgo-qf-dropdown');
});
flairChoices.appendChild(linkFlair);
}
var li = document.createElement('LI');
var spacer = document.createElement('DIV');
spacer.className = 'spacer';
spacer.appendChild(quickFlair);
spacer.appendChild(flairChoices);
li.appendChild(spacer);
e.appendChild(li);
});
}
function addRemoveWithReasons() {
var diaDiv = document.createElement('DIV'),
txtArea = document.createElement('TEXTAREA');
diaDiv.id = 'ruleDialog';
txtArea.id = 'ruleText';
diaDiv.appendChild(txtArea);
$('#ruleDialog').hide();
$('#ruleText').hide();
$('#siteTable').append( diaDiv );
// Add our special button to all flatlists under threads
$('.link .flat-list.buttons').each(function(i, e) {
// Skip elements we've already added the dropdown to
if ($(e).has('.rgo-dropdown').length) { return; }
// Create selected option/trigger div
var selected = document.createElement('DIV');
selected.className = 'dropdown lightdrop rgo-dropdown';
var selectedSpan = document.createElement('SPAN');
selectedSpan.className = 'selected';
$(selectedSpan).text('remove w/ reason');
selected.appendChild(selectedSpan);
// Create options div
var options = document.createElement('DIV');
options.className = 'drop-choices';
// The rule by button label and hover-text
var rules = {
'Rule 1: Relevancy': 'Relevancy',
'Rule 2: Quality': 'Quality',
'Rule 2b: Duplicate': 'Duplicate',
'Rule 3: Support': 'Support',
'Rule 4: Exploits': 'Exploits & Bugs',
'Rule 5: Trading': 'Trading, Betting & Giveaways',
'Rule 6: Scamming': 'Scamming & Cheating',
'Rule 7: Witch-hunts': 'Accusations & Witch-hunts',
'Rule 8: Advertising': 'Advertising',
'Rule 9: Abuse': 'Abuse & Poor Behavior'
};
for (var r in rules) {
var ruleLink = document.createElement('A');
$(ruleLink).text(r);
ruleLink.title = rules[r];
ruleLink.className = 'choice';
$(ruleLink).click(function(ev) {
ev.preventDefault();
var id = $(ev.target).closest('.thing').attr('data-fullname');
// If ctrl key is pressed when a rule is clicked with
// OneTap mode enabled, open a custom removal dialog.
if (ev.ctrlKey) {
chrome.storage.sync.get(chromeGet, function(storage) {
if (storage.oneTaps) {
$("#ruleText").css("display", "inline");
$("#ruleDialog").dialog({
height : 200,
width : 700,
modal : true,
title : 'Custom Removal',
buttons : {
"Get Salt": function() {
var removedThreadLink = 'https://redd.it/' + id.replace('t3_', '');
var removalMessage = $('#ruleText').val();
var footer = "";
if (storage.footer != '') {
footer = "\n\n---\n\n" + storage.footer.replace('%%thread_link%%', removedThreadLink);
removalMessage += footer;
}
// This actually makes the POSTs happen and removes the thread, it also closes the dialog on completion
chrome.runtime.sendMessage({
contentScriptQuery: 'removeWithReason',
thing: id,
text: removalMessage,
distinguish: true,
win: this,
mod: modhash
});
//$( this ).dialog('close');
//removeWithReason(id, removalMessage, distinguish = true, win = this);
var dd = $(ev.target.parentNode).siblings('.rgo-dropdown');
dd.html('removed');
dd.removeClass('rgo-dropdown');
},
"Abort!" : function() {
$( this ).dialog( 'close' );
}
}
});
}
});
} else {
var ruleClicked = ev.target.innerHTML.replace(' ','').toLowerCase().split(':')[0];
// Default values for the comments for each removal
var chromeGet = {
rule1: "Your thread was removed under **[Rule 1](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule2: "Your thread was removed under **[Rule 2](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule2b: "Your thread was removed as a duplicate under **[Rule 2](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule3: "Your thread was removed under **[Rule 3](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule4: "Your thread was removed under **[Rule 4](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule5: "Your thread was removed under **[Rule 5](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule6: "Your thread was removed under **[Rule 6](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule7: "Your thread was removed under **[Rule 7](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule8: "Your thread was removed under **[Rule 8](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
rule9: "Your thread was removed under **[Rule 9](https://www.reddit.com/r/GlobalOffensive/about/rules/)**.",
footer: "",
oneTaps: ""
};
chrome.storage.sync.get(chromeGet, function(storage) {
// If they want one click removals, don't show dialog
if (storage.oneTaps) {
// var property = $(ev.target).text().toLowerCase().replace(' ', '');
var property = ev.target.innerHTML.replace(' ','').toLowerCase().split(':')[0]
if (!chromeGet.hasOwnProperty(property)) {
chromeGet[property] = "Your thread has been removed. Please carefully [read our rules](https://www.reddit.com/r/GlobalOffensive/about/rules/) and ask if you have any questions.";
}
var removedThreadLink = 'https://redd.it/' + id.replace('t3_', '');
var removalMessage = storage[property];
var footer = "";
if (storage.footer != "") {
footer = "\n\n---\n\n" + storage.footer.replace('%%thread_link%%', removedThreadLink);
removalMessage += footer;
}
chrome.runtime.sendMessage({
contentScriptQuery: 'removeWithReason',
thing: id,
text: removalMessage,
distinguish: true,
win: null,
mod: modhash
});
//removeWithReason(id, removalMessage, distinguish = true, win = null);
var dd = $(ev.target.parentNode).siblings('.rgo-dropdown');
dd.html("removed");
dd.removeClass('rgo-dropdown');
} else { // Show dialog on each click
var ruleClicked = ev.target.innerHTML.replace(' ','').toLowerCase().split(':')[0];
$("#ruleText").val(storage[ruleClicked]);
$("#ruleText").html(storage[ruleClicked]);
$("#ruleText").css("display", "inline");
$("#ruleDialog").dialog({
height : 200,
width : 700,
modal : true,
title : ev.target.innerHTML + " Removal",
buttons : {
"Get Salt": function() {
var property = $(ev.target).text().toLowerCase().replace(' ', '');
if (!chromeGet.hasOwnProperty(property)) {
chromeGet[property] = "Your thread has been removed. Please carefully [read our rules](https://www.reddit.com/r/GlobalOffensive/about/rules/) and ask if you have any questions.";
}
var removedThreadLink = 'https://redd.it/' + id.replace('t3_', '');
var removalMessage = $( "#ruleText" ).val();
var footer = "";
if (storage.footer != "") {
footer = "\n\n---\n\n" + storage.footer.replace('%%thread_link%%', removedThreadLink);
removalMessage += footer;
}
// This actually makes the POSTs happen and removes the thread, it also closes the dialog on completion
chrome.runtime.sendMessage({
contentScriptQuery: 'removeWithReason',
thing: id,
text: removalMessage,
distinguish: true,
win: this,
mod: modhash
});
//$( this ).dialog('close');
//removeWithReason(id, removalMessage, distinguish = true, win = this);
var dd = $(ev.target.parentNode).siblings('.rgo-dropdown');
dd.html("removed");
dd.removeClass('rgo-dropdown');
},
"Abort!" : function() {
$( this ).dialog( "close" );
}
}
});
}
});
}
});
options.appendChild(ruleLink);
}
var li = document.createElement('LI');
var spacer = document.createElement('DIV');
spacer.className = "spacer";
spacer.appendChild(selected);
spacer.appendChild(options);
li.appendChild(spacer);
e.appendChild(li);
});
}
// Listener for displaying responses or errors from our background scripts
chrome.runtime.onMessage.addListener(
function(request, sender) {
if (request.status === 'error') {
alert('Error: ' + request.msg);
$("#ruleDialog").dialog('close');
}
if (request.status === 'success') {
$("#ruleDialog").dialog('close');
}
}
);
// Hook our functions
$(window).on('neverEndingLoad', function () {
addQuickFlair();
addRemoveWithReasons();
});
$(document).ready(function () {
checkNightMode();
addQuickFlair();
addRemoveWithReasons();
});