-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed-react-build.js
More file actions
62 lines (52 loc) · 1.64 KB
/
embed-react-build.js
File metadata and controls
62 lines (52 loc) · 1.64 KB
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
function EmbedReactBuild() {
this.url = '';
this.dependencies = [];
this.setURL = function(url) {
this.url = url;
}
this.setDependencies = function(dep) {
this.dependencies.push(...dep);
return dep;
}
this._fetchDependencies = function() {
return fetch(this.url + '.vite/manifest.json')
.then(res => res.json())
.then(res => {
this.setDependencies([res['index.html'].file]);
this.setDependencies(res['index.html'].css);
}
)
}
this._injectDependencies = function() {
const url = this.url;
this.dependencies.forEach((dep) => {
const ext = dep.split(".").slice(-1)[0];
switch(ext) {
case 'css': {
let link = document.createElement('link');
link.rel = "stylesheet";
link.href = url + dep;
document.head.appendChild(link);
break;
}
case 'js': {
let script = document.createElement('script');
script.src = url + dep;
script.type = "module";
document.body.appendChild(script);
break;
}
default:
}
})
}
this.run = function() {
return this._fetchDependencies()
.then(() => this._injectDependencies());
}
}
window.onload = (event) => {
const embed = new EmbedReactBuild()
embed.setURL("https://ownercommunity-ui.pages.dev/")
embed.run()
};