summaryrefslogtreecommitdiff
path: root/src/web/app/boot.js
blob: 7cfa71df218d3bb11afc0ab42c51f79840ba60a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * MISSKEY BOOT LOADER
 * (ENTRY POINT)
 */

/**
 * ドメインに基づいて適切なスクリプトを読み込みます。
 * ユーザーの言語およびモバイル端末か否かも考慮します。
 * webpackは介さないためrequireやimportは使えません。
 */

'use strict';

// Get the current url information
const Url = new URL(location.href);

// Extarct the (sub) domain part of the current url
//
// e.g.
//   misskey.alice               => misskey
//   misskey.strawberry.pasta    => misskey
//   dev.misskey.alice.tachibana => dev
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);

// 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');
	meta.setAttribute('content', [
		['width', 'device-width'],
		['initial-scale', '1'],
		['minimum-scale', '1'],
		['maximum-scale', '1'],
		['user-scalable', 'no']
	].map(x => x.join('=')).join(','));
	head.appendChild(meta);
}

// Switch desktop or mobile version
if (app == 'misskey') {
	app = isMobile ? 'mobile' : 'desktop';
}

// Load an app script
// Note: 'async' makes can load the script asyncly.
//       'defer' makes can 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);