Skip to content

Instantly share code, notes, and snippets.

@TristanWiley
Created February 3, 2017 02:21
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/bb607c7e0b8504729ff5b224d4062da9 to your computer and use it in GitHub Desktop.
Save TristanWiley/bb607c7e0b8504729ff5b224d4062da9 to your computer and use it in GitHub Desktop.
function onElementInserted(containerSelector, elementSelector, callback) {
var onMutationsObserved = function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
var elements = $(mutation.addedNodes).find(elementSelector);
for (var i = 0, len = elements.length; i < len; i++) {
callback(elements[i]);
}
}
});
};
var target = $(containerSelector)[0];
var config = {
childList: true,
subtree: true
};
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(onMutationsObserved);
observer.observe(target, config);
}
var contestants = [];
onElementInserted('body', '.message', function(element) {
var message = $(element).text();
if (message.includes("!taco")) {
var user = $(element).parent().find('.from').text();
var entry = {
message: message,
user: user
}
contestants.push(entry);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment