forked from Nearcyan/BlueRaven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
103 lines (86 loc) · 3.28 KB
/
popup.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
document.addEventListener('DOMContentLoaded', async () => {
const settingsDiv = document.getElementById('settings');
const { settings } = await chrome.storage.sync.get('settings');
// Section order and titles
const sections = [
{ id: 'buttonColors', title: 'Button Colors' },
{ id: 'replaceElements', title: 'UI Elements' },
{ id: 'styleFixes', title: 'Style Fixes' },
{ id: 'hideElements', title: 'Hide Elements' }
];
// Create sections in order
sections.forEach(({ id, title }) => {
if (TWITTER_MODS[id]) {
const sectionDiv = document.createElement('div');
// Add section title
const titleDiv = document.createElement('div');
titleDiv.className = 'section-title';
titleDiv.textContent = title;
sectionDiv.appendChild(titleDiv);
// Add section content
const contentDiv = document.createElement('div');
contentDiv.className = 'mod-section';
// Add toggles for each sub-setting
Object.entries(TWITTER_MODS[id]).forEach(([key, config]) => {
if (typeof config === 'object' && 'enabled' in config) {
const item = createToggle(
`${id}-${key}`,
config.description,
settings?.[id]?.[key]?.enabled ?? config.enabled,
(checked) => updateSetting(id, key, checked)
);
contentDiv.appendChild(item);
}
});
sectionDiv.appendChild(contentDiv);
settingsDiv.appendChild(sectionDiv);
}
});
});
function createToggle(id, label, checked, onChange) {
const div = document.createElement('div');
div.className = 'mod-item';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = id;
checkbox.checked = checked;
checkbox.addEventListener('change', (e) => onChange(e.target.checked));
const labelElement = document.createElement('label');
labelElement.htmlFor = id;
labelElement.textContent = label;
div.appendChild(checkbox);
div.appendChild(labelElement);
return div;
}
async function updateSetting(modType, key, value) {
try {
console.log(`Updating setting: ${modType}.${key} = ${value}`);
const { settings = {} } = await chrome.storage.sync.get('settings');
if (!settings[modType]) settings[modType] = {};
if (!settings[modType][key]) settings[modType][key] = {};
settings[modType][key].enabled = value;
console.log('New settings:', settings);
await chrome.storage.sync.set({ settings });
// Notify content script to refresh
const tabs = await chrome.tabs.query({ url: ['*://twitter.com/*', '*://x.com/*'] });
console.log('Found tabs to update:', tabs);
const updatePromises = tabs.map(tab =>
chrome.tabs.sendMessage(tab.id, {
type: 'refreshTheme',
modType,
key,
value
}).catch(err => console.error(`Failed to update tab ${tab.id}:`, err))
);
await Promise.all(updatePromises);
// Visual feedback
const checkbox = document.getElementById(`${modType}-${key}`);
if (checkbox) {
checkbox.classList.add('updated');
setTimeout(() => checkbox.classList.remove('updated'), 500);
}
} catch (error) {
console.error('Failed to update setting:', error);
alert('Failed to update setting. Check console for details.');
}
}