Disambiguation pages are all categorized as "All disambiguation pages", so you can check for that category to see if you're on a disambiguation page.
Thus, you could check if "All_disambiguation_pages" exists as a category to determine if you're on a disambiguation page. Using the query https://en.wikipedia.org/w/api.php?action=parse&prop=categories&page=Andrew%20Morgan:
for (category of r.parse.categories) {
if (Object.values(category).includes("All_disambiguation_pages")) {
// we know it's a disambiguation page
}
}
Alternatively, you could also check for the "Disambiguation" property, using the query:
https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&ppprop=disambiguation&redirects&format=xml&titles=Andrew%20Morgan
Of course, these only tell you if the page is a disambiguation page. Ultimately, you need to know what you're looking for. In the case of "Andrew Morgan", the astronaut is under "Andrew R. Morgan". But some articles might use "John Doe (Astronaut)" or some other title. There's no real standardization for this.
For the example of "astronaut", you could perhaps search the disambiguation page for the keyword "astronaut", and then go to that article:
fetch('https://en.wikipedia.org/w/api.php?action=opensearch&search=andrew%20morgan&format=json&origin=*')
.then(function(response) {
response.json().then(function(data) {
// data[1] is the array of titles, [2] is the array of descriptions, [3] is the array of links
let articleUrl = data[3][data[2].findIndex(element => element.includes("astronaut"))];
if (articleUrl !== -1) { // -1 would be not found
console.log(articleUrl); //the url
}
});
});