1

我对 chrome 扩展编程很陌生。我编写的程序可以按我的意愿工作,但是您需要单击特定选项卡才能使其工作。例如,我不能只在单独的窗口中浏览互联网时打开网站。

该网页是我公司的服务检查页面,公司的不同团队在该页面上发布他们已经供其他人查看和处理的问题。我的扩展程序会读取此页面以查找超过十分钟未被注意到的任何问题,并使用 toast 弹出窗口通知用户。我认为它是活动选项卡,因此扩展程序在当前单击的任何选项卡上运行,但我不确定。无论如何我可以做到这一点,所以这个扩展程序无需在标签中就可以运行吗?

这是我的代码,也欢迎任何有关如何设置扩展的建议:

清单.json

{
  "manifest_version": 2,

  "name": "Nagios Notifications",
  "description": "This extension allows a notification to be sent when a Nagios check reaches 10 minutes duration",
  "version": "1.0",
  "icons": { "16": "img/icon_16.png",
              "48": "img/icon_48.png",
              "128": "img/icon_128.png"},
  "browser_action": {
    "default_icon": "img/icon.png",
    "default_popup": "popup.html",
    "default_title": "Click to show notifications!"
  },
  "content_scripts": [
    {
      "matches": ["http://eotnagios/nagios/*"],
      "js": ["js/contentscript.js", "js/jquery-3.2.0.min.js", "js/jquery-toast-plugin-master/src/jquery.toast.js"],
      "css": ["js/jquery-toast-plugin-master/src/jquery.toast.css"],
      "all_frames": true,
      "run_at" : "document_start"
    }
  ],
  "permissions": [
    "activeTab",
    "storage",
    "background",
    "notifications",
    "tabs",
    "webNavigation",
    "http://eotnagios/nagios/*"
  ],
  "web_accessible_resources": [
    "img/*.png",
    "css/*.css",
    "js/*.js"
  ],
  "background": {
    "scripts": ["js/jquery-3.2.0.min.js", "js/jquery-toast-plugin-master/src/jquery.toast.js", "js/background.js"]
  }
}

背景.js

//When frame updates
chrome.webNavigation.onReferenceFragmentUpdated.addListener(function () {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
  chrome.tabs.sendMessage(tabs[0].id, {subject: "checkTable"}, function(response) {
    console.log(response);
    })
  })
})

//On page load
chrome.webNavigation.onCompleted.addListener(function () {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
  chrome.tabs.sendMessage(tabs[0].id, {subject: "checkTable"}, function(response) {
    console.log(response);
    })
  })
})

//When extension is clicked
chrome.browserAction.onClicked.addListener(function () {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
  chrome.tabs.sendMessage(tabs[0].id, {subject: "checkTable"}, function(response) {
    console.log(response);
    })
  })
})

//When page is clicked
chrome.tabs.onActivated.addListener(function () {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
  chrome.tabs.sendMessage(tabs[0].id, {subject: "checkTable"}, function(response) {
    console.log(response);
    })
  })
})

内容脚本.js:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.subject == "checkTable" || request.subject == "notifications") {
      var table = document.getElementsByClassName("status")[1];
      var durations = [];
      if (typeof table !== 'undefined'){
      for (var i = 0, row; row = table.rows[i]; i++) {
        var tableRow = {};
        //rows
        for (var j = 0, col; col = row.cells[j]; j++) {
          if (j == 4 && i != 0) {
          tableRow["duration"] = col.innerHTML;
          }
          else if (j == 1 && i != 0) {
            tableRow["service"] = col.innerHTML;
          }
        }
        if (Object.keys(tableRow).length == 2){
          durations.push(tableRow);
        }
      }
      for (var k = 0; k < durations.length; k++) {
        var acknowledged = "no";
        var time = durations[k]["duration"].split(" ")
        for (var item = 0; item < time.length; item++) {
          if (time[item].slice(-1) == "h") {
            var hours = time[item];
            if (hours.length == 2) {
              hours = parseInt(hours.substr(0, 1));
            }
            else if (hours.length == 3) {
              hours = parseInt(hours.substr(0, 2));
            }
          }
          else if (time[item].slice(-1) == "m") {
            var minutes = time[item];
            if (minutes.length == 2) {
              minutes = parseInt(minutes.substr(0, 1));
            }
            else if (minutes.length == 3) {
              minutes = parseInt(minutes.substr(0, 2));
            }

            var hourMinutes = hours * 60;
            minutes += hourMinutes;

            if (minutes > 10) {
              var check = durations[k]["service"];
              check = check.split(" ");
              for (var x = 0; x < check.length; x++){
                if (check[x].substr(0, 4) == "href") {
                  var line = check[x].split(";");
                  var host = line[1];
                  host = host.split("=")[1];
                  host = host.split("&")[0];
                  line = line[2];
                  line = line.split("#");
                  line = line[0].split(">")
                  line = line[0].split("=");
                  line = line[1].split("\"");
                  var service = line[0];
                }
                else if (check[x].substr(0, 3) == "src") {
                  var line = check[x].split("/");
                  line = line[3].split("\"");
                  line = line[0];
                  if (line == "ack.gif") {
                    acknowledged = "yes";
                  }}}}
              }
            }

            var serviceCheck = {"duration" : minutes, "service" : service, "host" : host, "acknowledged" : acknowledged};
            if (minutes > 10) {
              var toastColor;
              if (minutes <= 30) {
                toastColor = "orange";
              }
              else if (30 < minutes && minutes <= 60) {
                toastColor = "orangered";
              }
              else if (60 < minutes && minutes <= 120) {
                toastColor = "darkRed";
              }
              else {
                toastColor = "red";
              }

              //var notified = notifications(serviceCheck["host"], serviceCheck["service"]);
              if (serviceCheck["acknowledged"] == "no"){
            $.toast({
              text: "<strong>Minutes passed:</strong> " + minutes + " <strong>Service:</strong> " + service + " <strong>Host:</strong> " + host + " <strong>Acknowledged:</strong> " + acknowledged,
              hideAfter: false,
              position: 'bottom-right',
              bgColor: toastColor,
              textColor: 'white'
              });
            }
            }
              sendResponse(serviceCheck);
          }
        }
        }
      }
    )
4

0 回答 0