From fde5fda635c92a55bd0d4213750536640fc83fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathana=C3=ABl=20Houn?= Date: Mon, 22 Nov 2021 14:30:09 +0100 Subject: [PATCH] Use custom url in case of self-hosted --- server/index.gohtml | 30 +++++++++++++++--------------- server/static/js/app.js | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/server/index.gohtml b/server/index.gohtml index 0971511..b369c2e 100644 --- a/server/index.gohtml +++ b/server/index.gohtml @@ -67,19 +67,19 @@ Here's an example showing how to publish a message using curl (via POST):

- curl -d "Backup successful 😀" ntfy.sh/mytopic + curl -d "Backup successful 😀" ntfy.sh/mytopic

And another one using PUT:

- echo -en "\u26A0\uFE0F Unauthorized login" | curl -sT- ntfy.sh/mytopic + echo -en "\u26A0\uFE0F Unauthorized login" | curl -sT- ntfy.sh/mytopic

Here's an example in JS with fetch() (see full example):

- fetch('https://ntfy.sh/mytopic', {
+ fetch('https://ntfy.sh/mytopic', {
  method: 'POST', // PUT works too
  body: 'Hello from the other side.'
}) @@ -127,7 +127,7 @@ notifications like this (see live example):

- const eventSource = new EventSource('https://ntfy.sh/mytopic/sse');
+ const eventSource = new EventSource('https://ntfy.sh/mytopic/sse');
eventSource.onmessage = (e) => {
  // Do something with e.data
}; @@ -136,7 +136,7 @@ You can also use the same /sse endpoint via curl or any other HTTP library:

- $ curl -s ntfy.sh/mytopic/sse
+ $ curl -s ntfy.sh/mytopic/sse
event: open
data: {"id":"weSj9RtNkj","time":1635528898,"event":"open","topic":"mytopic"}

@@ -149,7 +149,7 @@ To consume JSON instead, use the /json endpoint, which prints one message per line:

- $ curl -s ntfy.sh/mytopic/json
+ $ curl -s ntfy.sh/mytopic/json
{"id":"SLiKI64DOt","time":1635528757,"event":"open","topic":"mytopic"}
{"id":"hwQ2YpKdmg","time":1635528741,"event":"message","topic":"mytopic","message":"Hi!"}
{"id":"DGUDShMCsc","time":1635528787,"event":"keepalive","topic":"mytopic"} @@ -158,7 +158,7 @@ Or use the /raw endpoint if you need something super simple (empty lines are keepalive messages):

- $ curl -s ntfy.sh/mytopic/raw
+ $ curl -s ntfy.sh/mytopic/raw

This is a notification
And another one with a smiley face 😀 @@ -173,7 +173,7 @@ cached messages).

- curl -s "ntfy.sh/mytopic/json?since=10m" + curl -s "ntfy.sh/mytopic/json?since=10m"

Polling (poll=1)

@@ -183,7 +183,7 @@ combined with since= (defaults to since=all).

- curl -s "ntfy.sh/mytopic/json?poll=1" + curl -s "ntfy.sh/mytopic/json?poll=1"

Subscribing to multiple topics (topic1,topic2,...)

@@ -192,7 +192,7 @@ comma-separated list of topics in the URL. This allows you to reduce the number of connections you have to maintain:

- $ curl -s ntfy.sh/mytopic1,mytopic2/json
+ $ curl -s ntfy.sh/mytopic1,mytopic2/json
{"id":"0OkXIryH3H","time":1637182619,"event":"open","topic":"mytopic1,mytopic2,mytopic3"}
{"id":"dzJJm7BCWs","time":1637182634,"event":"message","topic":"mytopic1","message":"for topic 1"}
{"id":"Cm02DsxUHb","time":1637182643,"event":"message","topic":"mytopic2","message":"for topic 2"} @@ -214,8 +214,8 @@ rsync -a root@laptop /backups/laptop \
  && zfs snapshot ... \
-   && curl -d "Laptop backup succeeded" ntfy.sh/backups \
-   || echo -en "\u26A0\uFE0F Laptop backup failed" | curl -sT- ntfy.sh/backups +   && curl -d "Laptop backup succeeded" ntfy.sh/backups \
+   || echo -en "\u26A0\uFE0F Laptop backup failed" | curl -sT- ntfy.sh/backups

Example: Server-sent messages in your web app

@@ -242,7 +242,7 @@ #!/bin/bash
if [ "${PAM_TYPE}" = "open_session" ]; then
-   echo -en "\u26A0\uFE0F SSH login: ${PAM_USER} from ${PAM_RHOST}" | curl -T- ntfy.sh/alerts
+   echo -en "\u26A0\uFE0F SSH login: ${PAM_USER} from ${PAM_RHOST}" | curl -T- ntfy.sh/alerts
fi
@@ -254,7 +254,7 @@ while read result; do
  [ -n "$result" ] && echo "$result" >> results.csv
- done < <(stdbuf -i0 -o0 curl -s ntfy.sh/results/raw) + done < <(stdbuf -i0 -o0 curl -s ntfy.sh/results/raw)

FAQ

@@ -331,7 +331,7 @@ To send notifications to it, simply PUT or POST to the topic URL. Here's an example using curl:

- curl -d "Backup failed" + curl -d "Backup failed" ntfy.sh/topic

If you'd like to receive desktop notifications when new messages arrive on this topic, you have diff --git a/server/static/js/app.js b/server/static/js/app.js index 11982f7..f89a658 100644 --- a/server/static/js/app.js +++ b/server/static/js/app.js @@ -12,6 +12,10 @@ let topics = {}; let currentTopic = ""; let currentTopicUnsubscribeOnClose = false; +let currentUrl = window.location.hostname; +if (window.location.port) { + currentUrl += ':' + window.location.port +} /* Main view */ const main = document.getElementById("main"); @@ -131,15 +135,15 @@ const fetchCachedMessages = async (topic) => { const showDetail = (topic) => { currentTopic = topic; - history.replaceState(topic, `ntfy.sh/${topic}`, `/${topic}`); + history.replaceState(topic, `${currentUrl}/${topic}`, `/${topic}`); window.scrollTo(0, 0); rerenderDetailView(); return false; }; const rerenderDetailView = () => { - detailTitle.innerHTML = `ntfy.sh/${currentTopic}`; // document.location.replaceAll(..) - detailTopicUrl.innerHTML = `ntfy.sh/${currentTopic}`; + detailTitle.innerHTML = `${currentUrl}/${currentTopic}`; // document.location.replaceAll(..) + detailTopicUrl.innerHTML = `${currentUrl}/${currentTopic}`; while (detailEventsList.firstChild) { detailEventsList.removeChild(detailEventsList.firstChild); } @@ -347,3 +351,11 @@ document.querySelectorAll('.anchor').forEach((el) => { el.appendChild(anchor); } }); + +// Change ntfy.sh url and protocol to match self-hosted one +document.querySelectorAll('.ntfyUrl').forEach((el) => { + el.innerHTML = currentUrl; +}); +document.querySelectorAll('.ntfyProtocol').forEach((el) => { + el.innerHTML = window.location.protocol + "//"; +});