Sharkey/src/client/app/boot.js

123 lines
3.8 KiB
JavaScript
Raw Normal View History

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';
2017-06-17 02:56:39 +03:00
// Chromeで確認したことなのですが、constやletを用いたとしても
// グローバルなスコープで定数/変数を定義するとwindowのプロパティ
// としてそれがアクセスできるようになる訳ではありませんが、普通に
// コンソールから定数/変数名を入力するとアクセスできてしまいます。
2017-06-17 03:53:02 +03:00
// ブロック内に入れてスコープを非グローバル化するとそれが防げます
2017-06-17 02:56:39 +03:00
// (Chrome以外のブラウザでは検証していません)
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;
if (url.pathname == '/docs') app = 'docs';
if (url.pathname == '/dev') app = 'dev';
if (url.pathname == '/auth') app = 'auth';
//#endregion
2017-06-06 20:02:56 +03:00
// Detect the user language
2018-04-13 19:56:10 +03:00
// Note: The default language is Japanese
2017-06-06 20:02:56 +03:00
let lang = navigator.language.split('-')[0];
2018-04-13 19:56:10 +03:00
if (!/^(en|ja)$/.test(lang)) lang = 'ja';
2018-03-03 02:49:47 +02:00
if (localStorage.getItem('lang')) lang = localStorage.getItem('lang');
2018-03-15 22:45:28 +02:00
if (ENV != 'production') lang = 'ja';
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-03-15 05:56:50 +02:00
// Script version
2018-03-03 00:32:18 +02:00
const ver = localStorage.getItem('v') || VERSION;
2018-03-15 06:11:31 +02:00
// Whether in debug mode
const isDebug = localStorage.getItem('debug') == 'true';
2018-03-15 05:56:50 +02:00
// Whether use raw version script
2018-03-15 22:45:28 +02:00
const raw = (localStorage.getItem('useRawScript') == 'true' && isDebug)
|| ENV != 'production';
2018-03-15 05:56:50 +02: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-03-15 05:56:50 +02:00
script.setAttribute('src', `/assets/${app}.${ver}.${lang}.${raw ? 'raw' : 'min'}.js`);
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
// 1秒経ってもスクリプトがロードされない場合はバージョンが古くて
// 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 12:29:21 +02:00
if (meta.version != ver) {
2018-04-13 00:06:18 +03:00
localStorage.setItem('v', meta.version);
2017-11-28 08:05:55 +02:00
alert(
'Misskeyの新しいバージョンがあります。ページを再度読み込みします。' +
'\n\n' +
'New version of Misskey available. The page will be reloaded.');
2017-11-28 08:41:41 +02:00
// Clear cache (serive worker)
try {
navigator.serviceWorker.controller.postMessage('clear');
2017-12-08 07:59:43 +02:00
navigator.serviceWorker.getRegistrations().then(registrations => {
registrations.forEach(registration => registration.unregister());
});
2017-11-28 08:41:41 +02:00
} catch (e) {
console.error(e);
}
2017-11-28 08:05:55 +02:00
// Force reload
location.reload(true);
}
}, 1000);
2017-06-06 20:02:56 +03:00
}