Quick Note before I begin: While I discuss Apple Shortcuts to explain the context behind my question, this is still a web/JavaScript problem. The only thing that you need to know is that I cannot run asynchronous or callback JavaScript code. If you are curious about the details behind the exact implementation/limitations of the JavaScript code made in this post, see Running JavaScript in Shortcuts. An example shortcut displaying my problem (which goes over the exact same things talked about in this post) can be found here
Once upon a time, I had some intentionally terrible and outdated JavaScript that I was forced to use as a workaround for a limitation on a block-coding automation app on my iPhone called “Shortcuts.” You see, there are times when I need the automations I write for Shortcuts to access an API or send some API calls. Shortcuts has an internal API Request Library called “Get Contents of URL”, but it’s borderline useless because it only returns response bodies. Since I needed additional data like the request’s status code, I made my own mediocre API Requester replacement in JavaScript. This is what it looked like in shortcuts:
data:text/html,<body/><script>o={error:"NetworkError",message:"You%20are%20offline!",stack:null};if(navigator.onLine){r=new%20XMLHttpRequest();r.open("GET","https://jsonplaceholder.typicode.com/users",false);try{r.send();o={"Status":r.status,"Response":r.responseText}}catch(e){o={error:e?.name??"UnknownError",message:e?.message??String(e),stack:e?.stack??null};}};document.write(JSON.stringify(o));</script>
And here it is as a more human-legible snippet:
<body />
<script>
o = {
error: "NetworkError",
message: "You are offline!",
stack: null
};
if (navigator.onLine) {
r = new XMLHttpRequest();
r.open("GET", "https://jsonplaceholder.typicode.com/users", false);
try {
r.send();
o = {
"Status": r.status,
"Response": r.responseText
}
} catch (e) {
o = {
error: e?.name ?? "UnknownError",
message: e?.message ?? String(e),
stack: e?.stack ?? null
};
}
};
document.write(JSON.stringify(o));
</script>
I would then extract the text from the JavaScript webpage, then move on with my life. However, I’m finding that some URLs passed into this just flat out don’t work. For instance, take https://zenquotes.io/api/random, a REST API GET request that’s supposed to return a random quote:
<body />
<script>
o = {
error: "NetworkError",
message: "You are offline!",
stack: null
};
if (navigator.onLine) {
r = new XMLHttpRequest();
r.open("GET", "https://zenquotes.io/api/random", false);
try {
r.send();
o = {
"Status": r.status,
"Response": r.responseText
}
} catch (e) {
o = {
error: e?.name ?? "UnknownError",
message: e?.message ?? String(e),
stack: e?.stack ?? null
};
}
};
document.write(JSON.stringify(o));
</script>
And here’s its pasteable URL equivalent:
data:text/html,<body/><script>o={error:"NetworkError",message:"You%20are%20offline!",stack:null};if(navigator.onLine){r=new%20XMLHttpRequest();r.open("GET","https://zenquotes.io/api/random",false);try{r.send();o={"Status":r.status,"Response":r.responseText}}catch(e){o={error:e?.name??"UnknownError",message:e?.message??String(e),stack:e?.stack??null};}};document.write(JSON.stringify(o));</script>
On mobile, all it does is give a vague “NetworkError” error explaining that it couldn’t connect to the internet. Thankfully, I get a more verbose error when I run the code on my PC saying that it “failed to load [URL].” No idea what that means, why it’s happening, or how to fix it (if that’s even possible). All I did was change the URL that it’s requesting, so I’m assuming that my code as-is isn’t all-encompassing enough or that I’m missing some important information. I think this because when I try requesting the URL through Shortcut’s native Request Library, it works fine. Any ideas? If you want to see what this problem looks like where I’m running it, check out this demo shortcut I made (all it does is go over everything I just talked about here and lets you test your own JS code if you have an idea for a solution)
