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:
@@ -2,24 +2,53 @@ import { loadOptions } from './utils.js';
|
|||||||
|
|
||||||
let options = {};
|
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) {
|
function ping(server) {
|
||||||
console.log('pinging ' + server);
|
console.log('pinging ' + server);
|
||||||
fetch(server)
|
fetch(server)
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
browser.browserAction.setIcon({
|
up();
|
||||||
path: 'icons/available-32.png'
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
browser.browserAction.setIcon({
|
down(response.statusText);
|
||||||
path: 'icons/offline-32.png'
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
browser.browserAction.setIcon({
|
down(error.message);
|
||||||
path: 'icons/offline-32.png'
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +56,7 @@ function reloadConfig() {
|
|||||||
loadOptions().then((savedOptions) => {
|
loadOptions().then((savedOptions) => {
|
||||||
options.server = savedOptions.server;
|
options.server = savedOptions.server;
|
||||||
options.interval = savedOptions.interval;
|
options.interval = savedOptions.interval;
|
||||||
|
options.alert = savedOptions.alert;
|
||||||
|
|
||||||
if (options.server && options.interval >= 1) {
|
if (options.server && options.interval >= 1) {
|
||||||
ping(options.server);
|
ping(options.server);
|
||||||
|
@@ -28,6 +28,7 @@
|
|||||||
"permissions": [
|
"permissions": [
|
||||||
"storage",
|
"storage",
|
||||||
"<all_urls>",
|
"<all_urls>",
|
||||||
"webRequest"
|
"webRequest",
|
||||||
|
"notifications"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@@ -29,5 +29,5 @@ input[type="number"] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
input[type="checkbox"] {
|
input[type="checkbox"] {
|
||||||
margin-bottom: -7px;
|
margin-bottom: -4px;
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,11 @@
|
|||||||
<label for="interval">Interval in seconds: </label>
|
<label for="interval">Interval in seconds: </label>
|
||||||
<input type="number" id="interval" name="interval">
|
<input type="number" id="interval" name="interval">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<input type="checkbox" id="alert" checked="checked" name="alert">
|
||||||
|
<label for="alert">Alert if server goes down</label>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<script type="module" src="ping.js"></script>
|
<script type="module" src="ping.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
@@ -3,7 +3,8 @@ import { loadOptions, saveOptions } from '../utils.js';
|
|||||||
function getParams() {
|
function getParams() {
|
||||||
return {
|
return {
|
||||||
server: document.getElementById('server').value,
|
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) => {
|
loadOptions().then((options) => {
|
||||||
document.getElementById('server').value = options.server;
|
document.getElementById('server').value = options.server;
|
||||||
document.getElementById('interval').value = options.interval;
|
document.getElementById('interval').value = options.interval;
|
||||||
|
document.getElementById('alert').checked = options.alert;
|
||||||
|
|
||||||
browser.runtime.sendMessage('configChanged');
|
browser.runtime.sendMessage('configChanged');
|
||||||
});
|
});
|
||||||
|
@@ -1,7 +1,8 @@
|
|||||||
function loadOptions() {
|
function loadOptions() {
|
||||||
return browser.storage.local.get({
|
return browser.storage.local.get({
|
||||||
server: '',
|
server: '',
|
||||||
interval: 60
|
interval: 60,
|
||||||
|
alert: false
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user