Files

30 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2023-03-25 16:51:49 +02:00
// KaiOS 2.x specific part to connect and retrieve raw data from Gopher servers
// Requires "tcp-socket" permission in the manifest
// and the "privileged" or "certified" app permission level
// resource format: [type, selector, host, port]
// type is autotrimmed, so you don't have to manually strip the description
// input is optional and only considered for resource type 7
function gopherRequest(resource, input, successCb, errorCb) {
2023-03-25 18:52:38 +02:00
var xsock = navigator.mozTCPSocket.open(resource[2], (resource[3]||70) | 0, {binaryType: 'arraybuffer'}),
type = resource[0][0], databuf = []
xsock.ondata = function(e) {
var i = 0, db = new Uint8Array(e.data), l = db.length
for(;i<l;i++)
databuf.push(db[i])
2023-03-25 16:51:49 +02:00
}
xsock.onclose = function() {
2023-03-25 18:52:38 +02:00
var res = new Uint8Array(databuf)
successCb(res, type)
databuf = []
2023-03-25 16:51:49 +02:00
}
xsock.onerror = errorCb
xsock.onopen = function() {
var selpath = resource[1]
if(type == '7' && input)
selpath += '\t' + input
2023-03-25 18:52:38 +02:00
var reqbuf = (new TextEncoder).encode(selpath + '\r\n').buffer
xsock.send(reqbuf) // send CRLF-terminated selector path and optionally the search string
2023-03-25 16:51:49 +02:00
}
}