Skip to content

Instantly share code, notes, and snippets.

@TristanWiley
Last active March 12, 2019 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TristanWiley/20453a808202f6c888218a70053f23a1 to your computer and use it in GitHub Desktop.
Save TristanWiley/20453a808202f6c888218a70053f23a1 to your computer and use it in GitHub Desktop.
Add "move to trash" to the StackOverflow/StackExchange chat rooms. Only works for room owners.
const trashBinId = '23262'
function moveToTrash(el) {
const messageId = el.parentElement.parentElement.id.split('-')[1]
const formData = new FormData();
formData.append('ids', messageId);
formData.append('to', trashBinId);
formData.append('fkey', fkey().fkey);
fetch(`https://chat.stackoverflow.com/admin/movePosts/${CHAT.CURRENT_ROOM_ID}`, {
credentials: "include",
mode: 'cors',
method: "POST",
body: formData
})
}
function addTrashToNodeChildren(node) {
Array.from(node.querySelectorAll('.message > .meta')).forEach(el => {
el.innerHTML += `<span onclick="moveToTrash(this)" style="vertical-align: middle; cursor: pointer;"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10pt" height="10pt" viewBox="0 0 10 10" version="1.1" style="">
<g id="surface1">
<path style=" stroke:none;fill-rule:nonzero;fill:rgb(0%,0%,0%);fill-opacity:1;" d="M 2.5 7.917969 C 2.5 8.375 2.875 8.75 3.332031 8.75 L 6.667969 8.75 C 7.125 8.75 7.5 8.375 7.5 7.917969 L 7.5 2.917969 L 2.5 2.917969 Z M 7.917969 1.667969 L 6.457031 1.667969 L 6.042969 1.25 L 3.957031 1.25 L 3.542969 1.667969 L 2.082031 1.667969 L 2.082031 2.5 L 7.917969 2.5 Z M 7.917969 1.667969 "></path>
</g>
</svg></span>`
})
}
var observer = new MutationObserver(function(mutations) {
mutations.map(m => Array.from(m.addedNodes)).forEach(addedNode => {
addedNode.forEach(node => {
const isUserContainer = node.classList && Array.from(node.classList).includes('user-container')
if (isUserContainer) {
addTrashToNodeChildren(node)
}
})
})
});
observer.observe(document.getElementById('chat'), {attributes: false, childList: true, characterData: false, subtree:true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment