As a newcomer to IPFS and JavaScript, I have successfully created a simple IPFS userscript that redirects addresses containing *://*/ipfs/*
or *://*/ipns/*
to
http://localhost:8080/<IPFS_HASH>
.
However, there is an issue when the local IPFS node is not running, causing the reroute to fail because nothing is happening on localhost:8080. My question is, is there a way for the userscript, which runs in my browser (Safari with Tampermonkey), to determine if localhost:8080 is reachable? If it is reachable, the script should initiate the redirect, but if not, it should do nothing.
When the local IPFS node is active, localhost:8080 returns a "404 page not found" error.
https://i.stack.imgur.com/mX5hA.png
On the other hand, when the node is inactive, Safari cannot reach anything at that address:
https://i.stack.imgur.com/r48Gs.png
The most straightforward solution may be to use JavaScript to mimic the command
curl -o /dev/null --silent --head --write-out "%{http_code}\n" localhost:8080
: if the response is "404", IPFS is active, and the script can proceed with the rerouting; but if it is "000", then IPFS is inactive, and the script should refrain from any action.
How can I implement this functionality in JavaScript? Any assistance would be greatly appreciated.