1

我在为 Firefox 浏览器编写插件时遇到问题。当 CSS 类出现“error-class”或“warn-class”时,插件应该会发出声音。我使用突变观察器来检查我的班级是否出现并存储我播放声音的事实。

它用于 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 中的声音

4

0 回答 0