我对编译Chrome扩展程序非常陌生。我编程的那个按照我的意愿工作,但是你需要点击特定的标签才能使它工作。例如,我不能在一个单独的窗口中浏览互联网时打开网站。
这个网页是我公司的服务检查页面,公司的不同团队发布了他们为他人查看和处理的问题。我的扩展程序会读取此页面,查看已超过十分钟未被注意的任何问题,并使用Toast弹出窗口通知用户。我认为它是活动选项卡,因此扩展程序在当前单击的任何选项卡上运行,但我不确定。无论如何我可以做到这样,这个扩展运行而不必在标签中?
以下是我的代码,欢迎任何关于如何设置扩展程序的建议:
的manifest.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"]
}
}
background.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);
})
})
})
contentscript.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);
}
}
}
}
)