replace direct uses of fetch with fetchWithType
This commit is contained in:
parent
c7750ad9e8
commit
b31373f6a7
1 changed files with 32 additions and 14 deletions
|
@ -16,6 +16,31 @@ function initDB(obj){
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fetchWithType(url){
|
||||||
|
return new Promise((resolve,reject)=>{
|
||||||
|
const ext = url.substring(url.lastIndexOf(".")+1);
|
||||||
|
let expected = (ext === "json") ? "application/json" : (ext === "css") ? "text/css" : null;
|
||||||
|
if(!expected){
|
||||||
|
reject("unsupported file extension");
|
||||||
|
}
|
||||||
|
fetch(url)
|
||||||
|
.then(response =>{
|
||||||
|
const contentType = response.headers.get('content-type');
|
||||||
|
if (!contentType || !contentType.includes(expected)) {
|
||||||
|
reject(`Oops, we got ${contentType} but expected ${expected}`);
|
||||||
|
}
|
||||||
|
if(ext === "json"){
|
||||||
|
response.json()
|
||||||
|
.then(r=>resolve(r))
|
||||||
|
}else{
|
||||||
|
response.text()
|
||||||
|
.then(r=>resolve(r))
|
||||||
|
|
||||||
|
}
|
||||||
|
},except => reject(except))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let previousCategory = new (function(){
|
let previousCategory = new (function(){
|
||||||
let current = null;
|
let current = null;
|
||||||
this.set = function(t){
|
this.set = function(t){
|
||||||
|
@ -42,9 +67,9 @@ async function onCategoryClicked(categoryNode){
|
||||||
|
|
||||||
async function onTargetClicked(targetNode){
|
async function onTargetClicked(targetNode){
|
||||||
const codeBlock = document.querySelector("pre");
|
const codeBlock = document.querySelector("pre");
|
||||||
let file = await fetch(`chrome/${getText(targetNode)}`);
|
fetchWithType(`chrome/${getText(targetNode)}`)
|
||||||
let t = await file.text();
|
.then(text => (codeBlock.textContent = text))
|
||||||
codeBlock.textContent = t;
|
.catch(e => console.log(e))
|
||||||
}
|
}
|
||||||
|
|
||||||
function onSomeClicked(e){
|
function onSomeClicked(e){
|
||||||
|
@ -124,16 +149,9 @@ function createCategories(){
|
||||||
|
|
||||||
document.onreadystatechange = (function () {
|
document.onreadystatechange = (function () {
|
||||||
if (document.readyState === "complete") {
|
if (document.readyState === "complete") {
|
||||||
fetch("html_resources/tagmap.json")
|
fetchWithType("html_resources/tagmap.json")
|
||||||
.then(response => {
|
.then(response=>(initDB(response)))
|
||||||
const contentType = response.headers.get('content-type');
|
.then(()=>createCategories())
|
||||||
if (!contentType || !contentType.includes('application/json')) {
|
.catch(e=>{console.log(e);document.getElementById("ui").textContent = "FAILURE, Database could not be loaded"});
|
||||||
throw new TypeError("Oops, we haven't got JSON!");
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
})
|
|
||||||
.then(json=>(initDB(json)))
|
|
||||||
.then(()=>createCategories())
|
|
||||||
.catch(e=>{console.log(e);document.getElementById("ui").textContent = "FAILURE, Database could not be loaded"});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
Loading…
Reference in a new issue