-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
168 lines (142 loc) · 5.25 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
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
// Keep track of current items and settings
let currentItems = [];
let currentSettings = {};
let retryCount = 0;
const MAX_RETRIES = 3;
// Function to check if we can connect to the tab
async function canConnectToTab(tabId) {
try {
await chrome.tabs.sendMessage(tabId, { type: 'ping' });
return true;
} catch (error) {
return false;
}
}
// Function to create a toggle switch for an item
function createToggleSwitch(itemName, checked) {
const option = document.createElement('div');
option.className = 'option';
const label = document.createElement('span');
label.className = 'label';
label.textContent = itemName;
const switchLabel = document.createElement('label');
switchLabel.className = 'switch';
const input = document.createElement('input');
input.type = 'checkbox';
input.id = itemName;
input.checked = checked;
// Add change listener to individual toggle
input.addEventListener('change', () => {
saveSettings({
...currentSettings,
[itemName]: input.checked
});
});
const slider = document.createElement('span');
slider.className = 'slider';
switchLabel.appendChild(input);
switchLabel.appendChild(slider);
option.appendChild(label);
option.appendChild(switchLabel);
return option;
}
// Function to update the popup with sidebar items
function updatePopup(items, savedSettings) {
currentItems = items;
currentSettings = savedSettings;
const container = document.getElementById('options-container');
container.innerHTML = ''; // Clear existing options
if (items.length === 0) {
container.innerHTML = '<div class="loading">Loading items... Please wait.</div>';
return;
}
items.forEach(item => {
const isChecked = savedSettings[item] || false;
const toggle = createToggleSwitch(item, isChecked);
container.appendChild(toggle);
});
}
// Function to save settings with retry
async function saveSettings(settings) {
try {
currentSettings = settings;
// Save to storage
await chrome.storage.sync.set(settings);
// Show save message
const saveMessage = document.getElementById('saveMessage');
saveMessage.classList.add('visible');
// Hide message after 2 seconds
setTimeout(() => {
saveMessage.classList.remove('visible');
}, 2000);
// Send message to content script to update
const tabs = await chrome.tabs.query({active: true, currentWindow: true});
if (tabs[0]) {
try {
await chrome.tabs.sendMessage(tabs[0].id, {
type: 'settingsUpdated',
settings: settings
});
} catch (error) {
console.warn('Could not update content script, will apply on next page load:', error);
}
}
} catch (error) {
console.error('Error saving settings:', error);
// Show error message
const saveMessage = document.getElementById('saveMessage');
saveMessage.textContent = 'Error saving settings';
saveMessage.style.color = '#ff4444';
saveMessage.classList.add('visible');
setTimeout(() => {
saveMessage.classList.remove('visible');
saveMessage.textContent = 'Settings saved!';
saveMessage.style.color = '';
}, 2000);
}
}
// Function to request sidebar items with retry
async function requestSidebarItems() {
try {
const tabs = await chrome.tabs.query({active: true, currentWindow: true});
if (!tabs[0]) {
throw new Error('No active tab found');
}
const canConnect = await canConnectToTab(tabs[0].id);
if (!canConnect) {
throw new Error('Cannot connect to tab');
}
await chrome.tabs.sendMessage(tabs[0].id, {
type: 'getSidebarItems'
});
} catch (error) {
console.error('Error requesting sidebar items:', error);
if (retryCount < MAX_RETRIES) {
retryCount++;
setTimeout(requestSidebarItems, 1000);
} else {
const container = document.getElementById('options-container');
container.innerHTML = '<div class="loading">Could not load items. Please refresh Twitter/X and try again.</div>';
}
}
}
// Initialize popup
document.addEventListener('DOMContentLoaded', async () => {
try {
// Get saved settings first
const savedSettings = await chrome.storage.sync.get(null);
currentSettings = savedSettings || {};
// Request sidebar items from content script
await requestSidebarItems();
} catch (error) {
console.error('Error initializing popup:', error);
const container = document.getElementById('options-container');
container.innerHTML = '<div class="loading">Error loading settings. Please try again.</div>';
}
});
// Listen for sidebar items from content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'sidebarItems') {
updatePopup(message.items, currentSettings);
}
});