iceshrimp-legacy/src/web/app/boot.js

71 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-01-01 03:23:09 +01:00
/**
2017-05-18 07:12:27 +02:00
* MISSKEY BOOT LOADER
* (ENTRY POINT)
2017-01-01 03:23:09 +01:00
*/
2016-12-31 12:26:22 +01:00
2017-05-18 07:12:27 +02:00
/**
* ドメインに基づいて適切なスクリプトを読み込みます
* ユーザーの言語およびモバイル端末か否かも考慮します
2017-05-24 14:06:19 +02:00
* webpackは介さないためrequireやimportは使えません
2017-05-18 07:12:27 +02:00
*/
'use strict';
2017-06-17 01:56:39 +02:00
// Chromeで確認したことなのですが、constやletを用いたとしても
// グローバルなスコープで定数/変数を定義するとwindowのプロパティ
// としてそれがアクセスできるようになる訳ではありませんが、普通に
// コンソールから定数/変数名を入力するとアクセスできてしまいます。
2017-06-17 02:53:02 +02:00
// ブロック内に入れてスコープを非グローバル化するとそれが防げます
2017-06-17 01:56:39 +02:00
// (Chrome以外のブラウザでは検証していません)
2017-06-06 19:02:56 +02:00
{
// Get the current url information
const url = new URL(location.href);
2017-03-17 16:02:41 +01:00
2017-06-06 19:02:56 +02:00
// Extarct the (sub) domain part of the current url
//
// e.g.
// misskey.alice => misskey
// misskey.strawberry.pasta => misskey
2017-06-17 01:37:41 +02:00
// dev.misskey.arisu.tachibana => dev
2017-06-06 19:02:56 +02:00
let app = url.host.split('.')[0];
// Detect the user language
// Note: The default language is English
let lang = navigator.language.split('-')[0];
if (!/^(en|ja)$/.test(lang)) lang = 'en';
// Detect the user agent
const ua = navigator.userAgent.toLowerCase();
const isMobile = /mobile|iphone|ipad|android/.test(ua);
2016-12-31 12:26:22 +01:00
2017-06-06 19:02:56 +02: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 19:00:51 +02:00
meta.setAttribute('content',
'width=device-width,' +
'initial-scale=1,' +
'minimum-scale=1,' +
'maximum-scale=1,' +
'user-scalable=no');
2017-06-06 19:02:56 +02:00
head.appendChild(meta);
}
// Switch desktop or mobile version
if (app == 'misskey') {
app = isMobile ? 'mobile' : 'desktop';
}
// 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');
script.setAttribute('src', `/assets/${app}.${VERSION}.${lang}.js`);
script.setAttribute('async', 'true');
script.setAttribute('defer', 'true');
head.appendChild(script);
}