I am currently writing a Chrome extension for personal use. It is a plugin for a browser game that would call alert()
function when I am under attack so that I don't need to check the tab constantly. It is done by checking if a div has a new class added to to a div. (The class is added by the game to turn a div into red background, indicating an attack) Currently, I am using a timer of 10 seconds to check if the class exists. It looks something like this:
//jquery already included
setInterval(function() {
if ( $( ".attacked" ).length )
alert("You are under attack!");
}, 10000)
The class will be removed by the game after I have do some action to handle the attack, so there is no need to set exit condition for the loop.
It works, but it is too heavy. It significantly slows my PC down. Is there a more light weight method to achieve the same thing? I am thinking of is it possible to add a event listener on the div class change, or if I am able to check the ajax return data while the ajax request is not called by me, but other methods are also welcome.
EDIT: This question was marked as duplicated with another similar question. It looks the same but this one is a different situation. I am writing a Chrome extension that the change in class is trigger by script in the original site, and I have no way to touch those codes, so it is not possible to make it trigger an event.