aboutsummaryrefslogtreecommitdiff
path: root/src/js/arching-kaos-fetch.js
blob: a29a5ae379e14de6abe952d9032d8cfb8424be79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function archingKaosFetchJSON( url, callback, params ){
//    fetch(url, {
//        method:'GET',
//        headers:{
//            Accept: 'application/json'
//        }
//    }).then(response=>{
//        if(response.ok){
//            response.json().then(json=>{
//                callback(json);
//            })
//        } else {
//            if (DEBUG) console.log(e);
//        }
//    }).catch( e=>{
//        if (DEBUG) console.log(e);
//    })
    const request = new XMLHttpRequest();
    request.addEventListener("load", ()=>{
        var json = JSON.parse(request.response);
        if(request.status !== 404){
            callback(json, params);
        } else {
            archingKaosLog(`ERROR ${request.status} while loading ${url}`);
        }
    });
    request.addEventListener("error", ()=>{
        console.log("An error occured.");
    });
    request.addEventListener("progress", (event)=>{
        if (event.lengthComputable && progressPlaceholder){
            progressPlaceholder = (event.loaded / event.total) * 100;
        } else {
            progressPlaceholder = 0;
        }
    });
    request.addEventListener("abort", ()=>{
        console.log("Request aborted.");
    });
    request.open("GET", url);
    request.send();

}

async function archingKaosFetchText( url, callback ){
    return fetch(url, {
        method:'GET',
        headers:{
            Accept: 'application/json'
        }
    }).then(response=>{
        if(response.ok){
            response.text().then(text=>{
                return callback(text);
            })
        } else {
            if (DEBUG) console.log(e);
        }
    })
}