Skip to content

Instantly share code, notes, and snippets.

@ferrybig
Created June 10, 2016 11:50
Show Gist options
  • Save ferrybig/8e8cc059a4d356a6dac8f0e9818b0fb2 to your computer and use it in GitHub Desktop.
Save ferrybig/8e8cc059a4d356a6dac8f0e9818b0fb2 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Spam Reporter
// @namespace http://github.com/Tiny-Giant
// @version 1.0.0.0
// @description Adds a link to the post menu for most posts.
// @author @TinyGiant
// @include /^https?:\/\/\w*.?(stackexchange.com|stackoverflow.com|serverfault.com|superuser.com|askubuntu.com|stackapps.com|mathoverflow.net)\/q(uestions)?\/\d+/
// @grant GM_xmlhttpRequest
// ==/UserScript==
/* jshint -W097 */
(function(){
'use strict';
var notify = (function(){
var count = 0, timeout;
return function(m,t) {
console.log(m,t);
if($('#notify-' + count).length) {
clearTimeout(timeout);
StackExchange.notify.close(count);
}
StackExchange.notify.show(m,++count);
if(t) timeout = setTimeout(StackExchange.notify.close.bind(null,count), t);
};
})();
var engage = function(scope) {
if (!scope) {
return false;
}
var room = 11540; // use 68414 for testing
var postLink = scope.querySelector('.short-link').href;
var reportSent = function(response) {
console.log(response);
if (response.status !== 200) {
notify('Error sending request: ' + resp.responseText);
return false;
}
notify('Close vote request sent.',1000);
};
var sendReport = function(response) {
console.log(response);
if (response.status !== 200) {
notify('Failed sending report, check the console for more information.');
return false;
}
var fkey = response.responseText.match(/hidden" value="([\dabcdef]{32})/)[1];
if (!fkey) {
notify('Failed retrieving key, is the room URL valid?');
return false;
}
var reportStr = '!!/alive ' + postLink;
var options = {
method: 'POST',
url: 'http://chat.stackexchange.com/chats/' + room + '/messages/new',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: 'text=' + encodeURIComponent(reportStr) + '&fkey=' + fkey,
onload: reportSent
};
GM_xmlhttpRequest(options);
};
var report = function(e) {
e.preventDefault();
if(!confirm('Do you really want to report this post?')) {
return false;
}
var options = {
method: 'GET',
url: 'http://chat.stackexchange.com/rooms/' + room,
onload: sendReport
};
GM_xmlhttpRequest(options);
};
var sep = document.createElement('span');
sep.className = 'lsep';
sep.textContent = '|';
scope.appendChild(sep);
var link = document.createElement('a');
link.href = '#';
link.textContent = 'report';
link.addEventListener('click', report, false);
scope.appendChild(link);
};
var menus = document.querySelectorAll('.post-menu');
for(var i in Object.keys(menus)) engage(menus[i]);
$(document).ajaxComplete(function(){
var url = arguments[2].url;
if (/^\/posts\/ajax-load-realtime\//.test(url)) engage(/\d+/.exec(url)[0]);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment