From 5ea9edf13249b6b2277856257cd0f57ed7e925c6 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Mon, 27 Nov 2017 16:29:32 +0100 Subject: [PATCH] Fixed several issues on chrome extension --- extras/chrome_extension/js/background.js | 401 +++++++++++++---------- extras/chrome_extension/js/popup.js | 206 +++++------- extras/chrome_extension/manifest.json | 8 +- extras/chrome_extension/popup.html | 20 +- 4 files changed, 324 insertions(+), 311 deletions(-) diff --git a/extras/chrome_extension/js/background.js b/extras/chrome_extension/js/background.js index 3de478b61b..15360f1c18 100644 --- a/extras/chrome_extension/js/background.js +++ b/extras/chrome_extension/js/background.js @@ -1,29 +1,27 @@ -var timer = null; -var allEvents=new Array(); -var newEvents=new Array(); -var oldEvents=new Array(); -var api_div_numbers=21; - +var refreshTimer = null; +var isFetching = null; +var storedEvents=new Array(); $(window).load(function() { initilise(); - if(timer) { - clearTimeout(timer); - } - timer =setTimeout(main , 100 ); + // Wait some ms to throw main function + var delay = setTimeout(main, 100); + resetInterval(); }); - - -function fetchEvents(){ - return oldEvents; +function fetchEvents() { + return storedEvents; } function main() { - var url=localStorage["ip_address"]+'/include/api.php?op=get&op2=events&return_type=csv&apipass='+localStorage["api_pass"]+'&user='+localStorage["user_name"]+'&pass='+localStorage["pass"]; - var feedUrl = url; - localStorage["data_check"]="true"; + + chrome.runtime.sendMessage({text: "FETCH_EVENTS"}); + // Do not fetch if is fetching now + if (isFetching) return; + isFetching = true; + + var feedUrl = localStorage["ip_address"]+'/include/api.php?op=get&op2=events&return_type=csv&apipass='+localStorage["api_pass"]+'&user='+localStorage["user_name"]+'&pass='+localStorage["pass"]; req = new XMLHttpRequest(); req.onload = handleResponse; req.onerror = handleError; @@ -32,96 +30,68 @@ function main() { } function handleError() { - localStorage["data_check"]="false"; - if(timer) { - clearTimeout(timer); - } - timer =setTimeout(main , 1000); + chrome.runtime.sendMessage({text: "FETCH_EVENTS_URL_ERROR"}); + isFetching = false; } function handleResponse() { var doc = req.responseText; if (doc=="auth error") { - localStorage["data_check"]="false"; - if(timer) { - clearTimeout(timer); - } - timer =setTimeout(main , 1000); - } - else{ - var n=doc.search("404 Not Found"); - if(n>0){ - localStorage["data_check"]="false"; - if(timer) { - clearTimeout(timer); - } - timer =setTimeout(main , 1000); - } - - else{ - localStorage["data_check"]="true" + 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){ - if(check()){ - all_event_array=reply.split("\n"); - allEvents=divideArray(all_event_array); - if(oldEvents.length==0){ - oldEvents=allEvents; + var fetchedEvents = parseReplyEvents(reply); + + // If there is no events requested, mark all as visited + if (storedEvents.length == 0) { + for(var k=0;k' + title + '
' + event + ''; + // 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 check(){ - if (localStorage["data_check"]=="true" && localStorage["ip_address"] != null && localStorage["api_pass"] != null &&localStorage["user_name"]!=null &&localStorage["pass"]!=null && localStorage["ip_address"] != "" && localStorage["api_pass"] != "" &&localStorage["user_name"]!="" &&localStorage["pass"]!=""){ - return true; +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']=="") + ? 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" + } + ); + + // Add the link + notification.onclick = function (event) { + event.preventDefault(); + window.open(url, '_blank'); } - else - return false; + + // Close notification after 10 secs + setTimeout(function() {notification.close()}, 10000); +} + +function resetInterval () { + if (refreshTimer) clearInterval(refreshTimer); + refreshTimer = setInterval(main, localStorage["refresh"]*1000); } function initilise(){ - 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 (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; + } } diff --git a/extras/chrome_extension/js/popup.js b/extras/chrome_extension/js/popup.js index c6213cbf4e..29bfa6b7b3 100644 --- a/extras/chrome_extension/js/popup.js +++ b/extras/chrome_extension/js/popup.js @@ -1,5 +1,3 @@ -var timer = null; -var event_array; var max_events; var bg; $(document).ready(function(){ @@ -8,66 +6,68 @@ $(document).ready(function(){ localStorage["events"]="20"; } bg=chrome.extension.getBackgroundPage(); - if(timer) { - clearTimeout(timer); - } - timer =setTimeout(mainPoP , 2000 ); -}); -function mainPoP(){ - if(bg.check()){ + // Display the information + if (bg.fetchEvents().length == 0) { + showError("Error in fetching data!! Check your internet connection"); + } else { showEvents(); } - else - { - showUrlError(); - } -} -function showUrlError(){ - var res=document.createDocumentFragment(); - var r = document.getElementById('e'); - var eve=document.createElement('div'); - eve.id="event_temp"; - eve.setAttribute("class","b"); - - var p = document.createElement('a'); - p.href="options.html"; - p.target="_blank"; - p.innerText="Configure ip address,API password, user name and password with correct values"; - eve.appendChild(p); - res.appendChild(eve); - r.parentNode.insertBefore(res,r); - $('.loader').hide(); - $('div.b').show(); - - if(timer) { - clearTimeout(timer); - } - timer =setTimeout(refresh , 1000 ); + + // Adding buttons listeners + document.getElementById("m_refresh").addEventListener("click", mrefresh); + + // Added listener to background messages + chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){ + switch (message.text) { + case "FETCH_EVENTS": + setSpinner(); + //$('div.b').hide(); + break; + case "FETCH_EVENTS_SUCCESS": + unsetSpinner(); + showEvents(); + break; + case "FETCH_EVENTS_DATA_ERROR": + unsetSpinner(); + showError("Error in fetching data!! Check your internet connection"); + break; + case "FETCH_EVENTS_URL_ERROR": + unsetSpinner(); + showError("Configure ip address,API password, user name and password with correct values"); + break; + default: + console.log("Unrecognized message: ", message.text); + break; + } + }); +}); + +function setSpinner () { + $('#refr_img_id').attr("src", "images/spinny.gif"); } -function showDataError(){ - var res=document.createDocumentFragment(); - var r = document.getElementById('e'); - var eve=document.createElement('div'); - eve.id="event_temp"; - eve.setAttribute("class","b"); - - var p = document.createElement('a'); - p.innerText="Error in fetching data!! Check your internet connection"; - eve.appendChild(p); - res.appendChild(eve); - r.parentNode.insertBefore(res,r); - $('.loader').hide(); - $('div.b').show(); - - if(timer) { - clearTimeout(timer); - } - timer =setTimeout(refresh , 1000 ); +function unsetSpinner() { + $('#refr_img_id').attr("src", "images/refresh.png"); +} + +function clearError() { + $('.error').hide(); + $('.error a').text(""); +} + +function showError(text){ + $('.error a').text(text); + $('.error').show(); } function showEvents(){ + clearError(); + var wrapper = document.getElementById('event'); + var e_refr = document.getElementById('event_temp'); + if(e_refr){ + wrapper.removeChild(e_refr); + } var allEvents=bg.fetchEvents(); var r = document.getElementById('e'); var res=document.createDocumentFragment(); @@ -77,7 +77,7 @@ function showEvents(){ var i=0; if(allEvents.length>0){ - while(i
+ + + + + + + +
-
Loading...
+
+

Error

+ +

Events

-
- -
+