diff --git a/main.js b/main.js index befd9ab..caf1c99 100644 --- a/main.js +++ b/main.js @@ -8,7 +8,6 @@ var wobblyGo var wobblySpinner var startpage = 'gemini://tildeverse.org/' var xhr -var xhrTimeout function geminiInit() { // Store references to all our DOM elements @@ -82,35 +81,33 @@ function geminiGo(fromURL) { pushState() } + // Enable throbber to indicate page loading wobblySpinner.classList.add('active') + // If any unfinished xhrs, abort them and begin anew if (xhr) { xhr.abort() } - // Send gemini request to cgi and populate result into wobbly window - xhrTimout = setTimeout(stopXHR, 5000) + // Setup gemini request for CGI xhr = new XMLHttpRequest() xhr.open( "GET", "browser.cgi?" + url ) - xhr.onload = function () { + + // If success: show page contents and clear throbber + xhr.onload = () => { const body = xhr.responseText wobblyWindow.innerHTML = body wobblySpinner.classList.remove('active') - clearTimeout(xhrTimeout) } - xhr.onerror = function () { - console.error(xhr.statusText) + + // If error: show error contents on page + xhr.onerror = () => { wobblySpinner.classList.remove('active') wobblyWindow.innerHTML = `

Something went wrong

Error reported:

${xhr.statusText}

` - clearTimeout(xhrTimeout) } - xhr.send(null) -} -function stopXHR() { - xhr.abort() - wobblySpinner.classList.remove('active') - wobblyWindow.innerHTML = `

Request Timeout

Your request was taking too long so it has been cancelled.

` + // Initiate request + xhr.send(null) } // Simple show/hide of help block