-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.js
696 lines (605 loc) · 27.3 KB
/
script.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
var BLAZE_DEBUG_MODE = false;
var API_KEYS = {
BLAZE: "p3YZ1qDutpcBd7Bte2mcDw((",
DEBUG: "2WQ5ksCzcYLeeYJ0qM4kHw(("
}
$(document).ready(function() {
var apiEndpoint = "answers";
var currentPage = 1;
var pageSize = 100;
var autorefresh_time = 1000000000; //crazy long == no refresh, because I'm lazy
var autorefresh_timeout;
var sort = ByCreationDate;
var previousFlags;
var previousFlagText = "";
var highlightsOnly = true;
$("#blaze-api-key-field").focus();
InitSiteAPIKeyAutocomplete();
var hasToken = false;
var lochash = location.hash.substr(1);
var site = lochash.substr(lochash.indexOf('site=')).split('&')[0].split('=')[1];
if (site) {
console.log("site: " + site);
$("#blaze-api-key-field").val(site);
}
var token = lochash.substr(lochash.indexOf('access_token='))
.split('&')[0]
.split('=')[1];
if (token) {
console.log("saving token to localstorage: " + token);
localStorage.setItem('access_token', token);
}
var token = localStorage.getItem('access_token');
if (token) {
SetAuthButtonText("Verifying...")
$.ajax({
type: "GET",
url: "https://api.stackexchange.com/2.2/access-tokens/" + token,
data: "key=" + BLAZE_DEBUG_MODE ? API_KEYS.DEBUG : API_KEYS.BLAZE + "",
success: function(data) {
console.log("success!")
console.log(data);
hasToken = true
if (data["items"].length == 0) {
console.log("current token invalid")
localStorage.removeItem("access_token")
hasToken = false
SetAuthButtonText("Authenticate")
}
else {
SetAuthButtonText("Verified")
}
},
error: function(data) {
console.log("error!");
console.log(data);
ShowErrorWithMessage(JSON.parse(data.responseText).error_message);
}
});
}
$(document).on('click', 'a.flag-post-naa', function(e) {
e.preventDefault();
var $this = $(e.target)
if (!$this.is('a.flag-post-naa')) {
$this = $this.parents('a.flag-post-naa')
}
var postId = $this.attr("data-postid");
var siteName = $this.attr("data-site");
$.ajax({
type: "GET",
url: "https://api.stackexchange.com/2.2/answers/" + postId + "/flags/options",
data: {
'key': BLAZE_DEBUG_MODE ? API_KEYS.DEBUG : API_KEYS.BLAZE,
'site': siteName,
'access_token': token
},
success: function(data) {
$("#flag_options_form").html("");
$("#flag_options_form").html(RenderFlagOptions(data["items"]));
$("#flag_modal").modal();
$("#modal-flag-answer-button").attr("data-site-name", siteName);
$("#modal-flag-answer-button").attr("data-post-id", postId);
console.log(data);
},
error: function(data) {
console.log("error!");
console.log(data);
ShowErrorWithMessage(JSON.parse(data.responseText).error_message);
}
});
});
function AutoRefresh() {
RefreshData();
autorefresh_timeout = setTimeout(AutoRefresh, autorefresh_time);
}
$(".autorefresh-option#autorefresh-none").click(function() {
$(".autorefresh-option").removeClass("chosen");
$(this).addClass("chosen");
autorefresh_time = 10000000000;
clearTimeout(autorefresh_timeout);
autorefresh_timeout = setTimeout(AutoRefresh, autorefresh_time);
});
$(".autorefresh-option#autorefresh-10-seconds").click(function() {
$(".autorefresh-option").removeClass("chosen");
$(this).addClass("chosen");
autorefresh_time = 10000;
clearTimeout(autorefresh_timeout);
autorefresh_timeout = setTimeout(AutoRefresh, autorefresh_time);
});
$(".autorefresh-option#autorefresh-30-seconds").click(function() {
$(".autorefresh-option").removeClass("chosen");
$(this).addClass("chosen");
autorefresh_time = 30000;
clearTimeout(autorefresh_timeout);
autorefresh_timeout = setTimeout(AutoRefresh, autorefresh_time);
});
$(".autorefresh-option#autorefresh-5-minutes").click(function() {
$(".autorefresh-option").removeClass("chosen");
$(this).addClass("chosen");
autorefresh_time = 300000;
clearTimeout(autorefresh_timeout);
autorefresh_timeout = setTimeout(AutoRefresh, autorefresh_time);
});
$(".dropdown li").click(function() {
$(this).addClass("chosen");
$(this).siblings("li").not(this).removeClass("chosen");
});
$(document).on("click", "#modal-flag-answer-button", function(e) {
var $this = $(e.target)
if (!$this.is('#modal-flag-answer-button')) {
$this = $this.parents('#modal-flag-answer-button')
}
var postId = $this.attr("data-post-id");
var postText = $("#answer_" + postId.toString()).text();
var flagId = $('input[name=flag_type]:checked', '#flag_options_form').val();
var site = $this.attr("data-site-name");
$.ajax({
type: "POST",
url: "https://api.stackexchange.com/2.2/answers/" + postId + "/flags/add",
data: {
'key': BLAZE_DEBUG_MODE ? API_KEYS.DEBUG : API_KEYS.BLAZE,
'site': site,
'access_token': token,
'option_id': flagId,
'comment': ''
},
success: function(data) {
console.log(data);
$("#flag_modal").modal('hide')
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("error");
console.log(data);
ShowErrorWithMessage(jqXHR.responseText.error_message);
}
});
});
$(".blaze-logo").click(function() {
$("table#datatable tr").remove();
$(".blaze-header").fadeIn();
$(".site-api-key-form").fadeIn();
$("nav").fadeOut();
});
$("#select-answers").click(function() {
$(".blaze-fetch-items").html("Fetch Answers");
apiEndpoint = 'answers';
});
$("#select-comments").click(function() {
$(".blaze-fetch-items").html("Fetch Comments");
apiEndpoint = 'comments';
});
$("#select-questions").click(function() {
$(".blaze-fetch-items").html("Fetch Questions");
apiEndpoint = 'questions';
});
$("#select-users").click(function() {
$(".blaze-fetch-items").html("Fetch Users");
apiEndpoint = 'users';
});
$(".blaze-fetch-items").click(function() {
RefreshData();
});
$(".authenticate-user-button").click(function() {
var token = localStorage.getItem('access_token');
SetAuthButtonText("Working...");
if (token) {
$.ajax({
type: "GET",
url: "https://api.stackexchange.com/2.2/access-tokens/" + token,
data: {
'key': BLAZE_DEBUG_MODE ? API_KEYS.DEBUG : API_KEYS.BLAZE
},
success: function(data) {
if (data["items"].length == 0) {
console.log("current token invalid");
localStorage.removeItem("access_token");
window.open("https://stackexchange.com/oauth/dialog?client_id=2670&scope=write_access&redirect_uri=https://charcoal-se.org/blaze/index.html","_self");
SetAuthButtonText("Redirecting...");
hasToken = false;
}
else {
SetAuthButtonText("Verified");
hasToken = true;
}
},
error: function(jqXHR, textStatus, errorThrown) {
ShowErrorWithMessage(jqXHR.responseText.error_message);
}
});
}
else {
window.open("https://stackexchange.com/oauth/dialog?client_id=2670&scope=write_access&redirect_uri=https://charcoal-se.org/blaze/index.html", "_self")
}
});
$(".refresh-current-data-button").click(function() {
var oldHTML = $(this).html();
$(this).html("working...");
RefreshData(function() {
$(".refresh-current-data-button").html(oldHTML);
});
});
$(document).keypress(function(e) {
if(e.which == 13) {
RefreshData();
}
});
function RefreshData(f) {
RemoveErrorsAndWarnings();
var site = $("#blaze-api-key-field").val();
window.location.hash = "site=" + site;
var oldButtonText = $(".blaze-fetch-items").html();
$(".blaze-fetch-items").html("Loading...");
var args = {
'page': currentPage,
'pagesize': pageSize,
'key': BLAZE_DEBUG_MODE ? API_KEYS.DEBUG : API_KEYS.BLAZE,
'site': site,
'order': 'desc',
'sort': 'creation',
'filter': '!LeJQlFEfIbsDDTG1lReSJX'
}
if (apiEndpoint == "questions") args.filter = '!)Q7pHZaD2SW58N2KuVqkwvB5';
if (apiEndpoint == "comments") args.filter = '!)Q3IqX*j)mxF9SKNRz3tb5yK';
if (apiEndpoint == "users") args.filter = '!40.F89yKwjYalEn_s';
var url = "https://api.stackexchange.com/2.2/" + apiEndpoint;
$.ajax({
type: "GET",
url: url,
data: args,
success: function(data) {
console.log("backoff: " + data["backoff"]);
if (data["backoff"]) {
var backoff = parseInt(data["backoff"], 10);
if (backoff > 0) {
ShowWarningWithMessage("Backoff received: " + backoff + " seconds :(");
}
}
$(".blaze-header").fadeOut();
$(".site-api-key-form").fadeOut();
$("nav").fadeIn();
var items = data["items"];
items.sort(sort);
$("table#datatable tr").remove();
$(items).each(function(index, item) {
if (apiEndpoint == 'questions') {
$("table#datatable").append(RenderQuestion(item));
}
else if (apiEndpoint == 'answers') {
$("table#datatable").append(RenderAnswer(item, site));
}
else if (apiEndpoint == 'comments') {
$("table#datatable").append(RenderComment(item));
}
else if (apiEndpoint == 'users') {
$("table#datatable").append(RenderUser(item));
}
});
$(".blaze-fetch-items").html(oldButtonText);
$("div.alert.alert-danger").remove();
if (typeof(f) === "function") f();
},
error: function(jqXHR, textStatus, errorThrown) {
string = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>';
string = string + jqXHR.responseText["error_message"].charAt(0).toUpperCase() + jqXHR.responseText["error_message"].slice(1);
string = string + '</div>';
$("div.alert.alert-danger").remove();
$(".site-api-key-form").prepend(string);
$(".blaze-fetch-items").html(oldButtonText);
ShowErrorWithMessage(jqXHR.responseText["error_message"].charAt(0).toUpperCase() + jqXHR.responseText["error_message"].slice(1));
}
});
}
// Array sorting functions:
function ByLength(a, b) {
var aLength = a["body"].length;
var bLength = b["body"].length;
return ((aLength < bLength) ? -1 : ((aLength > bLength) ? 1 : 0));
}
function ByCreationDate(a, b) {
var aDate = a["creation_date"];
var bDate = b["creation_date"];
return ((aDate > bDate) ? -1 : ((aDate < bDate) ? 1 : 0));
}
$("#sort-by-newest-creation").click(function() {
sort = ByCreationDate;
$("table#datatable tr").remove();
$("#current-sort-indicator").html("Working...");
RefreshData(function()
{
$("#current-sort-indicator").html("Newest");
});
});
$("#sort-by-shortest-length").click(function() {
sort = ByLength;
console.log($("#current-sort-indicator"));
$("table#datatable tr").remove();
RefreshData(function()
{
$("#current-sort-indicator").html("Shortest");
});
});
$("#highlights-enable").click(function() {
highlightsOnly = true;
RefreshData(function(){});
});
$("#highlights-disable").click(function() {
highlightsOnly = false;
RefreshData(function(){});
});
// Rendering things
function RenderAnswer(item) {
var string;
var warningChecks = AnswerWarningHeuristics(item);
if(warningChecks) {
string = '<tr id="answer_' + item["answer_id"] + '_container" ' + (highlightsOnly ? '' : 'class="warning-answer"') + '><td style="vertical-align:top" class="col-md-1"><div class="score"><h2 style="color:rgba(0,0,0,0.6); pull:right">';
}
else {
string = '<tr id="answer_' + item["answer_id"] + '_container"' + (highlightsOnly ? 'style="display: none"' : '') + '><td style="vertical-align:top" class="col-md-1"><div class="score"><h2 style="color:rgba(0,0,0,0.6); pull:right">';
}
string = string + item["score"];
string = string + '</h2></div></td><td class=""><div class="post col-md-9" style="max-width:75%"><h3><a target="_blank" href="';
string = string + item["link"];
string = string + '">';
string = string + item["title"];
string = string + '</a>';
string = string + '</h3>';
string += '<span class="warning-info" style="color:#c6c625">';
if(warningChecks) {
string += 'Warning: ' + warningChecks;
}
string += '</span>';
string += '<hr><span class="post-body" id="answer_' + item["answer_id"] + '" style="color:rgba(70,70,70,1)">';
string = string + item["body"];
string = string + '</span>'
var siteUrl = item["link"].split("/")[2];
if (hasToken) {
string = string + '<br /><a class="flag-post-naa" style="float:left; color:rgb(165,65,65);" href="#" data-site="' + siteUrl + '" data-postid="' + item["link"].split("#")[1] + '"><strong>Flag on Site</strong></a>'
}
string = string + RenderUsercard(item["owner"], item);
string = string + '</p></div></td></tr>';
string = string + '<tr><td class="col-md-1"></td></tr>'; //<td><strong style="color:#b65454">flag</strong></td>
return string;
}
function RenderQuestion(item) {
var string = '<tr><td style="vertical-align:top" class="col-md-1"><div class="score"><h2 style="color:rgba(0,0,0,0.6); pull:right">';
string = string + item["score"];
string = string + '</h2></div></td><td class=""><div class="post col-md-9" style="max-width:75% !important"><h3><a target="_blank" href="';
string = string + item["link"];
string = string + '">';
string = string + item["title"];
string = string + '</a>';
string = string + "</br><small>";
for (var i = 0; i < item["tags"].length; i++) {
string = string + '<kbd style="background-color:grey">' + item["tags"][i] + '</kbd> ';
};
string = string + "</small>";
string = string + '</h3><hr><span class="post-body" style="color:rgba(70,70,70,1)">';
string = string + item["body"];
var siteUrl = item["link"].split("/")[2];
string = string + RenderUsercard(item["owner"], item);
string = string + '</p></div></td></tr>';
return string;
}
function RenderComment(item) {
var string = '<tr style="padding: 0.5em 0; border-bottom: 1px solid grey;"><td class="col-md-1"><div class="score"><h5 style="color:rgba(0,0,0,0.6); pull:right; text-align:right">';
if (item["score"] != "0") string = string + item["score"];
string = string + '</h5></div></td><td><div class="post col-md-9">';
string = string + '<span class="post-body" style="color:rgba(70,70,70,1)">';
string = string + item["body"];
string = string + ' - <a target="_blank" href="';
string = string + item["owner"]["link"];
string = string + '">';
string = string + item["owner"]["display_name"];
if (item["owner"]["user_type"] == 'moderator') string = string + " ♦";
string = string + '</a> <a target="_blank" style="color:grey" href="';
string = string + item["link"];
string = string + '"><span data-livestamp="';
string = string + item["creation_date"];
string = string + '"></span> <span class="glyphicon glyphicon-link"></span>';
string = string + '</a></span></div></td></tr>';
return string;
}
function RenderUser(item) {
var string = '<tr style="margin-top:10px"><td style="vertical-align:top" class="col-md-1">';
string = string + RenderUsercard(item, item).replace("posted ", "created ");
string = string + '</td><td class=""><div class="post col-md-9" style="max-width:75% !important"><p class="text-danger">';
string = string + (("about_me" in item) ? item["about_me"] : "-");
string = string + '</p></div></td></tr>';
return string;
}
function RenderUsercard(user, item) {
var string = "<div style='background-color: clear; padding-left:5px;padding-right:3px;padding-bottom:3px; padding-top: 2px; width:175px; min-height:58px; float:right;border: 1px dashed rgba(0,0,0,.2);'><div style='margin-top: 0px; font-size:12px; margin-bottom: 2px;color: grey;'>posted ";
string = string + '<span style="font-weight: bold;"data-livestamp="';
string = string + item["creation_date"];
string = string + '"></span>';
string = string + "</div><div style='float:left; width:32px; height:32px'><img src='";
string = string + user["profile_image"];
string = string + "' style='width:32px; height:32px'></div><div style='color:#888; font-size:12px; margin-left:5px;'><span style='padding-left:6px; border-left:6margin-left:6px; border-top:-10px'><a target='_blank' href='";
string = string + user["link"];
string = string + "'>";
string = string + user["display_name"];
if (user['user_type'] == 'moderator') string = string + ' ♦';
string = string + "</a></span>";
string = string + "</br><span style='color:grey; font-size:12px; padding-left:6px; border-left:6margin-left:6px; border-top:-10px'>";
string = string + FormatRep(user["reputation"]);
string = string + "</span>";
string = string + "</div></div>";
return string;
}
function RenderFlagOptions(items) {
string = "";
$(items).each(function(index, item) {
if(!item["has_flagged"]) {
string = string + '<input type="radio" name="flag_type" id="flag-' + item["option_id"] + '" value="' + item["option_id"] + '">'
string = string + '<label for="flag-' + item["option_id"] + '" style="margin-left:10px; font-weight: bold;">' + item["title"] + '</label>';
string = string + '<label for="flag-' + item["option_id"] + '" style="margin-left:20px; font-weight: normal;">' + item["description"] + '</label>';
}
else {
string = string + '<input type="radio" name="flag_type" id="flag-' + item["option_id"] + '" value="' + item["option_id"] + '" disabled>';
string = string + '<label for="flag-' + item["option_id"] + '" style="margin-left:10px; color:gray; font-weight: bold;">' + item["title"] + '</label>';
string = string + '<label for="flag-' + item["option_id"] + '" style="margin-left:20px; color:rgb(165,65,65); font-weight:bold;">you have already raised this type of flag</label>';
}
string = string + '<br />';
});
return string;
}
// Autocomplete things
function InitSiteAPIKeyAutocomplete() {
$.ajax({
type: "GET",
url: "https://api.stackexchange.com/2.2/sites",
data: {
'pagesize': 1000,
'filter': '!mszzl.y_MC',
'key': BLAZE_DEBUG_MODE ? API_KEYS.DEBUG : API_KEYS.BLAZE
},
success: function(data) {
var items = data["items"];
var siteApiKeys = [];
jQuery.each(items, function(index, item) {
siteApiKeys.push(item["api_site_parameter"]);
});
$("#blaze-api-key-field").autocomplete({
source: siteApiKeys
});
},
error: function(jqXHR, textStatus, errorThrown) {
ShowErrorWithMessage(jqXHR.responseText.error_message);
}
});
}
$(".choose-site").click(function(e) {
$("#blaze-api-key-field").val($(this).attr("id"));
e.preventDefault();
});
function ShowWarningWithMessage(message) {
RemoveErrorsAndWarnings();
$(".navbar").before($("<div></div>", {
'class': 'blaze-modal-warning'
}).text(message).click(function() {
$(this).slideUp(200);
}));
}
function ShowErrorWithMessage(message) {
RemoveErrorsAndWarnings();
$(".navbar").before($("<div></div>", {
'class': 'blaze-modal-error'
})
.text(message)
.click(function() {
$(this).slideUp(200);
}));
}
function RemoveErrorsAndWarnings() {
$(".blaze-modal-error").each(function() {
$(this).slideUp(200);
});
$(".blaze-modal-warning").each(function() {
$(this).slideUp(200);
});
}
function SetAuthButtonText(text) {
$(".authenticate-user-button").html('<span class="glyphicon glyphicon-lock"></span> ' + text);
}
function FormatRep(reputation) {
return Math.abs(Number(reputation)) >= 1.0e+4 ?
(Math.abs(Number(reputation)) / 1.0e+3).toFixed(1) + "k" :
Math.abs(Number(reputation));
}
// Experimental post-classifying heuristics
function getKey(object, item) {
for(var key in object) {
if(object[key] == item) {
return key;
}
}
return null;
}
/**
* Applies post-classifying heuristics for post warnings to an answer.
* @param {object} item - The API-returned object representing the answer.
* @returns {string||boolean} - If the post should be highlighted, the reason(s). If not, false.
*/
function AnswerWarningHeuristics(item) {
var answerText = item["body"];
var checks = {
"ContainsTelephone": function(text) {
var matches = text.match(/[0-9\-\*]{7,15}/gi);
if(matches) {
for(var match in matches) {
var formatted;
try {
formatted = phoneUtils.formatE164(match);
}
catch(e) {
continue;
}
var testCountries = ["US", "IN"];
for(var countryCode in testCountries) {
try {
if(phoneUtils.isPossibleNumber(formatted, countryCode)
&& phoneUtils.isValidNumber(formatted, countryCode)) return true;
}
catch(e) {}
}
}
return false;
}
else return false;
},
"ContainsEmail": function(text) {
// Thanks voyager (http://stackoverflow.com/users/34813) (http://stackoverflow.com/a/1373724/3160466)
return text.match(/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gi);
},
"PostLengthUnderThreshold": function(text) {
return text.length < 100;
},
"HighLinkProportion": function(text, item) {
var proportionThreshold = 0.35; // as in, max 35% of the answer can be links
var id = item["answer_id"];
var linkRegex = /<a\shref="([^"]*)"(.*)>(.*)<\/a>/gi;
var matches = linkRegex.exec(text);
if(matches) {
console.log("[AWC.HighLinkProportion] id " + id + " has matches:");
console.log(matches);
var linkLength = 0;
for(var i = 3; i < matches.length; i += 4) { // This only matches link titles, not the entire HTML.
linkLength += matches[i].length;
}
return (linkLength / text.length) >= proportionThreshold;
}
else {
return false;
}
},
"ContainsSignature": function(text, item) {
return text.substr(-item["owner"]["display_name"].length) === item["owner"]["display_name"];
},
"MeTooAnswer": function(text) {
return (text.match(/(how\s(can(\si)?|to)\s)?(fix|solve|answer)(\s\w+){0,3}\s(problem|question|issue)\?/gi) ||
text.match(/(i\s)?(have\s)?(the\s)?same\s((problem|question|issue)|here)/gi)) &&
!text.match(/(i\s)?(fixed|solved)\s(this|it)\s(problem|question|issue)?(by|when)/gi);
},
"ThanksAnswer": function(text) {
return text.match(/thank(s)?(ing)?\s(you|to|@\w+)/gi) && text.match(/that\s(helped|solved)/gi);
},
"Can'tCommentAnswer": function(text) {
return text.match(/(can't(\sadd(\sa)?)?|rep(utation)?(\sto)?)\scomment/gi);
}
};
var checkHits = [];
$.each(checks, function(index, value) {
if(value(answerText, item)) {
var matchedReason = getKey(checks, value);
console.warn("Post ID " + item["answer_id"] + " matched warning '" + matchedReason + "'.");
checkHits.push(getKey(checks, value));
}
});
return checkHits.join(', ');
}
});