This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
84 lines (57 loc) · 2.01 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const version = '2.0.0';
let css = '';
let styleEl;
const updateCSS = (c) => {
styleEl.innerHTML = '';
styleEl.appendChild(document.createTextNode(c));
};
export default {
goosemodHandlers: {
onImport: async function () {
styleEl = document.createElement('style');
document.head.appendChild(styleEl);
// Setup ace
eval(await (await fetch(`https://ajaxorg.github.io/ace-builds/src-min-noconflict/ace.js`)).text()); // Load Ace main
eval(await (await fetch(`https://ajaxorg.github.io/ace-builds/src-min-noconflict/theme-monokai.js`)).text()); // Load Monokai theme
eval(await (await fetch(`https://ajaxorg.github.io/ace-builds/src-min-noconflict/mode-css.js`)).text()); // Load CSS lang
goosemodScope.settings.createItem('Custom CSS', [
`(v${version})`,
{
type: 'custom',
element: () => {
const el = document.createElement('div');
el.id = 'gm-editor';
el.style.width = '90%';
el.style.height = '85vh';
el.innerHTML = css;
el.className = '';
(async function() {
await sleep(10);
const editor = ace.edit('gm-editor');
const session = editor.getSession();
session.setUseWorker(false); // Tell Ace not to use Workers
session.setMode('ace/mode/css'); // Set lang to CSS
editor.setTheme('ace/theme/monokai'); // Set theme to Monokai
session.on('change', () => {
const val = session.getValue();
css = val;
updateCSS(val);
});
})();
return el;
}
}
]);
},
onRemove: async function () {
styleEl.remove();
goosemodScope.settings.removeItem('Custom CSS');
},
getSettings: () => [css],
loadSettings: ([_css]) => {
css = _css; // Update internal var
updateCSS(css); // Update actual style
},
}
};