Last active
April 6, 2017 12:25
-
-
Save ferrybig/67efa17d8c6bf37770cb89847026ec1a to your computer and use it in GitHub Desktop.
Spamtracker Reboot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name SpamtrackerReboot | |
// @namespace http://tampermonkey.net/ | |
// @version 0.6 | |
// @description Rewrite of the spamtracker project, this userscript will notify you using sound and a notification if a new spam post has been posted in any smoke detector supported rooms | |
// @author You | |
// @match *://chat.meta.stackexchange.com/* | |
// @match *://chat.stackexchange.com/* | |
// @match *://chat.stackoverflow.com/* | |
// @run-at document-end | |
// @grant none | |
// ==/UserScript== | |
// ========================================================================== | |
// | |
// | |
// | |
// This project has been moved to https://github.com/ferrybig/SpamTrackerReboot/ | |
// | |
// | |
// | |
// ============================================================================ | |
window.Spamtracker = (function(target) { | |
'use strict'; | |
var useSound = true; | |
var sound = new Audio('//cdn-chat.sstatic.net/chat/meta2.mp3'); | |
var callback; | |
var lastMessageObserverTarget; | |
var lastMessageObserver; | |
var notifications = {}; | |
var restoreCallback = function() { | |
callback = function(url, elm) { | |
if('fire' in window) { | |
window.focus(); | |
fire.openReportPopupForMessage(elm); | |
} else { | |
window.open(url); | |
} | |
}; | |
}; | |
/** | |
* Usefull four our script to interact with clicking on notifications | |
*/ | |
var setCallback = function(newCallback) { | |
callback = newCallback; | |
}; | |
var notifyMe = function (id, title, message, callback, url, elm) { | |
//console.log("Trying notification!", url); | |
var notification = new Notification(title, { body: message, icon: "//i.imgur.com/kS4QNIv.png" }); | |
notification.onshow = function() { | |
setTimeout(function() { | |
dismissNotification(id); | |
}, 15000); | |
}; | |
notification.onclick = function() { | |
callback(url, elm); | |
dismissNotification(id); | |
}; | |
notifications[id] = notification; | |
}; | |
var dismissNotification = function(id) { | |
notifications[id].close(); | |
delete notifications[id]; | |
}; | |
function processChatMessage(message) { | |
//console.log("Chat message!" + message.children[1].innerHTML); | |
var smoke = /\/\/goo.gl\/eLDYqh|spam|offensive|abusive/i; | |
var content = message.children[1].innerHTML; | |
var i, msg = {}, parts, ch, path, hash, site = '', qId = ''; | |
if (smoke.test(content) && /\/\/[a-z]*.stackexchange.com|stackoverflow.com|superuser.com|serverfault.com|askubuntu.com|stackapps.com|mathoverflow.net/i.test(content)) { | |
//console.log("Match!"); | |
ch = message.children[1].children; | |
for (i=ch.length-1; i>=0; i--) { | |
if (ch[i].tagName == 'A') { | |
hash = ch[i].href.split('#'); | |
path = ch[i].href.split('/'); | |
if (path[3] == 'questions' && hash.length>1) { | |
site = path[2]; | |
qId = hash[1]; | |
} | |
else if (/^[qa]/.test(path[3])) { | |
site = path[2]; | |
qId = path[4]; | |
} | |
} | |
} | |
if (site && qId && useSound) { | |
sound.play(); | |
} | |
msg.id = site+'-'+qId+'-'+Date.now(); | |
parts = message.children[1].textContent.split(': '); | |
if (parts.length > 1) { | |
msg.title = parts[0]; | |
msg.message = parts[1]; | |
} else { | |
// msg.title = 'Flag Request'; | |
// msg.message = message.children[1].textContent; | |
return; // Not used for now... | |
} | |
msg.type = 'chat'; | |
notifyMe(msg.id, msg.title, msg.message, callback, '//' + site + '/q/' + qId, message); | |
} | |
} | |
var registerMessageObserver = function(elm) { | |
if(elm === lastMessageObserverTarget) { | |
return; | |
} | |
//console.log("Register message observer!"); | |
lastMessageObserverTarget = elm; | |
if(lastMessageObserver !== undefined) { | |
lastMessageObserver.disconnect(); | |
} | |
var children = elm.getElementsByClassName('message'); | |
if(children.length) { | |
processChatMessage(children[children.length - 1]); | |
} | |
lastMessageObserver = new MutationObserver(function(mutations) { | |
processChatMessage(children[children.length - 1]); | |
}); | |
lastMessageObserver.observe(elm, { childList: true }); | |
}; | |
var registerMonologObserver = function(elm) { | |
//console.log("Register monolog observer!"); | |
var children = elm.getElementsByClassName('messages'); | |
if(children.length) { | |
registerMessageObserver(children[children.length - 1]); | |
} else { | |
var observer = new MutationObserver(function(mutations) { | |
registerMessageObserver(children[children.length - 1]); | |
observer.disconnect(); | |
}); | |
observer.observe(elm, { childList: true }); | |
} | |
}; | |
var registerObserver = function() { | |
//console.log("Register main observer!"); | |
Notification.requestPermission(); | |
var children = target.getElementsByClassName('monologue'); | |
if(children.length) { | |
registerMonologObserver(children[children.length - 1]); | |
} | |
var observer = new MutationObserver(function(mutations) { | |
registerMonologObserver(children[children.length - 1]); | |
}); | |
observer.observe(target, { childList: true }); | |
}; | |
registerObserver(); | |
restoreCallback(); | |
return { | |
setCallback: setCallback, | |
restoreCallback: restoreCallback, | |
}; | |
})(document.getElementById('chat')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment