This repository was archived by the owner on Dec 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (44 loc) · 1.41 KB
/
app.js
File metadata and controls
57 lines (44 loc) · 1.41 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
const __PATCH__ = `chrome-extension://${chrome.runtime.id}`;
const __LIBS__ = `${__PATCH__}/libs`;
const __SCRIPT__ = `${__PATCH__}/scripts/${location.host}`;
const request = ({ url, type = 'text' }) => (
new Promise(resolve => {
fetch(url)
.then(res => resolve(res[type]()))
})
)
const getFileTypeByFileName = ({ fileName }) => {
let type;
if (fileName.endsWith('.css')) {
type = 'style';
} else {
type = 'script';
}
return type
}
const appendScript = ({ fileName, data, type = getFileTypeByFileName({ fileName }) }) => {
let el = document.createElement(type);
el.innerHTML = data;
document.head.appendChild(el);
}
!(async function() {
let config = await request({ url: `${__PATCH__}/config.json`, type: 'json' });
let host = location.hostname;
let files = config.files[host];
if (files === undefined) {
return;
}
console.log(`%cЗагружаю...`, 'color: white');
files.forEach(async fileName => {
let url, data, type;
if (fileName.startsWith('%')) {
url = config.dict[fileName.slice(1)];
type = 'script';
} else {
url = `${__SCRIPT__}/${fileName}`;
}
data = await request({ url });
appendScript({ fileName, data, type });
console.log(`Загрузил %c${fileName.replace('%', '')}`, 'color: lime');
});
})()