forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
49 lines (47 loc) · 1.29 KB
/
Copy pathstart.js
File metadata and controls
49 lines (47 loc) · 1.29 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
'use strict';
import { runtime } from './ext.js';
import { err,info,log } from './logger.js';
import { us } from './api.js';
// runtime.onMessage.addListener(onMessage);
const msg = runtime.connect({name:'messenger'}),
win = self ?? window,
doc = win.document;
let config = {};
const inject = async (src,type) => {
let elm;
try {
return await new Promise((resolve, reject) => {
if (!doc) { reject(`Load error for ${src}`) };
if(type.match(/script/g)) {
elm = us.make('script','injected', {
async: true,
src: runtime.getURL(src)
});
} else {
elm = us.make('link','css', {
href: runtime.getURL(src),
rel: 'stylesheet'
});
};
elm.onload = () => resolve(elm);
elm.onerror = () => reject(`Load error for ${src}`);
(doc.head || doc.documentElement || doc).appendChild(elm);
});
} catch (msg) {
return err(msg);
}
};
msg.onMessage.addListener((m) => {
if(!m) return;
if(m.cfg) {
config = m.cfg ?? config;
log('Config:',config);
if(doc.readyState === 'complete') {
inject('web_accessible_resources/magic-userjs.user.js','script');
} else {
win.onload = () => {
inject('web_accessible_resources/magic-userjs.user.js','script');
};
};
};
});