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,16 +1,7 @@
/*
Unused
Listen for messages from the page.
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 + "\"");
}
});
*/
* Simple script which looks for changes in the chat area, and rolls extra damage
* based on any relevant hit dice.
*/
function handleMoonslicer(message) {
var result = message.querySelector('div.sheet-result')
@ -36,11 +27,11 @@ function handleMoonslicer(message) {
submit_button.click();
}
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();
}
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();
}
textarea.value = old_text;
@ -49,16 +40,16 @@ function handleMoonslicer(message) {
/*
Send a message to the page script.
*/
function newMessageHandler(event) {
var message = event.target;
if (message.classList.contains('you')) {
function handleMutation(mutation) {
mutation.addedNodes.forEach( function(message) {
if (message.classList && message.classList.contains('you')) {
var labelElem = message.querySelector('div.sheet-rolltemplate-atk div.sheet-label');
var label = labelElem.querySelector('span a');
if (label && label.innerHTML == 'Moonslicer') {
handleMoonslicer(message);
};
};
});
}
/*
@ -67,4 +58,7 @@ the "from-content-script" element.
*/
var textchat = document.getElementById("textchat");
var content = textchat.getElementsByClassName("content")[0];
content.addEventListener("DOMNodeInserted", newMessageHandler);
var observer = new MutationObserver(function(mutations) {mutations.forEach(handleMutation);});
observer.observe(content, {childList: true});