-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-client.ts
57 lines (51 loc) · 1.37 KB
/
load-client.ts
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
interface Window {
BMB: any
}
(function() {
const l = document.getElementById('bmbclientloader') as HTMLScriptElement
if (!l) {
return
}
const host = l.src.match(/https?:\/\/[^/]*/)[0] || ''
function fetchFiles(callback: (cssFile: string, jsFile: string) => void) {
const req = new XMLHttpRequest()
req.onreadystatechange = function () {
if (req.readyState === XMLHttpRequest.DONE) {
if (req.status === 200) {
const files = JSON.parse(req.responseText).files
const cssFile = files['main.css']
const jsFile = files['main.js']
callback(cssFile, jsFile)
}
}
}
req.open('GET', `${host}/asset-manifest.json`)
req.send()
}
function init() {
const m = l.getAttribute('data-m')
if (m) {
window.BMB.renderFixedClient(m)
}
}
fetchFiles(function(cssFile, jsFile) {
let loadedCount = 0
const head = document.getElementsByTagName('head')[0]
const link = document.createElement('link')
const script = document.createElement('script')
function loaded() {
loadedCount += 1
if (loadedCount === 2) {
init()
}
}
link.rel = 'stylesheet'
link.type = 'text/css'
link.href = cssFile
link.onload = loaded
head.appendChild(link)
script.src = jsFile
script.onload = loaded
head.appendChild(script)
})
})()