-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
621 lines (532 loc) · 22.2 KB
/
Copy pathpopup.js
File metadata and controls
621 lines (532 loc) · 22.2 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Load environment variables
// API endpoints
const API_BASE = 'http://localhost:5001';
const API_ENDPOINTS = {
LOGIN: `${API_BASE}/auth/login`,
SIGNUP: `${API_BASE}/auth/signup`,
POINTS: `${API_BASE}/points`,
POINTS_ADD: `${API_BASE}/points/add`,
FORGOT_PASSWORD: `${API_BASE}/auth/forgot-password`,
RESET_PASSWORD: `${API_BASE}/auth/reset-password`
};
// Stardust Animation
function createStardustAnimation() {
const container = document.querySelector('.stardust-container');
const numberOfStars = 50;
let animationRunning = false;
let starInterval = null;
let positions = Array.from({length: 10}, (_, i) => i * 10);
function createStar() {
if (!animationRunning) return;
const star = document.createElement('div');
star.className = 'stardust';
// Get a random position zone and add some randomness within it
const zoneIndex = Math.floor(Math.random() * positions.length);
const basePosition = positions[zoneIndex];
const position = basePosition + Math.random() * 10;
star.style.left = `${position}%`;
// Randomize fall duration slightly for natural effect
const fallDuration = 2.5 + Math.random() * 0.8;
star.style.animationDuration = `${fallDuration}s`;
container.appendChild(star);
// Remove star after animation completes
setTimeout(() => {
if (star && star.parentNode) {
star.remove();
}
}, fallDuration * 1000);
}
function startAnimation() {
if (animationRunning) return;
animationRunning = true;
container.style.opacity = '1';
// Create stars gradually
starInterval = setInterval(() => {
if (container.childNodes.length < numberOfStars) {
createStar();
}
}, 100);
}
function stopAnimation() {
animationRunning = false;
if (starInterval) {
clearInterval(starInterval);
starInterval = null;
}
}
// Add mouse enter/leave listeners
document.body.addEventListener('mouseenter', startAnimation);
document.body.addEventListener('mouseleave', stopAnimation);
// Handle visibility changes
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
stopAnimation();
container.innerHTML = '';
}
});
// Cleanup function
return function cleanup() {
document.body.removeEventListener('mouseenter', startAnimation);
document.body.removeEventListener('mouseleave', stopAnimation);
stopAnimation();
container.innerHTML = '';
};
}
// DOM Elements
const loginForm = document.getElementById('loginForm');
const signupForm = document.getElementById('signupForm');
const mainContent = document.getElementById('mainContent');
const forgotPasswordForm = document.getElementById('forgotPasswordForm');
const resetPasswordForm = document.getElementById('resetPasswordForm');
const pointsValue = document.getElementById('pointsValue');
const statusDiv = document.getElementById('status');
const checkButton = document.getElementById('checkButton');
// Helper Functions
function showStatus(message, isError = false) {
const statusDiv = document.getElementById('status');
const statusContent = statusDiv.querySelector('.status-content');
// Clear any existing timeout
if (window.statusTimeout) {
clearTimeout(window.statusTimeout);
}
// Update content and styling
statusContent.textContent = message;
statusDiv.className = isError ? 'error' : 'success';
// Show the status
statusDiv.style.display = 'flex';
// Auto hide after 8 seconds
window.statusTimeout = setTimeout(() => {
if (statusDiv.style.display === 'flex') {
statusDiv.style.display = 'none';
}
}, 8000);
}
// Add click handler for close button
document.querySelector('.status-close').addEventListener('click', () => {
const statusDiv = document.getElementById('status');
statusDiv.style.display = 'none';
// Clear any existing timeout on manual close
if (window.statusTimeout) {
clearTimeout(window.statusTimeout);
}
});
function showSection(section) {
console.log(`Showing section: ${section}`);
loginForm.style.display = 'none';
signupForm.style.display = 'none';
mainContent.style.display = 'none';
forgotPasswordForm.style.display = 'none';
resetPasswordForm.style.display = 'none';
if (section === 'login') loginForm.style.display = 'block';
else if (section === 'signup') signupForm.style.display = 'block';
else if (section === 'main') mainContent.style.display = 'block';
else if (section === 'forgotPassword') forgotPasswordForm.style.display = 'block';
else if (section === 'resetPassword') resetPasswordForm.style.display = 'block';
}
// Function to handle points addition
async function handlePointsAdd(authToken, points) {
console.log('Attempting to add points:', points);
try {
console.log('Making request to:', API_ENDPOINTS.POINTS_ADD);
const response = await fetch(API_ENDPOINTS.POINTS_ADD, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
},
body: JSON.stringify({ points: points })
});
console.log('Points add response status:', response.status);
if (!response.ok) {
const errorData = await response.json();
console.error('Points add error response:', errorData);
throw new Error(errorData.message || `HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log('Points add success response:', data);
if (data.points !== undefined) {
pointsValue.textContent = data.points;
return data;
} else {
throw new Error('No points value in response');
}
} catch (error) {
console.error('Points addition error:', error);
throw new Error(`Failed to add points: ${error.message}`);
}
}
// Function to check current page for affiliate ID
async function checkCurrentPage() {
console.log('Checking current page for affiliate ID');
chrome.tabs.query({active: true, currentWindow: true}, async function(tabs) {
try {
if (!tabs[0]) {
console.error('No active tab found');
showStatus('No active tab found', true);
return;
}
const currentTab = tabs[0];
const isAmazonPage = currentTab.url.includes('amazon.');
if (!isAmazonPage) {
showStatus('This is not an Amazon page', true);
return;
}
// Try to inject content script if not already present
try {
await chrome.scripting.executeScript({
target: { tabId: currentTab.id },
files: ['config.js', 'content.js']
});
console.log('Content scripts injected');
} catch (error) {
console.log('Content scripts already present or injection failed:', error);
}
// Wait a bit for content script to initialize
await new Promise(resolve => setTimeout(resolve, 500));
console.log('Sending message to content script');
const response = await chrome.tabs.sendMessage(currentTab.id, {
action: 'checkReferral',
associateId: window.appConfig.AMAZON_ASSOCIATE_ID
});
console.log('Content script response:', response);
if (response && response.hasReferral) {
// Get auth token and award points
chrome.storage.local.get(['authToken'], async function(result) {
console.log('Retrieved auth token:', result.authToken ? 'exists' : 'missing');
if (result.authToken) {
try {
await handlePointsAdd(result.authToken, 1);
showStatus('Success! Referral active - Added 1 point', false);
await loadUserData(); // Refresh points display
} catch (error) {
console.error('Error adding points:', error);
showStatus(error.message, true);
}
} else {
showStatus('Please log in to add points', true);
}
});
} else {
showStatus('This page does not have your referral link', true);
}
} catch (error) {
console.error('Error checking referral link:', error);
if (error.message.includes('Receiving end does not exist')) {
showStatus('Please refresh the page and try again', true);
} else {
showStatus('Error checking referral link. Is this an Amazon page?', true);
}
}
});
}
// Authentication Functions
async function login() {
console.log('Attempting login');
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
try {
console.log('Making login request to:', API_ENDPOINTS.LOGIN);
const response = await fetch(API_ENDPOINTS.LOGIN, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
console.log('Login response status:', response.status);
const data = await response.json();
if (response.ok) {
console.log('Login successful');
chrome.storage.local.set({ authToken: data.token });
showSection('main');
loadUserData();
updateAssociateId();
} else {
console.error('Login failed:', data);
showStatus(data.message || 'Invalid email or password', true);
}
} catch (error) {
console.error('Network error during login:', error);
showStatus('Network error. Please try again.', true);
}
}
async function signup() {
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
try {
const response = await fetch(API_ENDPOINTS.SIGNUP, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
const data = await response.json();
if (response.ok) {
showStatus('Account created! Please login.');
showSection('login');
} else {
showStatus(data.message || 'Signup failed', true);
}
} catch (error) {
showStatus('Network error', true);
}
}
// Password Reset Functions
async function requestPasswordReset() {
const email = document.getElementById('forgotEmail').value;
const resetButton = document.getElementById('forgotPasswordBtn');
const originalText = resetButton.textContent;
try {
resetButton.textContent = 'Loading...';
resetButton.disabled = true;
const response = await fetch(API_ENDPOINTS.FORGOT_PASSWORD, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email })
});
const data = await response.json();
if (response.ok) {
showSection('login');
// Show status message after changing section
requestAnimationFrame(() => {
showStatus('Password reset link has been sent to your email');
});
} else {
showStatus(data.message || 'Failed to send reset link', true);
}
} catch (error) {
showStatus('Network error', true);
} finally {
resetButton.textContent = originalText;
resetButton.disabled = false;
}
}
async function resetPassword() {
const newPassword = document.getElementById('newPassword').value;
const confirmPassword = document.getElementById('confirmPassword').value;
if (newPassword !== confirmPassword) {
showStatus('Passwords do not match', true);
return;
}
try {
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
const response = await fetch(API_ENDPOINTS.RESET_PASSWORD, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token, new_password: newPassword })
});
const data = await response.json();
if (response.ok) {
showStatus('Password reset successful');
showSection('login');
} else {
showStatus(data.message || 'Failed to reset password', true);
}
} catch (error) {
showStatus('Network error', true);
}
}
function logout() {
chrome.storage.local.remove(['authToken'], function() {
showSection('login');
});
}
// Update Associate ID from config
function updateAssociateId() {
const associateId = window.appConfig.AMAZON_ASSOCIATE_ID;
chrome.storage.local.set({ associateId }, function() {
console.log('Associate ID updated from config:', associateId);
});
}
// Points Management
async function loadUserData() {
try {
const token = await new Promise(resolve => {
chrome.storage.local.get(['authToken'], result => resolve(result.authToken));
});
if (!token) return;
const response = await fetch(API_ENDPOINTS.POINTS, {
headers: { 'Authorization': `Bearer ${token}` }
});
if (response.ok) {
const data = await response.json();
pointsValue.textContent = data.points;
// Get the current user's email from the JWT token
const tokenData = JSON.parse(atob(token.split('.')[1]));
const userEmail = tokenData.sub; // JWT standard uses 'sub' for subject (user identifier)
document.getElementById('userEmail').textContent = userEmail;
// Load wallet address right after loading points
await loadWalletAddress();
}
} catch (error) {
console.error('Error loading user data:', error);
}
}
// Format wallet address for display (4tXX...XNDn format)
function formatWalletAddress(address) {
if (!address) return '';
return `${address.slice(0, 4)}...${address.slice(-4)}`;
}
// Load wallet address on startup
async function loadWalletAddress() {
try {
const token = await new Promise(resolve => {
chrome.storage.local.get(['authToken'], result => resolve(result.authToken));
});
if (!token) return;
const response = await fetch(`${API_BASE}/wallet`, {
headers: { 'Authorization': `Bearer ${token}` }
});
if (response.ok) {
const data = await response.json();
const walletInput = document.getElementById('walletAddress');
const saveWalletBtn = document.getElementById('saveWalletBtn');
if (data.wallet) {
walletInput.dataset.fullAddress = data.wallet; // Store full address
walletInput.value = formatWalletAddress(data.wallet); // Show truncated
walletInput.disabled = true;
saveWalletBtn.textContent = 'Change Wallet';
} else {
walletInput.disabled = false;
walletInput.dataset.fullAddress = '';
saveWalletBtn.textContent = 'Save Wallet';
}
}
} catch (error) {
console.error('Error loading wallet address:', error);
}
}
// Save wallet address
async function saveWalletAddress() {
const walletInput = document.getElementById('walletAddress');
const saveWalletBtn = document.getElementById('saveWalletBtn');
// If wallet is disabled and button says "Change Wallet", enable editing
if (walletInput.disabled && saveWalletBtn.textContent === 'Change Wallet') {
walletInput.disabled = false;
walletInput.value = walletInput.dataset.fullAddress || ''; // Show full address for editing
walletInput.focus();
saveWalletBtn.textContent = 'Save Wallet';
return;
}
const walletAddress = walletInput.value.trim();
// Basic Solana address validation (should be 32-44 characters)
if (!walletAddress.match(/^[1-9A-HJ-NP-Za-km-z]{32,44}$/)) {
showStatus('Please enter a valid Solana wallet address', true);
return;
}
try {
const token = await new Promise(resolve => {
chrome.storage.local.get(['authToken'], result => resolve(result.authToken));
});
if (!token) {
showStatus('Please log in first', true);
return;
}
const response = await fetch(`${API_BASE}/wallet`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ wallet: walletAddress })
});
let data;
try {
data = await response.json();
} catch (e) {
console.error('Failed to parse response:', e);
showStatus('Server error. Please try again later.', true);
return;
}
if (response.ok) {
showStatus('Wallet address saved successfully');
walletInput.dataset.fullAddress = walletAddress; // Store full address
walletInput.value = formatWalletAddress(walletAddress); // Show truncated
walletInput.disabled = true;
saveWalletBtn.textContent = 'Change Wallet';
} else {
showStatus(data?.message || 'Failed to save wallet address', true);
// If error is "User not found or no changes made", maintain the view-only state
if (data?.message === 'User not found or no changes made') {
walletInput.value = formatWalletAddress(walletInput.dataset.fullAddress); // Revert to truncated
walletInput.disabled = true;
saveWalletBtn.textContent = 'Change Wallet';
}
}
} catch (error) {
console.error('Error saving wallet address:', error);
showStatus('Network error while saving wallet address. Please check your connection.', true);
}
}
// Event Listeners
document.addEventListener('DOMContentLoaded', function() {
// Check authentication status
chrome.storage.local.get(['authToken'], function(result) {
if (result.authToken) {
showSection('main');
loadUserData();
loadWalletAddress();
} else {
showSection('login');
}
});
// Add wallet copy functionality
let copyTimeout = null;
const walletGroup = document.querySelector('.wallet-group');
walletGroup.addEventListener('click', function() {
const walletInput = document.getElementById('walletAddress');
const fullAddress = walletInput.dataset.fullAddress;
// Only proceed if we have a wallet address and input is disabled (view mode)
if (!fullAddress || !walletInput.disabled) {
return;
}
// Clear any existing timeout
if (copyTimeout) {
clearTimeout(copyTimeout);
copyTimeout = null;
}
// Copy to clipboard
navigator.clipboard.writeText(fullAddress).then(function() {
// Store current state
const originalText = walletInput.value;
const originalColor = walletInput.style.color;
// Show copied state
walletInput.value = 'Copied!';
walletInput.style.color = 'var(--accent-primary)';
// Set timeout to revert
copyTimeout = setTimeout(function() {
if (walletInput) {
walletInput.value = originalText;
walletInput.style.color = originalColor;
}
copyTimeout = null;
}, 3000);
}).catch(function(err) {
console.error('Failed to copy:', err);
showStatus('Failed to copy to clipboard', true);
});
});
// Login form event listeners
document.getElementById('loginBtn')?.addEventListener('click', login);
document.getElementById('signupBtn')?.addEventListener('click', signup);
document.getElementById('showSignup')?.addEventListener('click', () => showSection('signup'));
document.getElementById('showLogin')?.addEventListener('click', () => showSection('login'));
document.getElementById('showForgotPassword')?.addEventListener('click', () => showSection('forgotPassword'));
document.getElementById('forgotPasswordBtn')?.addEventListener('click', requestPasswordReset);
document.getElementById('resetPasswordBtn')?.addEventListener('click', resetPassword);
document.getElementById('logoutButton')?.addEventListener('click', logout);
// Add event listener for page check button
document.getElementById('checkButton')?.addEventListener('click', checkCurrentPage);
// Add wallet save button listener
document.getElementById('saveWalletBtn')?.addEventListener('click', saveWalletAddress);
// Add event listener for the "Back to Login" link in forgot password form
document.querySelector('#forgotPasswordForm a[href="#"]')?.addEventListener('click', (e) => {
e.preventDefault();
showSection('login');
});
createStardustAnimation();
});
// Listen for points updates from background script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.type === 'POINTS_UPDATED' && message.points !== undefined) {
pointsValue.textContent = message.points;
}
});