Skip to content

Instantly share code, notes, and snippets.

@boomboompower
Created April 12, 2020 07:47
Show Gist options
  • Save boomboompower/0841f6f6cc8e5cfe387c2bc2d505072e to your computer and use it in GitHub Desktop.
Save boomboompower/0841f6f6cc8e5cfe387c2bc2d505072e to your computer and use it in GitHub Desktop.
Logs all new chat messages into the browser console. Uses the MutationObserver API
(function () {
// Sources from https://stackoverflow.com/a/14570614/12697448
// Thanks vsync for the spectacular code
var observeDOM = (function () {
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
return function (obj, callback) {
if (!obj || !obj.nodeType === 1)
return; // validation
if (MutationObserver) {
// define a new observer
var obs = new MutationObserver(function (mutations, observer) {
callback(mutations);
})
// have the observer observe foo for changes in children
obs.observe(obj, {
childList: true,
subtree: true
});
} else if (window.addEventListener) {
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
}
})();
const chatListContainers = document.getElementsByClassName('chat-list__list-container');
if (chatListContainers.length === 0) {
console.error('Unable to find the chat list container element.');
return;
}
// Get the first one
const selectedChatList = chatListContainers[0];
// Use the MutationObserver to track changes on the chat element
observeDOM(selectedChatList, function (emmy) {
// Loop through all elements which have been modified
for (let i = 0; i < emmy.length; i++) {
// Added chat elements/nodes
const addedNodes = emmy[i].addedNodes;
// If there are added nodes continue
if (addedNodes.length > 0) {
// Loop through each node
for (let nodeCount = 0; nodeCount < addedNodes.length; nodeCount++) {
// Log the text of the node (non-formatted)
console.log(addedNodes[nodeCount].innerText);
}
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment