forked from xthezealot/white-theme-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitions-scrapper.themes.vscode.one.js
89 lines (77 loc) · 2.46 KB
/
definitions-scrapper.themes.vscode.one.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
85
86
87
88
89
(async () => {
// Extracts vscode color definitions from https://themes.vscode.one
const definitions = await getDefinitions();
if (definitions !== null) {
try {
const config = transformToDefinitionsConfig(definitions);
outputConfig(config);
} catch (error) {
console.error('Error when generating config from definitions.', error);
}
} else {
console.info('Definitions missing. Exiting...');
}
async function getDefinitions() {
const scrapper = new Scrapper();
try {
return await scrapper.extractDefinitions();
} catch (error) {
console.error('Error when extracting definitions.', error);
}
return null;
}
function transformToDefinitionsConfig(definitions) {
return definitions
.reduce((lines, group, index) => {
const { name, components } = group;
lines.push(`#${name}`);
Object.entries(components).forEach((component) => {
const [key, value] = component;
lines.push(`"${key}": "${value}",`);
});
const shouldAddNewLine = index === definitions.length - 1;
if (shouldAddNewLine) {
lines.push('');
}
return lines;
}, [])
.join('\n');
}
function outputConfig(config) {
console.log(config);
}
function Scrapper() {
function expandAllSettingGroups() {
document
.querySelectorAll('.setting-group:not(.expanded) .header.noselect')
.forEach((e) => e.click());
}
function extractExpandedDefinitions() {
return [...document.querySelectorAll('.setting-group.expanded')].map(
(settingGroup) => {
const name = settingGroup.querySelector(
'.header.noselect .name span:not(.count-badge)'
).textContent;
const components = Object.fromEntries(
[...settingGroup.querySelectorAll('.setting')].map((setting) => {
const info = setting.querySelector('.color-info');
const key = info.querySelector('.name').textContent;
const value = info.querySelector('.code').textContent;
return [key, value];
})
);
return { name, components };
}
);
}
async function extractDefinitions() {
expandAllSettingGroups();
await wait(0);
return extractExpandedDefinitions();
}
return { extractDefinitions };
}
function wait(delay) {
return new Promise((r) => setTimeout(r, delay));
}
})();