1
0
mirror of https://github.com/S2-/ping.git synced 2025-08-02 12:20:05 +02:00

add notification function

This commit is contained in:
s2
2019-03-18 16:44:36 +01:00
parent 0a33001bdd
commit 1902b5b2d9
6 changed files with 52 additions and 13 deletions

View File

@@ -2,24 +2,53 @@ import { loadOptions } from './utils.js';
let options = {};
function down(error) {
if (options.alert && !options.alertShown) {
browser.notifications.create('server-down-notification', {
'type': 'basic',
'iconUrl': browser.extension.getURL('icons/offline-48.png'),
'title': 'Server went down',
'message': error
});
options.alertShown = true;
}
browser.browserAction.setIcon({
path: 'icons/offline-32.png'
});
if (error) {
browser.browserAction.setTitle({
title: error
});
}
}
function up() {
browser.browserAction.setIcon({
path: 'icons/available-32.png'
});
browser.browserAction.setTitle({
title: 'pinging ' + options.server
});
options.alertShown = false;
browser.notifications.clear('server-down-notification');
}
function ping(server) {
console.log('pinging ' + server);
fetch(server)
.then(function(response) {
if (response.status === 200) {
browser.browserAction.setIcon({
path: 'icons/available-32.png'
});
up();
} else {
browser.browserAction.setIcon({
path: 'icons/offline-32.png'
});
down(response.statusText);
}
})
.catch((error) => {
browser.browserAction.setIcon({
path: 'icons/offline-32.png'
});
down(error.message);
});
}
@@ -27,6 +56,7 @@ function reloadConfig() {
loadOptions().then((savedOptions) => {
options.server = savedOptions.server;
options.interval = savedOptions.interval;
options.alert = savedOptions.alert;
if (options.server && options.interval >= 1) {
ping(options.server);

View File

@@ -28,6 +28,7 @@
"permissions": [
"storage",
"<all_urls>",
"webRequest"
"webRequest",
"notifications"
]
}

View File

@@ -29,5 +29,5 @@ input[type="number"] {
}
input[type="checkbox"] {
margin-bottom: -7px;
margin-bottom: -4px;
}

View File

@@ -20,6 +20,11 @@
<label for="interval">Interval in seconds: </label>
<input type="number" id="interval" name="interval">
</div>
<div>
<input type="checkbox" id="alert" checked="checked" name="alert">
<label for="alert">Alert if server goes down</label>
</div>
</form>
<script type="module" src="ping.js"></script>
</body>

View File

@@ -3,7 +3,8 @@ import { loadOptions, saveOptions } from '../utils.js';
function getParams() {
return {
server: document.getElementById('server').value,
interval: parseInt(document.getElementById('interval').value)
interval: parseInt(document.getElementById('interval').value),
alert: document.getElementById('alert').checked
};
};
@@ -19,6 +20,7 @@ document.addEventListener('DOMContentLoaded', function() {
loadOptions().then((options) => {
document.getElementById('server').value = options.server;
document.getElementById('interval').value = options.interval;
document.getElementById('alert').checked = options.alert;
browser.runtime.sendMessage('configChanged');
});

View File

@@ -1,7 +1,8 @@
function loadOptions() {
return browser.storage.local.get({
server: '',
interval: 60
interval: 60,
alert: false
});
};