-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
189 lines (160 loc) · 8.6 KB
/
settings.js
File metadata and controls
189 lines (160 loc) · 8.6 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(function () {
let observer = null;
function injectMaytchaOption() {
// Double check we are on the right page to avoid unnecessary DOM queries
if (!location.pathname.includes('/settings/appearance')) return;
const appearanceForm = document.querySelector('#appearance-form');
const themeModeHeading = document.getElementById('theme-mode-heading');
if (appearanceForm && themeModeHeading) {
// Check if already injected
if (document.getElementById('gitstyles-settings-container')) return;
// Identify native section elements to toggle
const dropdown = document.getElementById('color_mode_type_select');
const themeModeDescription = document.getElementById('theme-mode-description');
const dropdownContainer = dropdown ? dropdown.closest('.d-flex') : null;
const modePanels = document.querySelectorAll('[data-targets="appearance-form.modePanels"]');
const contrastSection = document.querySelector('react-partial[partial-name="appearance-settings-page"]');
const nativeElements = [
themeModeHeading,
dropdown,
themeModeDescription,
dropdownContainer,
...Array.from(modePanels),
contrastSection
].filter(Boolean);
// Define themes
const themes = [
{ id: 'default', name: 'GitHub Default', description: 'Use standard GitHub themes', color: '#f6f8fa' },
{ id: 'maytcha', name: 'Maytcha', description: 'A relaxing matcha green theme', image: 'https://z.hajspace.com/u/JghUHM.png', author: 'maytcha' },
{ id: 'ramune', name: 'Ramune', description: 'A refreshing soda blue theme', color: '#b2d4ff', author: 'maytcha' },
{ id: 'taro', name: 'Taro', description: 'A sweet purple taro theme', color: '#d1c0ff', author: 'maytcha' },
{ id: 'sakura', name: 'Sakura', description: 'A soft pink cherry blossom theme', color: '#ffc2d6', author: 'maytcha' },
{ id: 'honey', name: 'Honey', description: 'A warm yellow honey theme', color: '#ffe899', author: 'maytcha' },
{ id: 'latte', name: 'Latte', description: 'A cozy brown coffee theme', color: '#dccdbd', author: 'maytcha' },
{ id: 'mint', name: 'Mint', description: 'A fresh cyan mint theme', color: '#b2e0d6', author: 'maytcha' },
{ id: 'azuki', name: 'Azuki', description: 'A sweet red bean theme', color: '#ffc7c7', author: 'maytcha' },
{ id: 'catppuccin-macchiato-rose', name: 'Catppuccin Rose', description: 'Macchiato dark theme with rose accents', color: '#24273a', author: 'maytcha' }
];
// 1. Create GitStyles Container
const gitStylesContainer = document.createElement('div');
gitStylesContainer.id = 'gitstyles-settings-container';
gitStylesContainer.className = 'mb-4';
const cardsHtml = themes.map(theme => {
const isDefault = theme.id === 'default';
const previewContent = theme.image
? `<img alt="${theme.name} Preview" src="${theme.image}" style="position: absolute; width: 100%; height: 100%; object-fit: cover; top: 0; left: 0;">`
: `<div style="position: absolute; width: 100%; height: 100%; top: 0; left: 0; background-color: ${theme.color};"></div>`;
const authorHtml = theme.author
? `<span class="text-normal f6 color-fg-muted"> • by ${theme.author}</span>`
: '';
return `
<div class="position-relative mb-3 flex-shrink-0 col-6 col-md-4">
<input class="position-absolute" id="gitstyles-option-${theme.id}" type="radio" name="gitstyles_theme" value="${theme.id}" style="position:absolute;z-index:5;margin-top: calc(52.6315789474% + 4px);left:19px;">
<label class="radio-label pl-0 pr-0 pt-0 pb-2 overflow-hidden color-theme-toggle-label width-full height-full" for="gitstyles-option-${theme.id}">
<div class="mb-2 width-full overflow-hidden border-bottom" style="padding-bottom: 52.63%; position: relative; background-color: ${theme.color || 'transparent'}">
${previewContent}
</div>
<div class="ml-5 pr-2">
<div class="text-bold">${theme.name}${authorHtml}</div>
<div class="f6 color-fg-muted">${theme.description}</div>
</div>
</label>
</div>
`;
}).join('');
gitStylesContainer.innerHTML = `
<h3 class="h5 mb-2">gitstyles</h3>
<div class="d-flex gutter-condensed flex-wrap" role="radiogroup" aria-label="GitStyles theme picker">
${cardsHtml}
</div>
`;
// 2. Insert GitStyles container before the native heading
if (themeModeHeading.parentNode) {
themeModeHeading.parentNode.insertBefore(gitStylesContainer, themeModeHeading);
}
// 3. Logic
const radios = gitStylesContainer.querySelectorAll('input[name="gitstyles_theme"]');
// Helper to enforce base theme
function enforceBaseTheme(themeName) {
if (themeName === 'default') return;
const isDark = themeName === 'catppuccin-macchiato-rose';
const targetOptionId = isDark ? 'option-dark' : 'option-light';
const targetOption = document.getElementById(targetOptionId);
if (targetOption && !targetOption.checked) {
targetOption.click(); // Click to trigger GitHub's internal handlers
}
}
// Helper to toggle native visibility
function updateNativeVisibility(themeName) {
const isDefault = themeName === 'default' || !themeName;
nativeElements.forEach(el => {
if (el) el.style.display = isDefault ? '' : 'none';
});
if (isDefault) {
themeModeHeading.textContent = 'GitHub Themes';
}
}
// Load saved state
chrome.storage.local.get(['theme'], (result) => {
const currentTheme = result.theme || 'default';
const radio = document.getElementById(`gitstyles-option-${currentTheme}`);
if (radio) radio.checked = true;
if (currentTheme !== 'default') {
enforceBaseTheme(currentTheme);
updateNativeVisibility(currentTheme);
} else {
updateNativeVisibility('default');
}
});
// Save state & Enforce
radios.forEach(radio => {
radio.addEventListener('change', (e) => {
if (e.target.checked) {
const val = e.target.value;
const theme = val === 'default' ? null : val;
chrome.storage.local.set({ theme: theme });
if (theme) {
enforceBaseTheme(theme);
updateNativeVisibility(theme);
} else {
updateNativeVisibility('default');
}
}
});
});
}
}
function startObserver() {
if (!observer) {
observer = new MutationObserver(() => {
injectMaytchaOption();
});
observer.observe(document.body, { childList: true, subtree: true });
}
}
function stopObserver() {
if (observer) {
observer.disconnect();
observer = null;
}
}
function handleNavigation() {
if (location.pathname.includes('/settings/appearance')) {
injectMaytchaOption();
startObserver();
} else {
stopObserver();
}
}
// Initial run
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', handleNavigation);
} else {
handleNavigation();
}
// Handle Turbo/SPA navigation
document.addEventListener('turbo:load', handleNavigation);
document.addEventListener('turbo:frame-load', handleNavigation);
// Fallback for other navigation types
window.addEventListener('popstate', handleNavigation);
})();