-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
167 lines (147 loc) · 5.82 KB
/
popup.js
File metadata and controls
167 lines (147 loc) · 5.82 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
// DOM Elements
document.addEventListener('DOMContentLoaded', () => {
console.log('DOM Content Loaded - Initializing elements...');
// DOM Elements
const tabs = document.querySelectorAll('.tab');
const tabContents = document.querySelectorAll('.tab-content');
const submitBtn = document.getElementById('submit-btn');
const viewFilter = document.getElementById('view-filter');
const opportunitiesList = document.getElementById('opportunities-list');
// Form elements
const opportunityName = document.getElementById('opportunity-name');
const notes = document.getElementById('notes');
const deadline = document.getElementById('deadline');
const link = document.getElementById('link');
console.log('Elements initialized:', {
submitBtn: !!submitBtn,
opportunityName: !!opportunityName,
notes: !!notes,
deadline: !!deadline,
link: !!link
});
// Tab switching
tabs.forEach(tab => {
tab.addEventListener('click', () => {
const targetTab = tab.dataset.tab;
tabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
tabContents.forEach(content => {
content.classList.remove('active');
if (content.id === `${targetTab}-tab`) {
content.classList.add('active');
}
});
if (targetTab === 'view') {
loadOpportunities();
}
});
});
// Save opportunity
submitBtn.addEventListener('click', async (e) => {
e.preventDefault();
console.log('Save button clicked');
console.log('Form values:', {
name: opportunityName.value,
notes: notes.value,
deadline: deadline.value,
link: link.value
});
if (!opportunityName.value) {
showNotification('Please enter an opportunity name', 'error');
return;
}
const opportunity = {
timestamp: new Date().toISOString(),
name: opportunityName.value,
notes: notes.value,
deadline: deadline.value,
link: link.value
};
console.log('Sending opportunity:', opportunity);
try {
console.log('Sending message to background script...');
chrome.runtime.sendMessage({
action: 'saveOpportunity',
opportunity: opportunity
}, response => {
console.log('Received response:', response);
if (chrome.runtime.lastError) {
console.error('Runtime error:', chrome.runtime.lastError);
showNotification('Error: ' + chrome.runtime.lastError.message, 'error');
return;
}
if (response && response.success) {
console.log('Save successful');
clearForm();
showNotification('Opportunity saved successfully!');
if (document.querySelector('.tab[data-tab="view"]').classList.contains('active')) {
loadOpportunities();
}
} else {
console.error('Save failed:', response);
showNotification(response?.error || 'Failed to save opportunity', 'error');
}
});
} catch (error) {
console.error('Error in save process:', error);
showNotification('Error saving opportunity: ' + error.message, 'error');
}
});
// Load opportunities
async function loadOpportunities() {
try {
const response = await chrome.runtime.sendMessage({ action: 'getOpportunities' });
if (chrome.runtime.lastError) {
throw new Error(chrome.runtime.lastError.message);
}
if (response && response.opportunities) {
displayOpportunities(response.opportunities);
} else {
throw new Error('Failed to load opportunities');
}
} catch (error) {
console.error('Error loading opportunities:', error);
showNotification('Error loading opportunities', 'error');
}
}
// Display opportunities
function displayOpportunities(opportunities) {
opportunitiesList.innerHTML = '';
const now = new Date();
opportunities.forEach(opp => {
const item = document.createElement('div');
item.className = 'opportunity-item';
const deadline = opp.deadline ? new Date(opp.deadline).toLocaleDateString() : 'No deadline';
item.innerHTML = `
<h3>${opp.name}</h3>
<p>${opp.notes || ''}</p>
<p class="deadline">Deadline: ${deadline}</p>
${opp.link ? `<p><a href="${opp.link}" target="_blank">${opp.link}</a></p>` : ''}
`;
opportunitiesList.appendChild(item);
});
}
// Clear form
function clearForm() {
opportunityName.value = '';
notes.value = '';
deadline.value = '';
link.value = '';
}
});
// Notification function (outside DOMContentLoaded since it's used by other scripts)
function showNotification(message, type = 'success') {
const notification = document.createElement('div');
notification.className = `notification ${type}`;
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.classList.add('show');
setTimeout(() => {
notification.classList.remove('show');
setTimeout(() => {
notification.remove();
}, 300);
}, 3000);
}, 100);
}