-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSpamReporter.js
94 lines (85 loc) · 3.28 KB
/
SpamReporter.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
// ==UserScript==
// @name Spam Reporter
// @namespace https://github.com/Tunaki/stackoverflow-userscripts
// @version 0.1
// @description Adds a 'SPAM' link below answers that sends a report for SmokeDetector in SOCVR. Intended to be used for spam / offensive answers.
// @author Tunaki
// @include /^https?:\/\/\w*.?(stackexchange.com|stackoverflow.com|serverfault.com|superuser.com|askubuntu.com|stackapps.com|mathoverflow.net)\/.*/
// @grant GM_xmlhttpRequest
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==
var room = 41570;
function sendRequest(event) {
var messageJSON;
try {
messageJSON = JSON.parse(event.data);
} catch (zError) { }
if (!messageJSON) return;
if (messageJSON[0] == 'postHrefReport') {
var link = messageJSON[1];
if (!confirm('Do you really want to report this post?')) {
return false;
}
GM_xmlhttpRequest({
method: 'GET',
url: 'http://chat.stackoverflow.com/rooms/' + room,
onload: function (response) {
var fkey = response.responseText.match(/hidden" value="([\dabcdef]{32})/)[1];
var reportStr = '!!/report ' + link;
GM_xmlhttpRequest({
method: 'POST',
url: 'http://chat.stackoverflow.com/chats/' + room + '/messages/new',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: 'text=' + encodeURIComponent(reportStr) + '&fkey=' + fkey
});
}
});
}
};
window.addEventListener('message', sendRequest, false);
const ScriptToInject = function() {
function addXHRListener(callback) {
let open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', callback.bind(null, this), false);
open.apply(this, arguments);
};
};
function addReportLink(postId) {
var $posts;
if(!postId) {
$posts = $('.post-menu');
} else {
$posts = $('[data-questionid="' + postId + '"] .post-menu,[data-answerid="' + postId + '"] .post-menu');
}
$posts.each(function() {
var $this = $(this);
var $postLink = $this.find('a.short-link');
var postId = $postLink.attr('id').split('-')[2];
$this.append($('<span>').attr('class', 'lsep').html('|'));
$this.append($('<a>').attr('class', 'report-link').html('SPAM').click(function (e) {
e.preventDefault();
var href = $postLink.get(0).href;
var messageTxt = JSON.stringify(['postHrefReport', href]);
window.postMessage(messageTxt, "*");
}));
});
};
addXHRListener(function(xhr) {
let matches = /question" data-questionid="(\d+)/.exec(xhr.responseText);
if (matches === null) {
matches = /answer" data-answerid="(\d+)/.exec(xhr.responseText);
if (matches === null) {
return;
}
}
addReportLink(matches[1]);
});
$(document).ready(function() {
addReportLink();
});
}
const ScriptToInjectNode = document.createElement('script');
document.body.appendChild(ScriptToInjectNode);
const ScriptToInjectContent = document.createTextNode('(' + ScriptToInject.toString() + ')()');
ScriptToInjectNode.appendChild(ScriptToInjectContent);