Reputation: 113
I've a problem coding an addons for firefox browser. The addons is supposed to make sound when a CSS class appear "error-class" or "warn-class". I use the mutation observer to check if my class appear and to store the fact that I played the sound.
It's for a Grafana dashboard using status panel to alert when a service doesn't respond on time.
here my actual code :
let target = document.querySelectorAll(".panel-container");
let histo = new Array();
for (let i = 0; i < target.length; i++) {
let observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
let foo = mutation.target.getAttribute("class")
let error_class = "error-state"
let warning_class = "warn-state"
let ok_class = "ok-state"
let id = mutation.target.parentElement.parentElement.parentElement.parentElement.parentElement.id;
if (((foo.includes(error_class))|| (foo.includes(warning_class)))&& !(histo.includes(id))){
histo.push(id);
beep();
}
if (foo.includes(ok_class) && histo.includes(id)){
histo = arrayRemove(histo, id);
}
console.error(histo);
});
});
// configuration of the observer
let config = { attributes: true };
// pass in the target node, as well as the observer options
observer.observe(target[i], config);
}
function arrayRemove(arr, value) {
return arr.filter(function(ele){
return ele != value;
});
}
function beep() {
var snd = new Audio(<audio in base 64>)
and here the manifest :
{
"manifest_version": 2,
"name": "sound_on_grafana",
"version": "1.1",
"description": "make sound for error and warning state",
"icons": {
"48": "icons/border-48.png"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["sound_on_grafana.js"]
}
]
}
the expecting result is when "error-class" appear on an element of the html page the sound in base64 is played
Upvotes: 1
Views: 251