1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-15 23:28:26 +09:00
misskey/src/client/app/boot.js

138 lines
3.8 KiB
JavaScript
Raw Normal View History

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