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