2016-12-31 20:23:09 -06:00
|
|
|
/**
|
2017-05-18 00:12:27 -05:00
|
|
|
* MISSKEY BOOT LOADER
|
|
|
|
* (ENTRY POINT)
|
2016-12-31 20:23:09 -06:00
|
|
|
*/
|
2016-12-31 05:26:22 -06:00
|
|
|
|
2017-05-18 00:12:27 -05:00
|
|
|
'use strict';
|
|
|
|
|
2018-11-11 14:03:12 -06:00
|
|
|
(async function() {
|
2018-04-21 02:18:58 -05:00
|
|
|
// キャッシュ削除要求があれば従う
|
|
|
|
if (localStorage.getItem('shouldFlush') == 'true') {
|
|
|
|
refresh();
|
|
|
|
return;
|
|
|
|
}
|
2018-04-16 01:59:43 -05:00
|
|
|
|
2018-09-10 03:50:34 -05:00
|
|
|
const langs = LANGS;
|
|
|
|
|
2018-09-26 04:59:37 -05:00
|
|
|
//#region Apply theme
|
|
|
|
const theme = localStorage.getItem('theme');
|
|
|
|
if (theme) {
|
2018-12-11 05:36:55 -06:00
|
|
|
for (const [k, v] of Object.entries(JSON.parse(theme))) {
|
2018-09-26 04:59:37 -05:00
|
|
|
document.documentElement.style.setProperty(`--${k}`, v.toString());
|
2018-12-11 05:36:55 -06:00
|
|
|
}
|
2018-09-26 04:59:37 -05:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-05-23 15:28:46 -05:00
|
|
|
//#region Load settings
|
|
|
|
let settings = null;
|
|
|
|
const vuex = localStorage.getItem('vuex');
|
|
|
|
if (vuex) {
|
|
|
|
settings = JSON.parse(vuex);
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2017-06-06 12:02:56 -05:00
|
|
|
// Get the current url information
|
|
|
|
const url = new URL(location.href);
|
2017-03-17 10:02:41 -05:00
|
|
|
|
2018-03-27 00:13:12 -05:00
|
|
|
//#region Detect app name
|
2019-05-02 10:05:12 -05:00
|
|
|
let app = null;
|
2018-03-27 00:13:12 -05:00
|
|
|
|
2019-05-02 10:05:12 -05:00
|
|
|
if (`${url.pathname}/`.startsWith('/docs/')) app = 'docs';
|
|
|
|
if (`${url.pathname}/`.startsWith('/dev/')) app = 'dev';
|
|
|
|
if (`${url.pathname}/`.startsWith('/auth/')) app = 'auth';
|
|
|
|
if (`${url.pathname}/`.startsWith('/admin/')) app = 'admin';
|
2018-03-27 00:13:12 -05:00
|
|
|
//#endregion
|
2017-06-06 12:02:56 -05:00
|
|
|
|
2018-11-14 14:20:25 -06:00
|
|
|
// Script version
|
|
|
|
const ver = localStorage.getItem('v') || VERSION;
|
|
|
|
|
2018-05-20 12:13:39 -05:00
|
|
|
//#region Detect the user language
|
2018-08-22 11:37:05 -05:00
|
|
|
let lang = null;
|
2018-08-21 11:48:08 -05:00
|
|
|
|
2018-09-10 03:50:34 -05:00
|
|
|
if (langs.includes(navigator.language)) {
|
2018-08-22 11:37:05 -05:00
|
|
|
lang = navigator.language;
|
|
|
|
} else {
|
2018-09-10 03:50:34 -05:00
|
|
|
lang = langs.find(x => x.split('-')[0] == navigator.language);
|
2018-05-20 12:13:39 -05:00
|
|
|
|
2018-08-22 11:37:05 -05:00
|
|
|
if (lang == null) {
|
|
|
|
// Fallback
|
|
|
|
lang = 'en-US';
|
|
|
|
}
|
|
|
|
}
|
2018-05-20 12:13:39 -05:00
|
|
|
|
2018-08-25 05:39:06 -05:00
|
|
|
if (settings && settings.device.lang &&
|
2019-02-05 02:30:17 -06:00
|
|
|
langs.includes(settings.device.lang))
|
|
|
|
{
|
2018-08-25 03:06:40 -05:00
|
|
|
lang = settings.device.lang;
|
2018-08-25 05:39:06 -05:00
|
|
|
}
|
2018-11-11 14:03:12 -06:00
|
|
|
|
2019-02-04 23:42:18 -06:00
|
|
|
localStorage.setItem('lang', lang);
|
2018-05-20 12:13:39 -05:00
|
|
|
//#endregion
|
2017-06-06 12:02:56 -05:00
|
|
|
|
2019-01-31 09:09:28 -06:00
|
|
|
//#region Fetch locale data
|
|
|
|
const cachedLocale = localStorage.getItem('locale');
|
2018-11-16 02:13:22 -06:00
|
|
|
const localeKey = localStorage.getItem('localeKey');
|
|
|
|
|
2019-01-31 09:09:28 -06:00
|
|
|
if (cachedLocale == null || localeKey != `${ver}.${lang}`) {
|
2018-11-14 14:20:25 -06:00
|
|
|
const locale = await fetch(`/assets/locales/${lang}.json?ver=${ver}`)
|
2018-11-11 14:03:12 -06:00
|
|
|
.then(response => response.json());
|
|
|
|
|
2019-01-31 09:09:28 -06:00
|
|
|
localStorage.setItem('locale', JSON.stringify(locale));
|
|
|
|
localStorage.setItem('localeKey', `${ver}.${lang}`);
|
2018-11-11 14:03:12 -06:00
|
|
|
}
|
2019-01-31 09:09:28 -06:00
|
|
|
//#endregion
|
2018-11-11 14:03:12 -06:00
|
|
|
|
2017-06-06 12:02:56 -05:00
|
|
|
// Detect the user agent
|
|
|
|
const ua = navigator.userAgent.toLowerCase();
|
2019-07-07 14:32:57 -05:00
|
|
|
let isMobile = /mobile|iphone|ipad|android/.test(ua) || window.innerWidth < 576;
|
2019-05-29 06:44:20 -05:00
|
|
|
if (settings && settings.device.appTypeForce) {
|
|
|
|
if (settings.device.appTypeForce === 'mobile') {
|
2019-07-07 14:32:57 -05:00
|
|
|
isMobile = true;
|
2019-05-29 06:44:20 -05:00
|
|
|
} else if (settings.device.appTypeForce === 'desktop') {
|
2019-07-07 14:32:57 -05:00
|
|
|
isMobile = false;
|
2019-05-29 06:44:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-31 05:26:22 -06:00
|
|
|
|
2017-06-06 12:02:56 -05:00
|
|
|
// Get the <head> element
|
|
|
|
const head = document.getElementsByTagName('head')[0];
|
|
|
|
|
|
|
|
// If mobile, insert the viewport meta tag
|
|
|
|
if (isMobile) {
|
|
|
|
const meta = document.createElement('meta');
|
|
|
|
meta.setAttribute('name', 'viewport');
|
2017-08-10 12:00:51 -05:00
|
|
|
meta.setAttribute('content',
|
|
|
|
'width=device-width,' +
|
|
|
|
'initial-scale=1,' +
|
|
|
|
'minimum-scale=1,' +
|
|
|
|
'maximum-scale=1,' +
|
|
|
|
'user-scalable=no');
|
2017-06-06 12:02:56 -05:00
|
|
|
head.appendChild(meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch desktop or mobile version
|
2019-05-02 10:05:12 -05:00
|
|
|
if (app == null) {
|
|
|
|
app = isMobile ? 'mobile' : 'desktop';
|
2017-06-06 12:02:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load an app script
|
|
|
|
// Note: 'async' make it possible to load the script asyncly.
|
|
|
|
// 'defer' make it possible to run the script when the dom loaded.
|
|
|
|
const script = document.createElement('script');
|
2019-05-02 10:05:12 -05:00
|
|
|
script.setAttribute('src', `/assets/${app}.${ver}.js`);
|
2017-06-06 12:02:56 -05:00
|
|
|
script.setAttribute('async', 'true');
|
|
|
|
script.setAttribute('defer', 'true');
|
|
|
|
head.appendChild(script);
|
2017-11-28 00:05:55 -06:00
|
|
|
|
2018-04-13 15:23:13 -05:00
|
|
|
// 3秒経ってもスクリプトがロードされない場合はバージョンが古くて
|
2017-11-28 00:05:55 -06:00
|
|
|
// 404になっているせいかもしれないので、バージョンを確認して古ければ更新する
|
|
|
|
//
|
|
|
|
// 読み込まれたスクリプトからこのタイマーを解除できるように、
|
|
|
|
// グローバルにタイマーIDを代入しておく
|
|
|
|
window.mkBootTimer = window.setTimeout(async () => {
|
|
|
|
// Fetch meta
|
2018-08-21 11:50:13 -05:00
|
|
|
const res = await fetch('/api/meta', {
|
2017-11-28 00:05:55 -06:00
|
|
|
method: 'POST',
|
|
|
|
cache: 'no-cache'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Parse
|
|
|
|
const meta = await res.json();
|
|
|
|
|
|
|
|
// Compare versions
|
2019-02-17 13:52:40 -06:00
|
|
|
if (meta.version != ver) {
|
|
|
|
localStorage.setItem('v', meta.version);
|
2018-04-12 16:06:18 -05:00
|
|
|
|
2017-11-28 00:05:55 -06:00
|
|
|
alert(
|
|
|
|
'Misskeyの新しいバージョンがあります。ページを再度読み込みします。' +
|
|
|
|
'\n\n' +
|
|
|
|
'New version of Misskey available. The page will be reloaded.');
|
|
|
|
|
2018-04-16 01:59:43 -05:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}, 3000);
|
|
|
|
|
|
|
|
function refresh() {
|
|
|
|
localStorage.setItem('shouldFlush', 'false');
|
2017-12-07 23:59:43 -06:00
|
|
|
|
2018-11-12 10:11:36 -06:00
|
|
|
localStorage.removeItem('locale');
|
|
|
|
|
2018-09-06 10:44:57 -05:00
|
|
|
// Clear cache (service worker)
|
2018-04-16 01:59:43 -05:00
|
|
|
try {
|
|
|
|
navigator.serviceWorker.controller.postMessage('clear');
|
2017-11-28 00:41:41 -06:00
|
|
|
|
2018-04-16 01:59:43 -05:00
|
|
|
navigator.serviceWorker.getRegistrations().then(registrations => {
|
2018-12-11 05:36:55 -06:00
|
|
|
for (const registration of registrations) registration.unregister();
|
2018-04-16 01:59:43 -05:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2017-11-28 00:05:55 -06:00
|
|
|
}
|
2018-04-16 01:59:43 -05:00
|
|
|
|
|
|
|
// Force reload
|
2018-04-21 02:32:15 -05:00
|
|
|
location.reload(true);
|
2018-04-16 01:59:43 -05:00
|
|
|
}
|
2018-04-21 02:21:12 -05:00
|
|
|
})();
|