Created
February 3, 2017 02:21
-
-
Save TristanWiley/bb607c7e0b8504729ff5b224d4062da9 to your computer and use it in GitHub Desktop.
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
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