Last active
April 25, 2018 18:00
-
-
Save rlemon/f5d3dc0263da7e37a27d0976555d3f55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const KEYWORD_MAP = {}; | |
/* | |
KEYWORD_MAP[<user_id>][<term>] returns the reply | |
*/ | |
KEYWORD_MAP[774078] = { | |
'jesus': 'https://i.imgur.com/6Vv7kyL.png' | |
}; | |
KEYWORD_MAP[617762] = { | |
'BAM!' : 'SPACE GUN!' | |
}; | |
function keywordParser(node) { | |
if( !node.classList.contains('message') || node.classList.contains('pending') ) { | |
return; | |
} | |
const sig = node.parentNode.parentNode.querySelector('a.signature'); | |
if( !sig ) { | |
return; | |
} | |
const [,,userId] = sig.getAttribute('href').split('/'); | |
if( !(Number(userId) in KEYWORD_MAP) ) { | |
return; | |
} | |
const text = node.textContent; | |
// right now we only trigger on the first found word. | |
const matchedKey = Object.keys(KEYWORD_MAP[userId]).find(key => text.includes(key)); | |
if( !matchedKey ) { | |
return; | |
} | |
const reply = KEYWORD_MAP[userId][matchedKey]; | |
const [,messageId] = node.id.split('-'); | |
sendMessage(messageId, reply); | |
} | |
function sendMessage(messageId, reply) { | |
const [roomid] = /\d+/.exec(location); | |
fetch(`https://chat.stackoverflow.com/chats/${roomid}/messages/new`, { | |
credentials: 'same-origin', | |
method: 'POST', | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }, | |
body: `fkey=${fkey().fkey}&text=:${messageId} ${reply}` | |
}).catch( error => { | |
console.log(error); | |
setTimeout(() => { | |
sendMessage(messageId, reply); | |
}, 1000); | |
}); | |
} | |
setTimeout(() => { | |
DOMObserver.addParser(keywordParser); | |
}, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment