2020-04-02 19:44:21 +09:00
|
|
|
let data;
|
|
|
|
|
2020-09-27 17:48:29 +03:00
|
|
|
const urlResolver = document.createElement('a');
|
|
|
|
|
2020-09-26 12:27:14 +03:00
|
|
|
// `fetch` with `file:` support
|
|
|
|
// Recent browsers seem to support `file` protocol under some conditions.
|
|
|
|
// Based on https://github.com/github/fetch/pull/92#issuecomment-174730593
|
2020-09-27 17:48:29 +03:00
|
|
|
// https://github.com/github/fetch/pull/92#issuecomment-512187452
|
2020-09-26 12:27:14 +03:00
|
|
|
async function fetchLocal(url, options) {
|
2020-09-27 17:48:29 +03:00
|
|
|
urlResolver.href = url;
|
|
|
|
|
|
|
|
const requestURL = urlResolver.href;
|
|
|
|
|
2020-09-26 12:27:14 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest;
|
|
|
|
|
|
|
|
xhr.onload = () => {
|
|
|
|
// `file` protocol has invalid OK status
|
|
|
|
let status = xhr.status;
|
2020-09-27 17:48:29 +03:00
|
|
|
if (requestURL.startsWith('file:') && status === 0) {
|
2020-09-26 12:27:14 +03:00
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:48:29 +03:00
|
|
|
/* eslint-disable-next-line compat/compat */
|
2020-09-26 12:27:14 +03:00
|
|
|
resolve(new Response(xhr.responseText, {status: status}));
|
2020-09-27 17:48:29 +03:00
|
|
|
};
|
2020-09-26 12:27:14 +03:00
|
|
|
|
|
|
|
xhr.onerror = () => {
|
|
|
|
reject(new TypeError('Local request failed'));
|
2020-09-27 17:48:29 +03:00
|
|
|
};
|
2020-09-26 12:27:14 +03:00
|
|
|
|
|
|
|
xhr.open('GET', url);
|
|
|
|
|
|
|
|
if (options && options.cache) {
|
|
|
|
xhr.setRequestHeader('Cache-Control', options.cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
xhr.send(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-09 13:24:16 +02:00
|
|
|
async function getConfig() {
|
2020-04-02 19:44:21 +09:00
|
|
|
if (data) return Promise.resolve(data);
|
2020-08-09 13:24:16 +02:00
|
|
|
try {
|
2020-09-26 12:27:14 +03:00
|
|
|
const response = await fetchLocal('config.json', {
|
2020-08-09 13:24:16 +02:00
|
|
|
cache: 'no-cache'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('network response was not ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
data = await response.json();
|
|
|
|
|
2020-05-12 06:28:14 +09:00
|
|
|
return data;
|
2020-08-09 13:24:16 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.warn('failed to fetch the web config file:', error);
|
2020-05-12 06:28:14 +09:00
|
|
|
return getDefaultConfig();
|
2020-08-09 13:24:16 +02:00
|
|
|
}
|
2020-05-12 06:28:14 +09:00
|
|
|
}
|
|
|
|
|
2020-08-09 13:24:16 +02:00
|
|
|
async function getDefaultConfig() {
|
|
|
|
try {
|
2020-09-26 12:27:14 +03:00
|
|
|
const response = await fetchLocal('config.template.json', {
|
2020-08-09 13:24:16 +02:00
|
|
|
cache: 'no-cache'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('network response was not ok');
|
|
|
|
}
|
|
|
|
|
2020-08-09 14:24:59 +02:00
|
|
|
data = await response.json();
|
2020-04-02 19:50:09 +09:00
|
|
|
return data;
|
2020-08-09 13:24:16 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error('failed to fetch the default web config file:', error);
|
|
|
|
}
|
2020-04-02 03:51:22 +09:00
|
|
|
}
|
|
|
|
|
2020-08-02 17:28:25 +09:00
|
|
|
export function getMultiServer() {
|
2020-04-02 03:51:22 +09:00
|
|
|
return getConfig().then(config => {
|
|
|
|
return config.multiserver;
|
2020-04-24 13:46:57 +03:00
|
|
|
}).catch(error => {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.log('cannot get web config:', error);
|
2020-04-24 13:46:57 +03:00
|
|
|
return false;
|
2020-04-02 03:51:22 +09:00
|
|
|
});
|
|
|
|
}
|
2020-08-02 17:28:25 +09:00
|
|
|
|
2020-09-12 09:12:40 +09:00
|
|
|
export function getServers() {
|
|
|
|
return getConfig().then(config => {
|
|
|
|
return config.servers;
|
|
|
|
}).catch(error => {
|
|
|
|
console.log('cannot get web config:', error);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-02 17:28:25 +09:00
|
|
|
export function getThemes() {
|
|
|
|
return getConfig().then(config => {
|
|
|
|
return config.themes;
|
|
|
|
}).catch(error => {
|
|
|
|
console.log('cannot get web config:', error);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPlugins() {
|
|
|
|
return getConfig().then(config => {
|
|
|
|
return config.plugins;
|
|
|
|
}).catch(error => {
|
|
|
|
console.log('cannot get web config:', error);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|