-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
253 lines (222 loc) · 8.01 KB
/
Copy pathpopup.js
File metadata and controls
253 lines (222 loc) · 8.01 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Get DOM elements
const initBtn = document.getElementById('initBtn');
const deinitBtn = document.getElementById('deinitBtn');
const initStatus = document.getElementById('initStatus');
const ttlInput = document.getElementById('ttl');
const photoUrl = document.getElementById('photoUrl');
const sendBtn = document.getElementById('sendBtn');
const statusDiv = document.getElementById('status');
const quickOptions = document.querySelectorAll('.quick-option');
// Check initialization status on load
checkInitStatus();
// Auto-refresh status every 2 seconds while popup is open
setInterval(() => {
checkInitStatus();
}, 2000);
// Handle deinitialization button
deinitBtn.addEventListener('click', async () => {
try {
deinitBtn.disabled = true;
initStatus.textContent = 'Deinitializing...';
initStatus.className = 'init-status';
initStatus.style.display = 'block';
// Query the active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// Check if we're on Telegram Web
if (!tab.url || !tab.url.includes('web.telegram.org')) {
initStatus.textContent = '❌ Please open Telegram Web (web.telegram.org/a)';
initStatus.className = 'init-status';
deinitBtn.disabled = false;
return;
}
// Send message to content script to deinitialize
chrome.tabs.sendMessage(tab.id, {
action: 'deinitializeExtension'
}, (response) => {
if (chrome.runtime.lastError) {
initStatus.textContent = '❌ Please reload Telegram Web page and try again';
initStatus.className = 'init-status';
deinitBtn.disabled = false;
console.error('Connection error:', chrome.runtime.lastError);
return;
}
if (response && response.success) {
initStatus.textContent = '✅ Extension deinitialized! Reload to re-patch.';
initStatus.className = 'init-status';
initBtn.disabled = false;
localStorage.removeItem('extension_initialized');
} else {
initStatus.textContent = '❌ ' + (response?.error || 'Deinitialization failed');
initStatus.className = 'init-status';
}
deinitBtn.disabled = false;
});
} catch (error) {
initStatus.textContent = '❌ Error: ' + error.message;
initStatus.className = 'init-status';
deinitBtn.disabled = false;
}
});
// Handle initialization button
initBtn.addEventListener('click', async () => {
try {
initBtn.disabled = true;
initStatus.textContent = 'Initializing...';
initStatus.className = 'init-status';
initStatus.style.display = 'block';
// Query the active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// Check if we're on Telegram Web
if (!tab.url || !tab.url.includes('web.telegram.org')) {
initStatus.textContent = '❌ Please open Telegram Web (web.telegram.org/a)';
initStatus.className = 'init-status';
initBtn.disabled = false;
return;
}
// Send message to content script to initialize
chrome.tabs.sendMessage(tab.id, {
action: 'initializeExtension'
}, (response) => {
if (chrome.runtime.lastError) {
initStatus.textContent = '❌ Please reload Telegram Web page and try again';
initStatus.className = 'init-status';
initBtn.disabled = false;
console.error('Connection error:', chrome.runtime.lastError);
return;
}
if (response && response.success) {
initStatus.textContent = '✅ Extension initialized! Please reload the page.';
initStatus.className = 'init-status initialized';
localStorage.setItem('extension_initialized', 'true');
} else {
initStatus.textContent = '❌ ' + (response?.error || 'Initialization failed');
initStatus.className = 'init-status';
initBtn.disabled = false;
}
});
} catch (error) {
initStatus.textContent = '❌ Error: ' + error.message;
initStatus.className = 'init-status';
initBtn.disabled = false;
}
});
// Check if extension is already initialized
async function checkInitStatus() {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab.url || !tab.url.includes('web.telegram.org')) {
initStatus.textContent = '⚠️ Open Telegram Web (web.telegram.org/a)';
initStatus.style.display = 'block';
initBtn.disabled = true;
deinitBtn.disabled = true;
return;
}
// Check with content script
chrome.tabs.sendMessage(tab.id, {
action: 'checkInitStatus'
}, (response) => {
if (chrome.runtime.lastError) {
// Content script not loaded - page needs reload
initStatus.textContent = '⚠️ Please reload Telegram Web page first';
initStatus.style.display = 'block';
initBtn.disabled = true;
deinitBtn.disabled = true;
return;
}
if (response && response.initialized) {
initStatus.textContent = '✅ Extension is initialized';
initStatus.className = 'init-status initialized';
initStatus.style.display = 'block';
initBtn.disabled = true;
deinitBtn.disabled = false;
} else {
initStatus.textContent = 'Click to initialize extension';
initStatus.style.display = 'block';
initBtn.disabled = false;
deinitBtn.disabled = true;
}
});
} catch (error) {
console.error('Error checking init status:', error);
initStatus.textContent = '❌ Error checking status';
initStatus.style.display = 'block';
}
}
// Handle quick TTL options
quickOptions.forEach(option => {
option.addEventListener('click', () => {
const ttl = option.getAttribute('data-ttl');
ttlInput.value = ttl;
// Update active state
quickOptions.forEach(opt => opt.classList.remove('active'));
option.classList.add('active');
});
});
// Handle photo URL input
photoUrl.addEventListener('input', (e) => {
const url = e.target.value.trim();
if (url) {
sendBtn.disabled = false;
} else {
sendBtn.disabled = true;
}
});
// Handle send button click
sendBtn.addEventListener('click', async () => {
const url = photoUrl.value.trim();
if (!url) {
showStatus('Please enter a photo URL', 'error');
return;
}
const ttl = parseInt(ttlInput.value);
if (isNaN(ttl) || ttl < 1 || ttl > 60) {
showStatus('TTL must be between 1 and 60 seconds', 'error');
return;
}
try {
sendBtn.disabled = true;
showStatus('Sending photo...', 'info');
// Query the active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// Check if we're on Telegram Web
if (!tab.url || !tab.url.includes('web.telegram.org')) {
showStatus('Please open Telegram Web (web.telegram.org/a)', 'error');
sendBtn.disabled = false;
return;
}
// Send message to content script to execute the function
chrome.tabs.sendMessage(tab.id, {
action: 'sendDisappearingPhoto',
ttl: ttl,
imageUrl: url
}, (response) => {
if (chrome.runtime.lastError) {
showStatus('Please reload Telegram Web page and try again', 'error');
sendBtn.disabled = false;
console.error('Connection error:', chrome.runtime.lastError);
return;
}
if (response && response.success) {
showStatus(`✅ Photo sent successfully! (${ttl}s TTL)`, 'success');
// Reset after success
setTimeout(() => {
photoUrl.value = '';
sendBtn.disabled = true;
statusDiv.style.display = 'none';
}, 2000);
} else {
showStatus('Error: ' + (response?.error || 'Unknown error'), 'error');
sendBtn.disabled = false;
}
});
} catch (error) {
showStatus('Error: ' + error.message, 'error');
sendBtn.disabled = false;
}
});
// Helper function to show status messages
function showStatus(message, type) {
statusDiv.textContent = message;
statusDiv.className = 'status ' + type;
statusDiv.style.display = 'block';
}