-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlncomment.js
259 lines (208 loc) · 9.03 KB
/
nlncomment.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
// ==UserScript==
// @name Get and Flag comment
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Flag Overflow
// @author Shree: https://stackoverflow.com/users/965146/shree
// @match *://*.chat.stackoverflow.com/rooms/232981/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
// change ^ @match where you want to run and get feedback from the script.
let BIT = true;
(function() {
'use strict';
const API_KEY = ''; // Your API_KEY
const Access_Token = ''; // Your Access_Token
const Site_Name = 'stackoverflow'; // Change site name if needed
const Room_ID = ' '; // Room to send message
const Time_Out = 50000;
flagComment();
async function flagComment() {
let getresult = [];
let resultlength = 0;
let keywords = [
"((?:^)@(\\w+)\\b\\s)?thank([\\w.,!\']*)?(\\s[\\w{1,8},.@!:\\-)]*)?(\\s[\\w{1,8},.@!:\\-)]*)?(\\s[\\w{1,8},.@!:\\-)]*)?",
"glad(?:\\sto\\shelp|hear)?",
"(appreciate|perfect|awesome|amazing|excellent)(?:,|\w{1,2})?(\\s(?:solution|example)?)?",
"solv(\\w{1,3})(\\s(\w{2,5}))\\s(\\w{1,9})?",
"(\\w{1,8}\\s)?up(?:\\s)?vote\\s(\\w{0,8})?\\s(\\w{0,8})?",
"(\\w{1,5}([\\w{,2}\']*)\\s)?work([\\w*{0,3}!.]*)?(\\s[:\\-)+=.}]*)?",
"save(\\w{1,3})?\\s(\\w{0,4})\\s([\\w{0,6}.!:\\-)]*)?",
"([\\w{1,8},.@!:)]*\\s)?(love|cheers|great)(\\s[\\w{1,8},.@!:)]*)?",
":\\)| :-\\)|;\\)"
];
let regex = new RegExp(keywords.join("|"), 'gi');
let ignorkeywords=[
"not|unfortunate|but|require|need|persists",
"\\b(doesn|don|didn|couldn|can|isn)(['’])?(\\w{1,2})?\\b",
"[?]"
];
let igreg = RegExp(ignorkeywords.join("|"), 'gi');
let response;
response = await getComments(API_KEY);
if(response.quota_remaining === '9999')
sendChatMessage("**Quota_Rollover:** "+ response.quota_remaining, Room_ID);
getresult = response.items.map(a => a.body_markdown + " #@# " + a.link + " #@# " + a.comment_id + " #@# " + a.post_id);
let finalresult = $.grep(getresult, function(elem) {
return (elem.split(' #@# ')[0].length < 85 && elem.split(' #@# ')[0].match(regex))
});
resultlength = finalresult.length;
//console.log(finalresult);
if (resultlength > 0) {
let comment = "";
let link = "";
let commentId = "";
let postID = "";
$.each(finalresult, function(ind, value) {
setTimeout(function() {
comment = value.split(' #@# ')[0].replaceAll("'","\'");
link = value.split(' #@# ')[1];
commentId = value.split(' #@# ')[2];
postID = value.split(' #@# ')[3];
let msg = "";
let matches = comment.match(regex) || [];
let reason = '';
let weight = 0;
if (matches.length) {
for (var i = 0, l = matches.length; i < l; i++) {
reason += ' ---' + $.trim(matches[i]) + '--- ';
weight += $.trim(matches[i]).split(" ").length;
}
} else {
reason = "---Secret Reason :)---";
}
let len = comment.length;
msg = '[' + comment + '](' + link + ') **Blacklisted Word:** ' + reason ;
if (len < 30){
msg += ' **Low length:** ' + len ;
weight += 1;
}
else{
msg += ' **Length:** ' + len ;
}
msg += ' **Weight:** ' + weight;
let flag = (BIT === true ? " 🚩" : " 🏳️"); // change flag colar after rich flag limit(100)
if ((weight >= 4 && len < 60) || (len < 30 && weight > 2) ||( weight > 6) ||(len == 15)) {
if (!comment.match(igreg)){
msg += flag;
if (BIT) {
getFlagOption(commentId, API_KEY, Access_Token, Site_Name, Room_ID);
}
}
else
{
let igmatches = comment.match(igreg) || [];
msg += ' **Ignore Word:** ';
for (var j = 0, ln = igmatches.length; j < ln; j++) {
msg += ' ---' + $.trim(igmatches[j]) + '--- ';
}
msg += " ❌";
}
}
sendChatMessage(msg, Room_ID);
}, ind * Time_Out);
});
}
}
window.setInterval(function() {
flagComment();
}, 40 * Time_Out);
// --- check Bit in every 2 hours for the flag limit until next solution found ----
window.setInterval(function(){
BIT = true;
}, 72 * Time_Out);
// --- ------ ------------- ------------------
})();
function getFlagOption(commentId, API_KEY, Access_Token, Site_Name, Room_ID) {
//FlagMessage 1. Get flag option (it's vary for each comment). 2. Flag comment
checkOption();
async function checkOption() {
let opt = await getFlagOptions(commentId, API_KEY, Access_Token);
if (typeof opt !== 'undefined' && opt) {
let optionId = "";
let optionIDs = opt.items.map(a => a.option_id + " #@# " + a.title);
optionId = optionIDs[2].split(' #@# ')[0]; // hard coded for no longer needed.
autoFlag(commentId, optionId, API_KEY, Access_Token, Site_Name);
}
}
}
function autoFlag(commentId, optionId, API_KEY, Access_Token, Site_Name) {
var flag = new XMLHttpRequest();
flag.open("POST", "https://api.stackexchange.com/2.2/comments/" + commentId + "/flags/add", true);
flag.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
flag.onload = function(data) {
let response;
try {
response = JSON.parse(this.response);
if (response.error_name) {
console.warn(response.error_name);
BIT=false;
console.log(BIT);
}
}
catch (e) {
if(response.error_message != "")
console.warn(response.error_message);
}
}
flag.send("key=" + API_KEY + "&site=" + Site_Name + "&option_id=" + optionId + "&access_token=" + Access_Token);
}
function getFlagOptions(commentId, API_KEY, Access_Token) {
return new Promise(resolve => {
GM_xmlhttpRequest({
method: 'GET',
url: 'https://api.stackexchange.com/2.2/comments/' + commentId + '/flags/options?key=' + API_KEY + '&site=stackoverflow&access_token=' + Access_Token,
onload: function(data) {
resolve(JSON.parse(data.responseText));
}
});
});
}
function sendChatMessage(msg, Room_ID) {
GM_xmlhttpRequest({
method: 'GET',
url: 'https://chat.stackoverflow.com/rooms/' + Room_ID,
onload: function(response) {
var fkey = response.responseText.match(/hidden" value="([\dabcdef]{32})/)[1];
GM_xmlhttpRequest({
method: 'POST',
url: 'https://chat.stackoverflow.com/chats/' + Room_ID + '/messages/new',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'text=' + encodeURIComponent(msg.replaceAll("'","\'")
.replace(/\n|\r/g, " ")
.replaceAll(""","\"")
.trim()) + '&fkey=' + fkey,
onload: function(r) {
}
});
}
});
}
function getComments(API_KEY) {
let d = new Date();
let dm = new Date(d.setMonth(d.getMonth() - 1));
let dy = new Date(d.setYear(d.getFullYear() - 1));
dm = getFormattedDate(dm)
dy = getFormattedDate(dy)
let dto = Date.parse(dm);
let dfrom = Date.parse(dy);
dto = dto.toString().slice(0, -3);
dfrom = dfrom.toString().slice(0, -3);
return new Promise(resolve => {
GM_xmlhttpRequest({
method: 'GET',
url:'https://api.stackexchange.com/2.2/comments?page=1&pagesize=100&order=desc&sort=creation&filter=!4(lY7*YmhBbS4j1g_&site=stackoverflow&key=' + API_KEY,
onload: function(data) {
resolve(JSON.parse(data.responseText));
}
});
});
}
function getFormattedDate(date) {
let year = date.getFullYear();
let month = (1 + date.getMonth()).toString().padStart(2, '0');
let day = date.getDate().toString().padStart(2, '0');
return year + '-' + month + '-' + day;
}