-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-manager.js
87 lines (78 loc) · 3.12 KB
/
theme-manager.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
class ThemeManager {
constructor(githubUsername, githubRepo) {
this.githubUsername = githubUsername;
this.githubRepo = githubRepo;
this.currentTheme = 'default';
this.themeLink = document.createElement('link');
this.themeLink.rel = 'stylesheet';
document.head.appendChild(this.themeLink);
this.themes = [];
}
async fetchThemes() {
const apiUrl = `https://api.github.com/repos/${this.githubUsername}/${this.githubRepo}/contents`;
try {
const response = await fetch(apiUrl);
const files = await response.json();
const themeFiles = files.filter(file => file.name.startsWith('theme-') && file.name.endsWith('.css'));
if (themeFiles.length > 0) {
this.themes = themeFiles.map(file => file.name.replace('theme-', '').replace('.css', ''));
this.addThemeSelector();
await this.applyTheme(this.currentTheme);
}
} catch (error) {
console.error('Error fetching themes:', error);
}
}
async applyTheme(themeName) {
if (!this.themes.includes(themeName)) {
console.error(`Theme ${themeName} not found`);
return;
}
const themeUrl = `https://raw.githubusercontent.com/${this.githubUsername}/${this.githubRepo}/main/theme-${themeName}.css`;
try {
const response = await fetch(themeUrl);
if (response.ok) {
const css = await response.text();
this.themeLink.href = 'data:text/css;charset=UTF-8,' + encodeURIComponent(css);
this.currentTheme = themeName;
localStorage.setItem('selectedTheme', themeName);
if (this.selector) {
this.selector.value = themeName;
}
} else {
console.error(`Failed to load theme: ${themeName}`);
}
} catch (error) {
console.error(`Error applying theme ${themeName}:`, error);
}
}
addThemeSelector() {
this.selector = document.createElement('select');
this.selector.id = 'theme-selector';
this.themes.forEach(theme => {
const option = document.createElement('option');
option.value = theme;
option.textContent = theme.charAt(0).toUpperCase() + theme.slice(1);
this.selector.appendChild(option);
});
this.selector.value = this.currentTheme;
this.selector.addEventListener('change', (e) => this.applyTheme(e.target.value));
const label = document.createElement('label');
label.htmlFor = 'theme-selector';
label.textContent = 'Theme: ';
const container = document.createElement('div');
container.id = 'theme-container';
container.appendChild(label);
container.appendChild(this.selector);
document.body.insertBefore(container, document.body.firstChild);
}
async initialize() {
await this.fetchThemes();
const savedTheme = localStorage.getItem('selectedTheme');
if (savedTheme && this.themes.includes(savedTheme)) {
await this.applyTheme(savedTheme);
} else if (this.themes.length > 0) {
await this.applyTheme(this.themes[0]);
}
}
}