2021-10-23 03:26:01 +02:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< title > ntfy.sh< / title >
< style >
< / style >
< / head >
< body >
< h1 > ntfy.sh< / h1 >
2021-10-23 19:21:33 +02:00
< p >
ntfy.sh is a super simple pub-sub notification service. It allows you to send desktop and (soon) phone notifications
via scripts, without signup or cost. It's entirely free and open source.
< / p >
2021-10-23 03:26:01 +02:00
2021-10-23 19:21:33 +02:00
< p >
< b > Usage:< / b > You can subscribe to a topic either in this web UI, or in your own app by subscribing to an SSE/EventSource
or JSON feed. Once subscribed, you can publish messages via PUT or POST.
< / p >
2021-10-23 03:26:01 +02:00
< div id = "error" > < / div >
2021-10-23 19:21:33 +02:00
< form id = "subscribeForm" >
< input type = "text" id = "topicField" size = "64" autofocus / >
< input type = "submit" id = "subscribeButton" value = "Subscribe topic" / >
< / form >
2021-10-23 03:26:01 +02:00
2021-10-23 19:21:33 +02:00
Topics:
< ul id = "topicsList" >
< / ul >
2021-10-23 03:26:01 +02:00
2021-10-23 19:21:33 +02:00
< script type = "text/javascript" >
let topics = {};
2021-10-23 03:26:01 +02:00
2021-10-23 19:21:33 +02:00
const topicField = document.getElementById("topicField");
const topicsList = document.getElementById("topicsList");
const subscribeButton = document.getElementById("subscribeButton");
const subscribeForm = document.getElementById("subscribeForm");
const errorField = document.getElementById("error");
const subscribe = function (topic) {
if (Notification.permission !== "granted") {
Notification.requestPermission().then(function (permission) {
if (permission === "granted") {
subscribeInternal(topic);
}
});
} else {
subscribeInternal(topic);
2021-10-23 03:26:01 +02:00
}
2021-10-23 19:21:33 +02:00
};
2021-10-23 03:26:01 +02:00
2021-10-23 19:21:33 +02:00
const subscribeInternal = function (topic) {
let eventSource = new EventSource(`${topic}/sse`);
eventSource.onerror = function (e) {
console.log(e);
errorField.innerHTML = "Error " + e;
};
eventSource.onmessage = function (e) {
const event = JSON.parse(e.data);
new Notification(event.message);
2021-10-23 03:26:01 +02:00
};
2021-10-23 19:21:33 +02:00
topics[topic] = eventSource;
let topicEntry = document.createElement('li');
topicEntry.id = `topic-${topic}`;
topicEntry.innerHTML = `${topic} < button onclick = "unsubscribe('${topic}')" > Unsubscribe< / button > `;
topicsList.appendChild(topicEntry);
};
2021-10-23 03:26:01 +02:00
2021-10-23 19:21:33 +02:00
const unsubscribe = function(topic) {
topics[topic].close();
document.getElementById(`topic-${topic}`).remove();
};
subscribeForm.onsubmit = function () {
alert("hi")
if (!topicField.value) {
return false;
2021-10-23 03:26:01 +02:00
}
2021-10-23 19:21:33 +02:00
subscribe(topicField.value);
return false;
2021-10-23 03:26:01 +02:00
};
2021-10-23 19:21:33 +02:00
if (!window["Notification"] || !window["EventSource"]) {
errorField.innerHTML = "Your browser is not compatible to use the web-based desktop notifications.";
topicField.disabled = true;
subscribeButton.disabled = true;
} else if (Notification.permission === "denied") {
errorField.innerHTML = "You have blocked desktop notifications for this website. Please unblock them and refresh to use the web-based desktop notifications.";
topicField.disabled = true;
subscribeButton.disabled = true;
}
2021-10-23 03:26:01 +02:00
< / script >
< / body >
< / html >