From 1ee0c09c5742557e037df5421ca62abddb90ae22 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 19 May 2021 14:33:38 +0200 Subject: Merging upstream version 1.31.0. Signed-off-by: Daniel Baumann --- web/gui/dashboard/refresh-badges.js | 98 +++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 web/gui/dashboard/refresh-badges.js (limited to 'web/gui/dashboard/refresh-badges.js') diff --git a/web/gui/dashboard/refresh-badges.js b/web/gui/dashboard/refresh-badges.js new file mode 100644 index 000000000..00dd4dadf --- /dev/null +++ b/web/gui/dashboard/refresh-badges.js @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// ---------------------------------------------------------------------------- +// This script periodically updates all the netdata badges you have added to a +// page as images. You don't need this script if you add the badges with +// - embedded badges auto-refresh by themselves. +// +// You can set the following variables before loading this script: + +/*global netdata_update_every *//* number, the time in seconds to update the badges + * (default: 15) */ +/*global netdata_live_callback *//* function, callback to be called on each iteration while updating the badges + * (default: null) */ +/*global netdata_paused_callback *//* function, callback to be called when the update pauses + * (default: null) */ + +/* +// EXAMPLE HTML PAGE: + + + + + + + + +
Please wait... loading...
+ + + + +*/ + +if(typeof netdata_update_every === 'undefined') + netdata_update_every = 15; + +var netdata_was_live = false; +var netdata_is_live = true; +var netdata_loops = 0; + +function update_netdata_badges() { + netdata_loops++; + netdata_is_live = false; + + var updated = 0; + var focus = document.hasFocus(); + + if(focus && netdata_loops >= netdata_update_every) { + var len = document.images.length; + while(len--) { + var url = document.images[len].src; + if(url.match(/\api\/v1\/badge\.svg/)) { + if(url.match(/\?/)) + url = url.replace(/&cacheBuster=\d*/, "") + "&cacheBuster=" + new Date().getTime().toString(); + else + url = url.replace(/\?cacheBuster=\d*/, "") + "?cacheBuster=" + new Date().getTime().toString(); + + document.images[len].src = url; + updated++; + } + } + netdata_loops = 0; + } + + if(focus || updated) + netdata_is_live = true; + + try { + if(netdata_is_live && typeof netdata_live_callback === 'function') + netdata_live_callback(netdata_update_every - netdata_loops, updated); + else if(netdata_was_live !== netdata_is_live && typeof netdata_paused_callback === 'function') + netdata_paused_callback(); + } + catch(e) { + console.log(e); + } + netdata_was_live = netdata_is_live; + + setTimeout(update_netdata_badges, 1000); +} +setTimeout(update_netdata_badges, 1000); -- cgit v1.2.3