ntfy/server/index.html

127 lines
4.4 KiB
HTML
Raw Normal View History

2021-10-23 04:26:01 +03:00
<!DOCTYPE html>
<html lang="en">
<head>
<title>ntfy.sh</title>
<style>
</style>
</head>
<body>
<h1>ntfy.sh</h1>
2021-10-23 20:21:33 +03:00
<p>
ntfy.sh is a super simple pub-sub notification service. It allows you to send desktop and (soon) phone notifications
2021-10-23 22:22:17 +03:00
via scripts, without signup or cost. It's entirely free and open source. You can find the source code <a href="https://github.com/binwiederhier/ntfy">on GitHub</a>.
2021-10-23 20:21:33 +03:00
</p>
2021-10-23 04:26:01 +03:00
2021-10-23 20:21:33 +03:00
<p>
2021-10-23 22:22:17 +03:00
You can subscribe to a topic either in this web UI, or in your own app by subscribing to an SSE/EventSource
2021-10-23 20:21:33 +03:00
or JSON feed. Once subscribed, you can publish messages via PUT or POST.
</p>
2021-10-23 04:26:01 +03:00
2021-10-23 22:22:17 +03:00
<p id="error"></p>
2021-10-23 04:26:01 +03:00
2021-10-23 20:21:33 +03:00
<form id="subscribeForm">
<input type="text" id="topicField" size="64" autofocus />
<input type="submit" id="subscribeButton" value="Subscribe topic" />
</form>
2021-10-23 04:26:01 +03:00
2021-10-23 22:22:17 +03:00
<p>Topics:</p>
2021-10-23 20:21:33 +03:00
<ul id="topicsList">
</ul>
2021-10-23 04:26:01 +03:00
2021-10-23 20:21:33 +03:00
<script type="text/javascript">
let topics = {};
2021-10-23 04:26:01 +03:00
2021-10-23 20:21:33 +03: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");
2021-10-23 22:22:17 +03:00
const subscribe = (topic) => {
2021-10-23 20:21:33 +03:00
if (Notification.permission !== "granted") {
2021-10-23 22:22:17 +03:00
Notification.requestPermission().then((permission) => {
2021-10-23 20:21:33 +03:00
if (permission === "granted") {
2021-10-23 22:22:17 +03:00
subscribeInternal(topic, 0);
2021-10-23 20:21:33 +03:00
}
});
} else {
2021-10-23 22:22:17 +03:00
subscribeInternal(topic, 0);
2021-10-23 04:26:01 +03:00
}
2021-10-23 20:21:33 +03:00
};
2021-10-23 04:26:01 +03:00
2021-10-23 22:22:17 +03:00
const subscribeInternal = (topic, delaySec) => {
setTimeout(() => {
// Render list entry
let topicEntry = document.getElementById(`topic-${topic}`);
if (!topicEntry) {
topicEntry = document.createElement('li');
topicEntry.id = `topic-${topic}`;
topicEntry.innerHTML = `${topic} <button onclick="unsubscribe('${topic}')">Unsubscribe</button>`;
topicsList.appendChild(topicEntry);
}
// Open event source
let eventSource = new EventSource(`${topic}/sse`);
eventSource.onopen = () => {
topicEntry.innerHTML = `${topic} <button onclick="unsubscribe('${topic}')">Unsubscribe</button>`;
delaySec = 0; // Reset on successful connection
};
eventSource.onerror = (e) => {
console.log("onerror")
const newDelaySec = (delaySec + 5 <= 30) ? delaySec + 5 : 30;
topicEntry.innerHTML = `${topic} <i>(Reconnecting in ${newDelaySec}s ...)</i> <button onclick="unsubscribe('${topic}')">Unsubscribe</button>`;
eventSource.close()
subscribeInternal(topic, newDelaySec);
};
eventSource.onmessage = (e) => {
const event = JSON.parse(e.data);
new Notification(event.message);
};
topics[topic] = eventSource;
localStorage.setItem('topics', JSON.stringify(Object.keys(topics)));
}, delaySec * 1000);
2021-10-23 20:21:33 +03:00
};
2021-10-23 04:26:01 +03:00
2021-10-23 22:22:17 +03:00
const unsubscribe = (topic) => {
2021-10-23 20:21:33 +03:00
topics[topic].close();
2021-10-23 22:22:17 +03:00
delete topics[topic];
localStorage.setItem('topics', JSON.stringify(Object.keys(topics)));
2021-10-23 20:21:33 +03:00
document.getElementById(`topic-${topic}`).remove();
};
subscribeForm.onsubmit = function () {
if (!topicField.value) {
return false;
2021-10-23 04:26:01 +03:00
}
2021-10-23 20:21:33 +03:00
subscribe(topicField.value);
2021-10-23 22:22:17 +03:00
topicField.value = "";
2021-10-23 20:21:33 +03:00
return false;
2021-10-23 04:26:01 +03:00
};
2021-10-23 20:21:33 +03:00
2021-10-23 22:22:17 +03:00
// Disable Web UI if notifications of EventSource are not available
2021-10-23 20:21:33 +03: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 22:22:17 +03:00
// Reset UI
topicField.value = "";
// Restore topics
const storedTopics = localStorage.getItem('topics');
if (storedTopics) {
JSON.parse(storedTopics).forEach((topic) => {
subscribeInternal(topic, 0);
});
}
2021-10-23 04:26:01 +03:00
</script>
</body>
</html>