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