出现CSS类时触发事件

时间:2019-06-07 13:12:10

标签: javascript firefox add-on mutation-observers mutation-events

我在为Firefox浏览器编码插件时遇到问题。当CSS类显示为“错误类”或“警告类”时,插件应该会发出声音。我使用变异观察器检查我的班级是否出现,并存储我播放声音的事实。

这是Grafana仪表板的状态面板,用于在服务未及时响应时发出警报。

这是我的实际代码:

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>)

这里是清单:

{
  "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"]
    }
  ]

}

预期结果是当“错误类”出现在html页面的元素上时,base64中的声音会播放

0 个答案:

没有答案