use a MutationObserver instead, making performance not super terrible

This commit is contained in:
Jan Petykiewicz 2017-04-07 21:29:32 -07:00
parent 1f2531a347
commit 551d5ddb53

View File

@ -1,15 +1,6 @@
/* /*
Unused * Simple script which looks for changes in the chat area, and rolls extra damage
Listen for messages from the page. * based on any relevant hit dice.
If the message was from the page script, show an alert.
window.addEventListener("message", function(event) {
if (event.source == window &&
event.data.direction &&
event.data.direction == "from-page-script") {
alert("Content script received message: \"" + event.data.message + "\"");
}
});
*/ */
function handleMoonslicer(message) { function handleMoonslicer(message) {
@ -36,11 +27,11 @@ function handleMoonslicer(message) {
submit_button.click(); submit_button.click();
} }
if (hit0 == 2 || hit1 == 2) { if (hit0 == 2 || hit1 == 2) {
textarea.value = "&{template:npcdmg} {{damage=1}} {{dmg1flag=1}} {{dmg1=[[floor(1d4/2)]] to self}} {{dmg1type=Ice}}"; textarea.value = "&{template:npcdmg} {{damage=1}} {{dmg1flag=1}} {{dmg1=[[floor(1d4/2)]]}} {{dmg1type=Ice to self}}}";
submit_button.click(); submit_button.click();
} }
if (hit0 == 1 || hit1 == 1) { if (hit0 == 1 || hit1 == 1) {
textarea.value = "&{template:npcdmg} {{damage=1}} {{dmg1flag=1}} {{dmg1=[[1d6]] to self}} {{dmg1type=Ice}}"; textarea.value = "&{template:npcdmg} {{damage=1}} {{dmg1flag=1}} {{dmg1=[[1d6]]} {{dmg1type=Ice to self}}}";
submit_button.click(); submit_button.click();
} }
textarea.value = old_text; textarea.value = old_text;
@ -49,16 +40,16 @@ function handleMoonslicer(message) {
/* /*
Send a message to the page script. Send a message to the page script.
*/ */
function newMessageHandler(event) { function handleMutation(mutation) {
var message = event.target; mutation.addedNodes.forEach( function(message) {
if (message.classList && message.classList.contains('you')) {
if (message.classList.contains('you')) {
var labelElem = message.querySelector('div.sheet-rolltemplate-atk div.sheet-label'); var labelElem = message.querySelector('div.sheet-rolltemplate-atk div.sheet-label');
var label = labelElem.querySelector('span a'); var label = labelElem.querySelector('span a');
if (label && label.innerHTML == 'Moonslicer') { if (label && label.innerHTML == 'Moonslicer') {
handleMoonslicer(message); handleMoonslicer(message);
}; };
}; };
});
} }
/* /*
@ -67,4 +58,7 @@ the "from-content-script" element.
*/ */
var textchat = document.getElementById("textchat"); var textchat = document.getElementById("textchat");
var content = textchat.getElementsByClassName("content")[0]; var content = textchat.getElementsByClassName("content")[0];
content.addEventListener("DOMNodeInserted", newMessageHandler);
var observer = new MutationObserver(function(mutations) {mutations.forEach(handleMutation);});
observer.observe(content, {childList: true});