fixes chrome extension credentials using POST instead GET
This commit is contained in:
parent
9f4397a760
commit
683985fbbf
|
@ -3,309 +3,321 @@ var isFetching = null;
|
|||
var storedEvents = new Array();
|
||||
var notVisited = {};
|
||||
|
||||
$(window).on('load', function() {
|
||||
initilise();
|
||||
// Wait some ms to throw main function
|
||||
var delay = setTimeout(main, 100);
|
||||
resetInterval();
|
||||
$(window).on("load", function() {
|
||||
initilise();
|
||||
// Wait some ms to throw main function
|
||||
var delay = setTimeout(main, 100);
|
||||
resetInterval();
|
||||
});
|
||||
|
||||
function fetchEvents() {
|
||||
return storedEvents;
|
||||
return storedEvents;
|
||||
}
|
||||
|
||||
function fetchNotVisited() {
|
||||
return notVisited;
|
||||
return notVisited;
|
||||
}
|
||||
|
||||
function removeNotVisited(eventId) {
|
||||
if (notVisited[eventId] === true) delete notVisited[eventId];
|
||||
if (notVisited[eventId] === true) delete notVisited[eventId];
|
||||
}
|
||||
|
||||
function main() {
|
||||
chrome.runtime.sendMessage({ text: "FETCH_EVENTS" });
|
||||
// Do not fetch if is fetching now
|
||||
if (isFetching) return;
|
||||
isFetching = true;
|
||||
|
||||
chrome.runtime.sendMessage({text: "FETCH_EVENTS"});
|
||||
// Do not fetch if is fetching now
|
||||
if (isFetching) return;
|
||||
isFetching = true;
|
||||
var url =
|
||||
localStorage["ip_address"] +
|
||||
"/include/api.php?op=get&op2=events&return_type=json";
|
||||
var feedUrl = url;
|
||||
var data = new FormData();
|
||||
|
||||
var feedUrl = localStorage["ip_address"]+'/include/api.php?op=get&op2=events&return_type=json&apipass='+localStorage["api_pass"]+'&user='+localStorage["user_name"]+'&pass='+localStorage["pass"];
|
||||
data.append("apipass", localStorage["api_pass"]);
|
||||
data.append("user", localStorage["user_name"]);
|
||||
data.append("pass", localStorage["pass"]);
|
||||
|
||||
req = new XMLHttpRequest();
|
||||
req.onload = handleResponse;
|
||||
req.onerror = handleError;
|
||||
req.open("GET", feedUrl, true);
|
||||
req.withCredentials = true
|
||||
req.send(null);
|
||||
req = new XMLHttpRequest();
|
||||
req.onload = handleResponse;
|
||||
req.onerror = handleError;
|
||||
req.open("POST", feedUrl, true);
|
||||
req.withCredentials = true;
|
||||
req.send(data);
|
||||
}
|
||||
|
||||
function handleError() {
|
||||
chrome.runtime.sendMessage({text: "FETCH_EVENTS_URL_ERROR"});
|
||||
isFetching = false;
|
||||
chrome.runtime.sendMessage({ text: "FETCH_EVENTS_URL_ERROR" });
|
||||
isFetching = false;
|
||||
}
|
||||
|
||||
function handleResponse() {
|
||||
var doc = req.responseText;
|
||||
if (doc=="auth error") {
|
||||
chrome.runtime.sendMessage({text: "FETCH_EVENTS_URL_ERROR"});
|
||||
} else {
|
||||
var n = doc.search("404 Not Found");
|
||||
if (n>0) {
|
||||
chrome.runtime.sendMessage({text: "FETCH_EVENTS_DATA_ERROR"});
|
||||
} else {
|
||||
getEvents(doc);
|
||||
chrome.runtime.sendMessage({text: "FETCH_EVENTS_SUCCESS"});
|
||||
}
|
||||
}
|
||||
isFetching = false;
|
||||
var doc = req.responseText;
|
||||
if (doc == "auth error") {
|
||||
chrome.runtime.sendMessage({ text: "FETCH_EVENTS_URL_ERROR" });
|
||||
} else {
|
||||
var n = doc.search("404 Not Found");
|
||||
if (n > 0) {
|
||||
chrome.runtime.sendMessage({ text: "FETCH_EVENTS_DATA_ERROR" });
|
||||
} else {
|
||||
getEvents(doc);
|
||||
chrome.runtime.sendMessage({ text: "FETCH_EVENTS_SUCCESS" });
|
||||
}
|
||||
}
|
||||
isFetching = false;
|
||||
}
|
||||
|
||||
function getEvents(reply){
|
||||
var fetchedEvents = parseReplyEvents(reply);
|
||||
function getEvents(reply) {
|
||||
var fetchedEvents = parseReplyEvents(reply);
|
||||
|
||||
// If there is no events requested, mark all as visited
|
||||
if (storedEvents.length == 0) {
|
||||
notVisited = {};
|
||||
storedEvents = fetchedEvents;
|
||||
return;
|
||||
}
|
||||
// If there is no events requested, mark all as visited
|
||||
if (typeof storedEvents != "undefined" && storedEvents.length == 0) {
|
||||
notVisited = {};
|
||||
storedEvents = fetchedEvents;
|
||||
return;
|
||||
}
|
||||
|
||||
// Discriminate the new events
|
||||
newEvents=fetchNewEvents(fetchedEvents,storedEvents);
|
||||
var newNotVisited = {};
|
||||
var notVisitedCount = 0;
|
||||
|
||||
// Check if popup is displayed to make some actions
|
||||
var views = chrome.extension.getViews({ type: "popup" });
|
||||
for(var k=0;k<newEvents.length;k++){
|
||||
newNotVisited[newEvents[k]['id']] = true;
|
||||
if (views.length == 0) {
|
||||
notVisitedCount++;
|
||||
displayNotification (newEvents[k])
|
||||
alertsSound(newEvents[k]);
|
||||
}
|
||||
}
|
||||
// Discriminate the new events
|
||||
newEvents = fetchNewEvents(fetchedEvents, storedEvents);
|
||||
var newNotVisited = {};
|
||||
var notVisitedCount = 0;
|
||||
|
||||
// Make that the old events marked as not visited remains with the
|
||||
// same status
|
||||
for(var k=0;k<fetchedEvents.length;k++){
|
||||
if (notVisited[fetchedEvents[k]['id']] === true) {
|
||||
newNotVisited[fetchedEvents[k]['id']] = true;
|
||||
notVisitedCount++;
|
||||
}
|
||||
}
|
||||
notVisited = newNotVisited;
|
||||
// Check if popup is displayed to make some actions
|
||||
var views = chrome.extension.getViews({ type: "popup" });
|
||||
for (var k = 0; k < newEvents.length; k++) {
|
||||
newNotVisited[newEvents[k]["id"]] = true;
|
||||
if (views.length == 0) {
|
||||
notVisitedCount++;
|
||||
displayNotification(newEvents[k]);
|
||||
alertsSound(newEvents[k]);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the number
|
||||
localStorage["new_events"] = (views.length == 0) ? notVisitedCount : 0;
|
||||
updateBadge();
|
||||
// Make that the old events marked as not visited remains with the
|
||||
// same status
|
||||
for (var k = 0; k < fetchedEvents.length; k++) {
|
||||
if (notVisited[fetchedEvents[k]["id"]] === true) {
|
||||
newNotVisited[fetchedEvents[k]["id"]] = true;
|
||||
notVisitedCount++;
|
||||
}
|
||||
}
|
||||
notVisited = newNotVisited;
|
||||
|
||||
// Store the requested events
|
||||
storedEvents = fetchedEvents;
|
||||
// Update the number
|
||||
localStorage["new_events"] = views.length == 0 ? notVisitedCount : 0;
|
||||
updateBadge();
|
||||
|
||||
// Store the requested events
|
||||
storedEvents = fetchedEvents;
|
||||
}
|
||||
|
||||
function updateBadge() {
|
||||
if (localStorage["new_events"] != 0) {
|
||||
chrome.browserAction.setBadgeBackgroundColor({color:[0,200,0,255]});
|
||||
chrome.browserAction.setBadgeText({ text: localStorage["new_events"] });
|
||||
} else {
|
||||
chrome.browserAction.setBadgeText({ text: "" });
|
||||
}
|
||||
if (localStorage["new_events"] != 0) {
|
||||
chrome.browserAction.setBadgeBackgroundColor({ color: [0, 200, 0, 255] });
|
||||
chrome.browserAction.setBadgeText({ text: localStorage["new_events"] });
|
||||
} else {
|
||||
chrome.browserAction.setBadgeText({ text: "" });
|
||||
}
|
||||
}
|
||||
|
||||
function fetchNewEvents(A,B){
|
||||
var arrDiff = new Array();
|
||||
for(var i = 0; i < A.length; i++) {
|
||||
var id = false;
|
||||
for(var j = 0; j < B.length; j++) {
|
||||
if(A[i]['id'] == B[j]['id']) {
|
||||
id = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!id) {
|
||||
arrDiff.push(A[i]);
|
||||
}
|
||||
}
|
||||
return arrDiff;
|
||||
function fetchNewEvents(A, B) {
|
||||
var arrDiff = new Array();
|
||||
for (var i = 0; i < A.length; i++) {
|
||||
var id = false;
|
||||
for (var j = 0; j < B.length; j++) {
|
||||
if (A[i]["id"] == B[j]["id"]) {
|
||||
id = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!id) {
|
||||
arrDiff.push(A[i]);
|
||||
}
|
||||
}
|
||||
return arrDiff;
|
||||
}
|
||||
|
||||
function parseReplyEvents(reply) {
|
||||
// Split the API response
|
||||
var data;
|
||||
try {
|
||||
data = JSON.parse(reply);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return [];
|
||||
}
|
||||
var e_array = JSON.parse(reply).data;
|
||||
|
||||
function parseReplyEvents (reply) {
|
||||
|
||||
// Split the API response
|
||||
var data = JSON.parse(reply)
|
||||
var e_array = JSON.parse(reply).data;
|
||||
|
||||
// Form a properly object
|
||||
var fetchedEvents=new Array();
|
||||
for(var i=0;i<e_array.length;i++){
|
||||
var event=e_array[i];
|
||||
fetchedEvents.push({
|
||||
'id' : event.id_evento,
|
||||
'agent_name' : event.agent_name,
|
||||
'agent' : event.id_agente,
|
||||
'date' : event.timestamp,
|
||||
'title' : event.evento,
|
||||
'module' : event.id_agentmodule,
|
||||
'type' : event.event_type,
|
||||
'source' : event.source,
|
||||
'severity' : event.criticity_name,
|
||||
'visited' : false
|
||||
});
|
||||
}
|
||||
// Return the events
|
||||
return fetchedEvents;
|
||||
// Form a properly object
|
||||
var fetchedEvents = new Array();
|
||||
for (var i = 0; i < e_array.length; i++) {
|
||||
var event = e_array[i];
|
||||
fetchedEvents.push({
|
||||
id: event.id_evento,
|
||||
agent_name: event.agent_name,
|
||||
agent: event.id_agente,
|
||||
date: event.timestamp,
|
||||
title: event.evento,
|
||||
module: event.id_agentmodule,
|
||||
type: event.event_type,
|
||||
source: event.source,
|
||||
severity: event.criticity_name,
|
||||
visited: false
|
||||
});
|
||||
}
|
||||
// Return the events
|
||||
return fetchedEvents;
|
||||
}
|
||||
|
||||
function alertsSound(pEvent){
|
||||
if(localStorage["sound_alert"]!="on"){
|
||||
return;
|
||||
}
|
||||
function alertsSound(pEvent) {
|
||||
if (localStorage["sound_alert"] != "on") {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (pEvent['severity']) {
|
||||
case "Critical":
|
||||
playSound(localStorage["critical"]);
|
||||
break;
|
||||
case "Informational":
|
||||
playSound(localStorage["informational"]);
|
||||
break;
|
||||
case "Maintenance":
|
||||
playSound(localStorage["maintenance"]);
|
||||
break;
|
||||
case "Normal":
|
||||
playSound(localStorage["normal"]);
|
||||
break;
|
||||
case "Warning":
|
||||
playSound(localStorage["warning"]);
|
||||
break;
|
||||
}
|
||||
switch (pEvent["severity"]) {
|
||||
case "Critical":
|
||||
playSound(localStorage["critical"]);
|
||||
break;
|
||||
case "Informational":
|
||||
playSound(localStorage["informational"]);
|
||||
break;
|
||||
case "Maintenance":
|
||||
playSound(localStorage["maintenance"]);
|
||||
break;
|
||||
case "Normal":
|
||||
playSound(localStorage["normal"]);
|
||||
break;
|
||||
case "Warning":
|
||||
playSound(localStorage["warning"]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function displayNotification (pEvent) {
|
||||
function displayNotification(pEvent) {
|
||||
// Check if the user is okay to get some notification
|
||||
if (Notification.permission === "granted") {
|
||||
// If it's okay create a notification
|
||||
getNotification(pEvent);
|
||||
}
|
||||
|
||||
// Check if the user is okay to get some notification
|
||||
if (Notification.permission === "granted") {
|
||||
// If it's okay create a notification
|
||||
getNotification(pEvent);
|
||||
}
|
||||
|
||||
// Otherwise, we need to ask the user for permission
|
||||
// Note, Chrome does not implement the permission static property
|
||||
// So we have to check for NOT 'denied' instead of 'default'
|
||||
else if (Notification.permission !== 'denied') {
|
||||
Notification.requestPermission(function (permission) {
|
||||
// Whatever the user answers, we make sure we store the information
|
||||
if(!('permission' in Notification)) {
|
||||
Notification.permission = permission;
|
||||
}
|
||||
|
||||
// If the user is okay, let's create a notification
|
||||
if (permission === "granted") getNotification(pEvent);
|
||||
});
|
||||
}
|
||||
// Otherwise, we need to ask the user for permission
|
||||
// Note, Chrome does not implement the permission static property
|
||||
// So we have to check for NOT 'denied' instead of 'default'
|
||||
else if (Notification.permission !== "denied") {
|
||||
Notification.requestPermission(function(permission) {
|
||||
// Whatever the user answers, we make sure we store the information
|
||||
if (!("permission" in Notification)) {
|
||||
Notification.permission = permission;
|
||||
}
|
||||
|
||||
// If the user is okay, let's create a notification
|
||||
if (permission === "granted") getNotification(pEvent);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getNotification(pEvent){
|
||||
|
||||
// Build the event text
|
||||
var even = pEvent['type'];
|
||||
if (pEvent['source'] != '') even += " : " + pEvent['source'];
|
||||
even += ". Event occured at " + pEvent['date'];
|
||||
if(pEvent['module'] != 0) even += " in the module with Id "+ pEvent['module'];
|
||||
even += ".";
|
||||
function getNotification(pEvent) {
|
||||
// Build the event text
|
||||
var even = pEvent["type"];
|
||||
if (pEvent["source"] != "") even += " : " + pEvent["source"];
|
||||
even += ". Event occured at " + pEvent["date"];
|
||||
if (pEvent["module"] != 0)
|
||||
even += " in the module with Id " + pEvent["module"];
|
||||
even += ".";
|
||||
|
||||
var url = (pEvent['agent'] == 0)
|
||||
? localStorage["ip_address"]+"/index.php?sec=eventos&sec2=operation/events/events"
|
||||
: localStorage["ip_address"]+"/index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=" + pEvent['agent'];
|
||||
var url =
|
||||
pEvent["agent"] == 0
|
||||
? localStorage["ip_address"] +
|
||||
"/index.php?sec=eventos&sec2=operation/events/events"
|
||||
: localStorage["ip_address"] +
|
||||
"/index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=" +
|
||||
pEvent["agent"];
|
||||
|
||||
var notification = new Notification(
|
||||
pEvent['title'],
|
||||
{
|
||||
body: even,
|
||||
icon: "images/icon.png"
|
||||
}
|
||||
);
|
||||
var notification = new Notification(pEvent["title"], {
|
||||
body: even,
|
||||
icon: "images/icon.png"
|
||||
});
|
||||
|
||||
// Add the link
|
||||
notification.onclick = function (event) {
|
||||
event.preventDefault();
|
||||
window.open(url, '_blank');
|
||||
}
|
||||
// Add the link
|
||||
notification.onclick = function(event) {
|
||||
event.preventDefault();
|
||||
window.open(url, "_blank");
|
||||
};
|
||||
|
||||
// Close notification after 10 secs
|
||||
setTimeout(function() {notification.close()}, 10000);
|
||||
// Close notification after 10 secs
|
||||
setTimeout(function() {
|
||||
notification.close();
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
function resetInterval () {
|
||||
if (refreshTimer) clearInterval(refreshTimer);
|
||||
refreshTimer = setInterval(main, localStorage["refresh"]*1000);
|
||||
function resetInterval() {
|
||||
if (refreshTimer) clearInterval(refreshTimer);
|
||||
refreshTimer = setInterval(main, localStorage["refresh"] * 1000);
|
||||
}
|
||||
|
||||
function initilise(){
|
||||
function initilise() {
|
||||
if (isFetching == null) isFetching = false;
|
||||
if (localStorage["ip_address"] == undefined) {
|
||||
localStorage["ip_address"] = "http://firefly.artica.es/pandora_demo";
|
||||
}
|
||||
|
||||
if (isFetching == null) isFetching = false;
|
||||
if(localStorage["ip_address"]==undefined){
|
||||
localStorage["ip_address"]="http://firefly.artica.es/pandora_demo";
|
||||
}
|
||||
|
||||
if(localStorage["api_pass"]==undefined){
|
||||
localStorage["api_pass"]="doreik0";
|
||||
}
|
||||
|
||||
if(localStorage["user_name"]==undefined){
|
||||
localStorage["user_name"]="demo";
|
||||
}
|
||||
|
||||
if(localStorage["pass"]==undefined){
|
||||
localStorage["pass"]="demo";
|
||||
}
|
||||
if(localStorage["critical"]==null){
|
||||
localStorage["critical"]="11";
|
||||
}
|
||||
if(localStorage["informational"]==null){
|
||||
localStorage["informational"]="1";
|
||||
}
|
||||
if(localStorage["maintenance"]==null){
|
||||
localStorage["maintenance"]="10";
|
||||
}
|
||||
if(localStorage["normal"]==null){
|
||||
localStorage["normal"]="6";
|
||||
}
|
||||
if(localStorage["warning"]==null){
|
||||
localStorage["warning"]="2";
|
||||
}
|
||||
if(localStorage["events"]==null){
|
||||
localStorage["events"]=20;
|
||||
}
|
||||
if(localStorage["refresh"]==null){
|
||||
localStorage["refresh"]="10";
|
||||
}
|
||||
if(localStorage["ip_address"]==null){
|
||||
localStorage["ip_address"]="http://firefly.artica.es/pandora_demo";
|
||||
}
|
||||
|
||||
if(localStorage["api_pass"]==null){
|
||||
localStorage["api_pass"]="doreik0";
|
||||
}
|
||||
|
||||
if(localStorage["user_name"]==null){
|
||||
localStorage["user_name"]="demo";
|
||||
}
|
||||
|
||||
if(localStorage["pass"]==null){
|
||||
localStorage["pass"]="demo";
|
||||
}
|
||||
if(localStorage["sound_alert"]==null){
|
||||
localStorage["sound_alert"]="on";
|
||||
}
|
||||
if(localStorage["changed"]==null){
|
||||
localStorage["changed"]="false";
|
||||
}
|
||||
if(localStorage["new_events"]==null){
|
||||
localStorage["new_events"]=parseInt(localStorage["events"]);
|
||||
}
|
||||
if(localStorage["error"]==null) {
|
||||
localStorage["error"] = true;
|
||||
}
|
||||
if (localStorage["api_pass"] == undefined) {
|
||||
localStorage["api_pass"] = "doreik0";
|
||||
}
|
||||
|
||||
if (localStorage["user_name"] == undefined) {
|
||||
localStorage["user_name"] = "demo";
|
||||
}
|
||||
|
||||
if (localStorage["pass"] == undefined) {
|
||||
localStorage["pass"] = "demo";
|
||||
}
|
||||
if (localStorage["critical"] == null) {
|
||||
localStorage["critical"] = "11";
|
||||
}
|
||||
if (localStorage["informational"] == null) {
|
||||
localStorage["informational"] = "1";
|
||||
}
|
||||
if (localStorage["maintenance"] == null) {
|
||||
localStorage["maintenance"] = "10";
|
||||
}
|
||||
if (localStorage["normal"] == null) {
|
||||
localStorage["normal"] = "6";
|
||||
}
|
||||
if (localStorage["warning"] == null) {
|
||||
localStorage["warning"] = "2";
|
||||
}
|
||||
if (localStorage["events"] == null) {
|
||||
localStorage["events"] = 20;
|
||||
}
|
||||
if (localStorage["refresh"] == null) {
|
||||
localStorage["refresh"] = "10";
|
||||
}
|
||||
if (localStorage["ip_address"] == null) {
|
||||
localStorage["ip_address"] = "http://firefly.artica.es/pandora_demo";
|
||||
}
|
||||
|
||||
if (localStorage["api_pass"] == null) {
|
||||
localStorage["api_pass"] = "doreik0";
|
||||
}
|
||||
|
||||
if (localStorage["user_name"] == null) {
|
||||
localStorage["user_name"] = "demo";
|
||||
}
|
||||
|
||||
if (localStorage["pass"] == null) {
|
||||
localStorage["pass"] = "demo";
|
||||
}
|
||||
if (localStorage["sound_alert"] == null) {
|
||||
localStorage["sound_alert"] = "on";
|
||||
}
|
||||
if (localStorage["changed"] == null) {
|
||||
localStorage["changed"] = "false";
|
||||
}
|
||||
if (localStorage["new_events"] == null) {
|
||||
localStorage["new_events"] = parseInt(localStorage["events"]);
|
||||
}
|
||||
if (localStorage["error"] == null) {
|
||||
localStorage["error"] = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,10 @@ $(document).ready(function() {
|
|||
bg = chrome.extension.getBackgroundPage();
|
||||
|
||||
// Display the information
|
||||
if (bg.fetchEvents().length == 0) {
|
||||
var events = bg.fetchEvents();
|
||||
if (!events) {
|
||||
showError("Failed to retrieve events, please retry");
|
||||
} else if (events.length == 0) {
|
||||
showError("No events");
|
||||
} else {
|
||||
showEvents();
|
||||
|
|
|
@ -1,245 +1,262 @@
|
|||
if ("undefined" == typeof(PandoraChrome)) {
|
||||
var PandoraChrome = {};
|
||||
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("pandora.");
|
||||
/* globals Components, req */
|
||||
if ("undefined" == typeof PandoraChrome) {
|
||||
var PandoraChrome = {};
|
||||
var prefManager = Components.classes["@mozilla.org/preferences-service;1"]
|
||||
.getService(Components.interfaces.nsIPrefService)
|
||||
.getBranch("pandora.");
|
||||
|
||||
var timer = null;
|
||||
var allEvents=new Array();
|
||||
var newEvents=new Array();
|
||||
var oldEvents=new Array();
|
||||
var api_div_numbers=21;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
PandoraChrome.fn = function(){
|
||||
return {
|
||||
displaySideBar: function(){
|
||||
PandoraChrome.fn.hideNotification();
|
||||
PandoraChrome.fn.hideBadge();
|
||||
toggleSidebar('viewPandoraSidebar');
|
||||
},
|
||||
displayDialog : function(){
|
||||
window.openDialog("chrome://pandorasidebar/content/options.xul", "","chrome, centerscreen, dialog, modal, resizable=no", null).focus();
|
||||
},
|
||||
handleClick: function(e){
|
||||
if(e.button == 0){
|
||||
toggleSidebar('viewPandoraSidebar');
|
||||
PandoraChrome.fn.setIcon('icon16');
|
||||
}
|
||||
},
|
||||
|
||||
Onloading: function () {
|
||||
if(prefManager.getBoolPref("firstLoad")){
|
||||
prefManager.setIntPref("new_events",prefManager.getIntPref("events"));
|
||||
prefManager.setBoolPref("firstLoad",false);
|
||||
}
|
||||
var timer = null;
|
||||
var allEvents = new Array();
|
||||
var newEvents = new Array();
|
||||
var oldEvents = new Array();
|
||||
var api_div_numbers = 21;
|
||||
}
|
||||
|
||||
if(timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer =setTimeout(PandoraChrome.fn.main , 100 );
|
||||
},
|
||||
PandoraChrome.fn = (function() {
|
||||
return {
|
||||
displaySideBar: function() {
|
||||
PandoraChrome.fn.hideNotification();
|
||||
PandoraChrome.fn.hideBadge();
|
||||
toggleSidebar("viewPandoraSidebar");
|
||||
},
|
||||
displayDialog: function() {
|
||||
window
|
||||
.openDialog(
|
||||
"chrome://pandorasidebar/content/options.xul",
|
||||
"",
|
||||
"chrome, centerscreen, dialog, modal, resizable=no",
|
||||
null
|
||||
)
|
||||
.focus();
|
||||
},
|
||||
handleClick: function(e) {
|
||||
if (e.button == 0) {
|
||||
toggleSidebar("viewPandoraSidebar");
|
||||
PandoraChrome.fn.setIcon("icon16");
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Onloading: function() {
|
||||
if (prefManager.getBoolPref("firstLoad")) {
|
||||
prefManager.setIntPref("new_events", prefManager.getIntPref("events"));
|
||||
prefManager.setBoolPref("firstLoad", false);
|
||||
}
|
||||
|
||||
main: function() {
|
||||
// alert('test_main');
|
||||
var url=prefManager.getCharPref("ip_address")+'/include/api.php?op=get&op2=events&return_type=csv&apipass='+prefManager.getCharPref("api_pass")+'&user='+prefManager.getCharPref("user_name")+'&pass='+prefManager.getCharPref("pass");
|
||||
var feedUrl = url;
|
||||
prefManager.setBoolPref("data_check", true);
|
||||
req = new XMLHttpRequest();
|
||||
req.onload = PandoraChrome.fn.handleResponse;
|
||||
req.onerror = PandoraChrome.fn.handleError;
|
||||
req.open("GET", feedUrl, true);
|
||||
req.send(null);
|
||||
},
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(PandoraChrome.fn.main, 100);
|
||||
},
|
||||
|
||||
handleError: function() {
|
||||
//alert("error");
|
||||
prefManager.setCharPref("data",null);
|
||||
prefManager.setBoolPref("data_check", false);
|
||||
if(timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer =setTimeout(PandoraChrome.fn.main , 1000);
|
||||
},
|
||||
main: function() {
|
||||
// alert('test_main');
|
||||
var url =
|
||||
prefManager.getCharPref("ip_address") +
|
||||
"/include/api.php?op=get&op2=events&return_type=json";
|
||||
var feedUrl = url;
|
||||
var data = new FormData();
|
||||
|
||||
handleResponse: function () {
|
||||
var doc = req.responseText;
|
||||
if (doc=="auth error") {
|
||||
prefManager.setCharPref("data",null);
|
||||
prefManager.setBoolPref("data_check", false);
|
||||
if(timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer =setTimeout(PandoraChrome.fn.main , 1000);
|
||||
data.append("apipass", prefManager.getCharPref("api_pass"));
|
||||
data.append("user", prefManager.getCharPref("user_name"));
|
||||
data.append("pass", prefManager.getCharPref("pass"));
|
||||
|
||||
}
|
||||
else{
|
||||
var n=doc.search("404 Not Found");
|
||||
if(n>0){
|
||||
prefManager.setBoolPref("data_check", true);
|
||||
req = new XMLHttpRequest();
|
||||
req.onload = PandoraChrome.fn.handleResponse;
|
||||
req.onerror = PandoraChrome.fn.handleError;
|
||||
req.open("POST", feedUrl, true);
|
||||
req.send(data);
|
||||
},
|
||||
|
||||
prefManager.setCharPref("data",null);
|
||||
prefManager.setBoolPref("data_check", false);
|
||||
if(timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer =setTimeout(PandoraChrome.fn.main , 1000);
|
||||
}
|
||||
|
||||
else{
|
||||
prefManager.setBoolPref("data_check", true);
|
||||
handleError: function() {
|
||||
//alert("error");
|
||||
prefManager.setCharPref("data", null);
|
||||
prefManager.setBoolPref("data_check", false);
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(PandoraChrome.fn.main, 1000);
|
||||
},
|
||||
|
||||
prefManager.setCharPref("data",doc);
|
||||
PandoraChrome.fn.getEvents(doc);
|
||||
}
|
||||
}
|
||||
},
|
||||
handleResponse: function() {
|
||||
var doc = req.responseText;
|
||||
if (doc == "auth error") {
|
||||
prefManager.setCharPref("data", null);
|
||||
prefManager.setBoolPref("data_check", false);
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(PandoraChrome.fn.main, 1000);
|
||||
} else {
|
||||
var n = doc.search("404 Not Found");
|
||||
if (n > 0) {
|
||||
prefManager.setCharPref("data", null);
|
||||
prefManager.setBoolPref("data_check", false);
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(PandoraChrome.fn.main, 1000);
|
||||
} else {
|
||||
prefManager.setBoolPref("data_check", true);
|
||||
|
||||
getEvents: function (reply){
|
||||
if(reply.length>100){
|
||||
all_event_array=reply.split("\n");
|
||||
allEvents=PandoraChrome.fn.divideArray(all_event_array);
|
||||
if(oldEvents.length==0){
|
||||
oldEvents=allEvents;
|
||||
}
|
||||
prefManager.setCharPref("data", doc);
|
||||
PandoraChrome.fn.getEvents(doc);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
newEvents=PandoraChrome.fn.fetchNewEvents(allEvents,oldEvents);
|
||||
if(newEvents.length!=0){
|
||||
for(var k=0;k<newEvents.length;k++){
|
||||
var temp=prefManager.getIntPref("new_events")+1;
|
||||
prefManager.setIntPref("new_events",temp);
|
||||
PandoraChrome.fn.showNotification(k);
|
||||
PandoraChrome.fn.showBadge(prefManager.getIntPref("new_events"));
|
||||
|
||||
}
|
||||
}
|
||||
oldEvents=allEvents;
|
||||
if(prefManager.getIntPref("new_events")>0){
|
||||
PandoraChrome.fn.showBadge(prefManager.getIntPref("new_events"));
|
||||
}
|
||||
else{
|
||||
PandoraChrome.fn.hideBadge();
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer =setTimeout(PandoraChrome.fn.main , prefManager.getIntPref("refresh")*1000 );
|
||||
}
|
||||
},
|
||||
|
||||
showNotification: function(eventId){
|
||||
//alert("notify"+eventId);
|
||||
if(prefManager.getBoolPref("sound_alert")){
|
||||
if(newEvents[eventId][19]=="Critical"){
|
||||
Sounds.playSound(prefManager.getIntPref("critical"));
|
||||
}
|
||||
if(newEvents[eventId][19]=="Informational"){
|
||||
Sounds.playSound(prefManager.getIntPref("informational"));
|
||||
}
|
||||
if(newEvents[eventId][19]=="Maintenance"){
|
||||
Sounds.playSound(prefManager.getIntPref("maintenance"));
|
||||
}
|
||||
if(newEvents[eventId][19]=="Normal"){
|
||||
Sounds.playSound(prefManager.getIntPref("normal"));
|
||||
}
|
||||
if(newEvents[eventId][19]=="Warning"){
|
||||
Sounds.playSound(prefManager.getIntPref("warning"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var newEve = document.getElementById('newEvent');
|
||||
newEve.label="Last Event : "+newEvents[eventId][6];
|
||||
var id;
|
||||
if(newEvents[eventId][9]==0){
|
||||
id=".";
|
||||
}
|
||||
else {
|
||||
id= " in the module with Id "+ newEvents[eventId][9] + ".";
|
||||
}
|
||||
|
||||
var event = newEvents[eventId][14]+" : "+newEvents[eventId][17]+". Event occured at "+ newEvents[eventId][5]+id;
|
||||
newEve.tooltipText=event;
|
||||
$('#newEvent').show();
|
||||
return;
|
||||
},
|
||||
|
||||
|
||||
hideNotification:function(){
|
||||
//alert("Hide Notif");
|
||||
$('#newEvent').hide();
|
||||
},
|
||||
|
||||
|
||||
showBadge: function (txt) {
|
||||
//alert(txt);
|
||||
var updateCount = document.getElementById('temp');
|
||||
updateCount.setAttribute("style","cursor:pointer; font-size:11px; color:#123863; font-weight:bold; display:none;") ;
|
||||
updateCount.label=txt;
|
||||
$('#temp').show();
|
||||
},
|
||||
|
||||
hideBadge: function () {
|
||||
var updateCount = document.getElementById('temp');
|
||||
//alert("hide B");
|
||||
$('#temp').hide();
|
||||
},
|
||||
|
||||
divideArray: function (e_array){
|
||||
var Events=new Array();
|
||||
for(var i=0;i<e_array.length;i++){
|
||||
var event=e_array[i].split(";");
|
||||
Events.push(event);
|
||||
}
|
||||
return Events;
|
||||
},
|
||||
|
||||
|
||||
fetchNewEvents: function (A,B){
|
||||
var arrDiff = new Array();
|
||||
// alert(A.length);
|
||||
//alert(B.length);
|
||||
for(var i = 0; i < A.length; i++) {
|
||||
var id = false;
|
||||
for(var j = 0; j < B.length; j++) {
|
||||
if(A[i][0] == B[j][0]) {
|
||||
id = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!id) {
|
||||
arrDiff.push(A[i]);
|
||||
}
|
||||
}
|
||||
return arrDiff;
|
||||
},
|
||||
|
||||
|
||||
getNotification:function (eventId){
|
||||
var title=newEvents[eventId][6];
|
||||
var id;
|
||||
if(newEvents[eventId][9]==0){
|
||||
id=".";
|
||||
}
|
||||
else {
|
||||
id= " in the module with Id "+ newEvents[eventId][9] + ".";
|
||||
}
|
||||
|
||||
var event = newEvents[eventId][14]+" : "+newEvents[eventId][17]+". Event occured at "+ newEvents[eventId][5]+id;
|
||||
//var event=newEvents[eventId][14]+' '+newEvents[eventId][17]+' Event occured at:'+ newEvents[eventId][5] +'in the module with Id '+ newEvents[eventId][9];
|
||||
return '<a>' + title + '</a> <br/> <span style="font-size:80%">' + event + '</span>';
|
||||
|
||||
getEvents: function(reply) {
|
||||
if (reply.length > 100) {
|
||||
all_event_array = reply.split("\n");
|
||||
allEvents = PandoraChrome.fn.divideArray(all_event_array);
|
||||
if (oldEvents.length == 0) {
|
||||
oldEvents = allEvents;
|
||||
}
|
||||
|
||||
};
|
||||
}();
|
||||
newEvents = PandoraChrome.fn.fetchNewEvents(allEvents, oldEvents);
|
||||
if (newEvents.length != 0) {
|
||||
for (var k = 0; k < newEvents.length; k++) {
|
||||
var temp = prefManager.getIntPref("new_events") + 1;
|
||||
prefManager.setIntPref("new_events", temp);
|
||||
PandoraChrome.fn.showNotification(k);
|
||||
PandoraChrome.fn.showBadge(prefManager.getIntPref("new_events"));
|
||||
}
|
||||
}
|
||||
oldEvents = allEvents;
|
||||
if (prefManager.getIntPref("new_events") > 0) {
|
||||
PandoraChrome.fn.showBadge(prefManager.getIntPref("new_events"));
|
||||
} else {
|
||||
PandoraChrome.fn.hideBadge();
|
||||
}
|
||||
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(
|
||||
PandoraChrome.fn.main,
|
||||
prefManager.getIntPref("refresh") * 1000
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
showNotification: function(eventId) {
|
||||
//alert("notify"+eventId);
|
||||
if (prefManager.getBoolPref("sound_alert")) {
|
||||
if (newEvents[eventId][19] == "Critical") {
|
||||
Sounds.playSound(prefManager.getIntPref("critical"));
|
||||
}
|
||||
if (newEvents[eventId][19] == "Informational") {
|
||||
Sounds.playSound(prefManager.getIntPref("informational"));
|
||||
}
|
||||
if (newEvents[eventId][19] == "Maintenance") {
|
||||
Sounds.playSound(prefManager.getIntPref("maintenance"));
|
||||
}
|
||||
if (newEvents[eventId][19] == "Normal") {
|
||||
Sounds.playSound(prefManager.getIntPref("normal"));
|
||||
}
|
||||
if (newEvents[eventId][19] == "Warning") {
|
||||
Sounds.playSound(prefManager.getIntPref("warning"));
|
||||
}
|
||||
}
|
||||
|
||||
var newEve = document.getElementById("newEvent");
|
||||
newEve.label = "Last Event : " + newEvents[eventId][6];
|
||||
var id;
|
||||
if (newEvents[eventId][9] == 0) {
|
||||
id = ".";
|
||||
} else {
|
||||
id = " in the module with Id " + newEvents[eventId][9] + ".";
|
||||
}
|
||||
|
||||
var event =
|
||||
newEvents[eventId][14] +
|
||||
" : " +
|
||||
newEvents[eventId][17] +
|
||||
". Event occured at " +
|
||||
newEvents[eventId][5] +
|
||||
id;
|
||||
newEve.tooltipText = event;
|
||||
$("#newEvent").show();
|
||||
return;
|
||||
},
|
||||
|
||||
hideNotification: function() {
|
||||
//alert("Hide Notif");
|
||||
$("#newEvent").hide();
|
||||
},
|
||||
|
||||
showBadge: function(txt) {
|
||||
//alert(txt);
|
||||
var updateCount = document.getElementById("temp");
|
||||
updateCount.setAttribute(
|
||||
"style",
|
||||
"cursor:pointer; font-size:11px; color:#123863; font-weight:bold; display:none;"
|
||||
);
|
||||
updateCount.label = txt;
|
||||
$("#temp").show();
|
||||
},
|
||||
|
||||
hideBadge: function() {
|
||||
var updateCount = document.getElementById("temp");
|
||||
//alert("hide B");
|
||||
$("#temp").hide();
|
||||
},
|
||||
|
||||
divideArray: function(e_array) {
|
||||
var Events = new Array();
|
||||
for (var i = 0; i < e_array.length; i++) {
|
||||
var event = e_array[i].split(";");
|
||||
Events.push(event);
|
||||
}
|
||||
return Events;
|
||||
},
|
||||
|
||||
fetchNewEvents: function(A, B) {
|
||||
var arrDiff = new Array();
|
||||
// alert(A.length);
|
||||
//alert(B.length);
|
||||
for (var i = 0; i < A.length; i++) {
|
||||
var id = false;
|
||||
for (var j = 0; j < B.length; j++) {
|
||||
if (A[i][0] == B[j][0]) {
|
||||
id = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!id) {
|
||||
arrDiff.push(A[i]);
|
||||
}
|
||||
}
|
||||
return arrDiff;
|
||||
},
|
||||
|
||||
getNotification: function(eventId) {
|
||||
var title = newEvents[eventId][6];
|
||||
var id;
|
||||
if (newEvents[eventId][9] == 0) {
|
||||
id = ".";
|
||||
} else {
|
||||
id = " in the module with Id " + newEvents[eventId][9] + ".";
|
||||
}
|
||||
|
||||
var event =
|
||||
newEvents[eventId][14] +
|
||||
" : " +
|
||||
newEvents[eventId][17] +
|
||||
". Event occured at " +
|
||||
newEvents[eventId][5] +
|
||||
id;
|
||||
//var event=newEvents[eventId][14]+' '+newEvents[eventId][17]+' Event occured at:'+ newEvents[eventId][5] +'in the module with Id '+ newEvents[eventId][9];
|
||||
return (
|
||||
"<a>" +
|
||||
title +
|
||||
'</a> <br/> <span style="font-size:80%">' +
|
||||
event +
|
||||
"</span>"
|
||||
);
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
/* Add Event Listener */
|
||||
window.addEventListener("load", PandoraChrome.fn.Onloading(), false);
|
||||
|
|
Loading…
Reference in New Issue