forked from tomasino/wobbly
107 lines
3.0 KiB
JavaScript
107 lines
3.0 KiB
JavaScript
var geminiHistory = []
|
|
var geminiPresent = 0
|
|
|
|
// DOM Elements
|
|
var wobblyWindow
|
|
var wobblyHelpButton
|
|
|
|
function geminiInit() {
|
|
wobblyWindow = document.getElementById("wobbly-window")
|
|
wobblyHelpButton = document.getElementById('wobbly-help-button')
|
|
wobblyHelpButton.addEventListener('click', geminiHelpToggle)
|
|
|
|
// Clear screen to init
|
|
wobblyWindow.innerHTML = ""
|
|
|
|
var startpage = "gemini://tildeverse.org/"
|
|
query_string = window.location.href.split('?')
|
|
if (query_string.length > 1){
|
|
startpage = query_string[1]
|
|
}
|
|
|
|
document.getElementById("wobbly-addressbar").value = startpage
|
|
geminiHistory.push({url:startpage})
|
|
geminiGo()
|
|
}
|
|
|
|
function geminiHelpToggle() {
|
|
if (document.getElementById("wobbly-help-button").innerHTML === 'Help') {
|
|
document.getElementById("wobbly-help").style.display = 'block'
|
|
document.getElementById("wobbly-help-button").innerHTML = "Unhelp"
|
|
} else {
|
|
document.getElementById("wobbly-help").style.display = 'none'
|
|
document.getElementById("wobbly-help-button").innerHTML = "Help"
|
|
}
|
|
}
|
|
|
|
function geminiGo() {
|
|
url = document.getElementById("wobbly-addressbar").value.trim().split("?")[0]
|
|
if (url.length == 0) {
|
|
return
|
|
}
|
|
if (! url.startsWith("gemini://")) {
|
|
url = "gemini://" + url
|
|
}
|
|
if (! (url === geminiHistory[geminiPresent].url)) {
|
|
geminiPresent++
|
|
geminiHistory[geminiPresent] = {url:url}
|
|
while (geminiHistory.length > geminiPresent +1) {
|
|
geminiHistory.pop()
|
|
}
|
|
}
|
|
var xmlHttp = new XMLHttpRequest()
|
|
xmlHttp.open( "GET", "browser.cgi?" + url )
|
|
xmlHttp.onload = function (e) {
|
|
var body = xmlHttp.responseText
|
|
document.getElementById("wobbly-window").innerHTML = body
|
|
geminiHistory[geminiPresent].body = body
|
|
}
|
|
xmlHttp.send(null)
|
|
}
|
|
|
|
function geminiBack() {
|
|
if (geminiPresent > 0) {
|
|
geminiPresent--
|
|
document.getElementById("wobbly-addressbar").value = geminiHistory[geminiPresent].url
|
|
document.getElementById("wobbly-window").innerHTML = geminiHistory[geminiPresent].body
|
|
}
|
|
}
|
|
|
|
// Big thanks to idiomdrottning for fixing my geminiUp function <3
|
|
function geminiUp() {
|
|
var components = geminiHistory[geminiPresent].url.match(/\/[^/]*/g)
|
|
if ("/" == components.pop()) components.pop()
|
|
if (components.length == 1) return
|
|
document.getElementById("wobbly-addressbar").value = "gemini:" + components.join("") + "/"
|
|
geminiGo()
|
|
}
|
|
function geminiForward() {
|
|
if (geminiPresent < geminiHistory.length - 1) {
|
|
geminiPresent++
|
|
document.getElementById("wobbly-addressbar").value = geminiHistory[geminiPresent].url
|
|
document.getElementById("wobbly-window").innerHTML = geminiHistory[geminiPresent].body
|
|
}
|
|
}
|
|
function geminiEnterListener(e) {
|
|
if(e.keyCode === 13) {
|
|
e.preventDefault()
|
|
geminiGo()
|
|
}
|
|
}
|
|
|
|
function geminiLinkClickListener(e) {
|
|
if (e.target.tagName === 'A' && e.target.href.startsWith("gemini://")) {
|
|
// We only care about clicked gemini:// links
|
|
e.preventDefault()
|
|
document.getElementById("wobbly-addressbar").value = e.target.href
|
|
geminiGo()
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
window.onload = function () {
|
|
geminiInit()
|
|
}
|