-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotesInject.js
More file actions
648 lines (558 loc) · 18.1 KB
/
notesInject.js
File metadata and controls
648 lines (558 loc) · 18.1 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/**
* MESSENGER NOTES FEATURE - CONTENT SCRIPT
*
* Injects a "Notes" button into Messenger conversation headers and provides
* a modal interface for managing per-contact notes (create, read, update, delete).
*
* Runs on: facebook.com/messages (matched via manifest.json)
*
* How it works:
* - messengerInject.js detects the active conversation and calls
* window.openNotesModal(userId, userName, profilePicture) exposed by this script.
* - The modal shows existing notes for the contact and lets the user add, edit,
* or delete notes. Each note is stored per-contact in the CRM backend.
*
* Communication:
* - All API calls are proxied through background.js via chrome.runtime.sendMessage()
* because content scripts running on HTTPS facebook.com cannot
* directly fetch the HTTP localhost CRM backend (mixed content / CSP restrictions).
* - Message types: NOTES_LOAD, NOTES_ADD, NOTES_UPDATE, NOTES_DELETE,
* NOTES_GET_ALL_CONTACTS.
* - Includes automatic retry logic to handle service worker wake-up latency.
*
* Also listens for chrome.runtime.onMessage from the extension popup:
* - OPEN_NOTES_MODAL: opens the notes modal for a given contact
* - GET_CONTACTS_WITH_NOTES: returns all contacts that have notes (for popup list)
*/
console.log('[Notes] Notes content script loaded');
/* ===============================
GLOBAL STATE
Tracks the currently open contact (userId, name, profilePicture),
the modal DOM reference, and the note ID being edited (if any).
=============================== */
let currentContactUserId = null;
let currentContactName = null;
let currentContactProfilePicture = null;
let notesModal = null;
let editingNoteId = null;
/* ===============================
BACKGROUND SERVICE WORKER COMMUNICATION
Content scripts on HTTPS pages (facebook.com) cannot fetch HTTP localhost due
to mixed content / CSP restrictions. All note CRUD API calls are routed through
background.js, which runs in the extension's own context with full host_permissions.
Includes timeout + automatic retry to handle service worker wake-up latency.
=============================== */
/**
* Send a single message attempt to background.js with timeout.
*/
function _sendOnce(type, payload, timeoutMs) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error('timeout'));
}, timeoutMs);
try {
chrome.runtime.sendMessage({ type, payload }, (response) => {
clearTimeout(timer);
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
return;
}
if (response && response.success) {
resolve(response.data);
} else {
reject(new Error(response?.error || 'Unknown error'));
}
});
} catch (e) {
clearTimeout(timer);
reject(e);
}
});
}
/**
* Send a message to background.js with automatic retry.
* First attempt uses a shorter timeout; if the service worker was asleep
* the wake-up may cause the first call to be lost — the retry handles that.
*/
async function sendToBackground(type, payload = {}) {
try {
return await _sendOnce(type, payload, 8000);
} catch (firstErr) {
console.log(`[Notes] First attempt for ${type} failed (${firstErr.message}), retrying...`);
return await _sendOnce(type, payload, 15000);
}
}
/**
* Load notes for a specific contact
*/
async function loadNotesForContact(contactUserId) {
try {
console.log('[Notes] Loading notes for contact:', contactUserId);
const notes = await sendToBackground('NOTES_LOAD', { contactUserId });
console.log(`[Notes] Loaded ${notes?.length || 0} notes`);
return notes || [];
} catch (error) {
console.error('[Notes] Error loading notes:', error);
return [];
}
}
/**
* Add a new note
*/
async function addNote(contactUserId, contactName, noteText, profilePicture = null) {
console.log('[Notes] Adding note for:', contactName);
const note = await sendToBackground('NOTES_ADD', { contactUserId, contactName, noteText, profilePicture });
console.log('[Notes] Note added successfully');
return note;
}
/**
* Update an existing note
*/
async function updateNote(contactUserId, noteId, noteText) {
console.log('[Notes] Updating note:', noteId);
await sendToBackground('NOTES_UPDATE', { contactUserId, noteId, noteText });
console.log('[Notes] Note updated successfully');
}
/**
* Delete a note
*/
async function deleteNote(contactUserId, noteId) {
console.log('[Notes] Deleting note:', noteId);
await sendToBackground('NOTES_DELETE', { contactUserId, noteId });
console.log('[Notes] Note deleted successfully');
}
/**
* Get all contacts that have notes
*/
async function getAllContactsWithNotes() {
try {
console.log('[Notes] Getting all contacts with notes');
const contacts = await sendToBackground('NOTES_GET_ALL_CONTACTS');
console.log(`[Notes] Found ${contacts?.length || 0} contacts with notes`);
return contacts || [];
} catch (error) {
console.error('[Notes] Error getting contacts with notes:', error);
return [];
}
}
/* ===============================
NOTES MODAL UI
Creates, opens, and manages the notes modal overlay. The modal contains a
textarea for new/edited notes, a list of existing notes with edit/delete buttons,
and loading/empty states.
=============================== */
function createNotesModal() {
const modal = document.createElement('div');
modal.id = 'crm-notes-modal';
modal.innerHTML = `
<style>
#crm-notes-modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 999999;
}
#crm-notes-modal.active {
display: flex;
align-items: center;
justify-content: center;
}
.notes-modal-content {
background: #fff;
border-radius: 10px;
width: 440px;
max-width: 92%;
max-height: 75vh;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
display: flex;
flex-direction: column;
overflow: hidden;
}
.notes-modal-header {
padding: 14px 16px;
border-bottom: 1px solid #e4e6eb;
display: flex;
justify-content: space-between;
align-items: center;
background: #fff;
}
.notes-modal-title {
font-size: 15px;
font-weight: 600;
margin: 0;
color: #1c1e21;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.notes-modal-close {
background: none;
border: none;
width: 28px;
height: 28px;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: #65676b;
font-size: 18px;
flex-shrink: 0;
}
.notes-modal-close:hover {
background: #f2f2f2;
color: #1c1e21;
}
.notes-modal-body {
padding: 16px;
overflow-y: auto;
flex: 1;
}
.notes-input-container {
margin-bottom: 16px;
}
.notes-textarea {
width: 100%;
min-height: 72px;
padding: 10px 12px;
border: 1px solid #ccd0d5;
border-radius: 6px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 13px;
line-height: 1.4;
resize: vertical;
transition: border-color 0.15s;
box-sizing: border-box;
color: #1c1e21;
}
.notes-textarea::placeholder {
color: #8a8d91;
}
.notes-textarea:focus {
outline: none;
border-color: #1877f2;
}
.notes-actions {
display: flex;
gap: 8px;
margin-top: 8px;
}
.notes-btn {
padding: 6px 14px;
border: none;
border-radius: 6px;
font-weight: 600;
cursor: pointer;
font-size: 13px;
line-height: 1.4;
}
.notes-btn-primary {
background: #1877f2;
color: #fff;
}
.notes-btn-primary:hover {
background: #166fe5;
}
.notes-btn-primary:disabled {
background: #bcc0c4;
cursor: not-allowed;
}
.notes-btn-secondary {
background: #e4e6eb;
color: #1c1e21;
}
.notes-btn-secondary:hover {
background: #d8dadf;
}
.notes-divider {
height: 1px;
background: #e4e6eb;
margin: 0 0 12px 0;
}
.note-item {
padding: 10px 12px;
margin-bottom: 8px;
border-radius: 6px;
background: #f0f2f5;
}
.note-item:last-child {
margin-bottom: 0;
}
.note-text {
margin: 0;
color: #1c1e21;
font-size: 13px;
line-height: 1.45;
white-space: pre-wrap;
word-wrap: break-word;
}
.note-meta {
display: flex;
justify-content: flex-end;
align-items: center;
margin-top: 6px;
}
.note-actions {
display: flex;
gap: 2px;
}
.note-action-btn {
background: none;
border: none;
padding: 2px 8px;
cursor: pointer;
color: #8a8d91;
font-size: 12px;
border-radius: 4px;
}
.note-action-btn:hover {
color: #1c1e21;
background: rgba(0, 0, 0, 0.04);
}
.note-action-btn.delete:hover {
color: #e4334b;
}
.empty-state {
text-align: center;
padding: 28px 16px;
color: #8a8d91;
font-size: 13px;
}
.empty-state p {
margin: 0;
}
.notes-spinner {
display: inline-block;
width: 18px;
height: 18px;
border: 2px solid #e4e6eb;
border-top-color: #1877f2;
border-radius: 50%;
animation: notesSpin 0.6s linear infinite;
}
@keyframes notesSpin {
to { transform: rotate(360deg); }
}
</style>
<div class="notes-modal-content">
<div class="notes-modal-header">
<span class="notes-modal-title" id="notes-contact-name">Notes</span>
<button class="notes-modal-close" id="notes-close-btn">×</button>
</div>
<div class="notes-modal-body">
<div class="notes-input-container">
<textarea
class="notes-textarea"
id="notes-textarea"
placeholder="Write a note..."
></textarea>
<div class="notes-actions">
<button class="notes-btn notes-btn-primary" id="notes-save-btn">
<span id="notes-save-text">Save</span>
</button>
<button class="notes-btn notes-btn-secondary" id="notes-cancel-btn" style="display: none;">
Cancel
</button>
</div>
</div>
<div class="notes-divider"></div>
<div id="notes-list-container">
<div class="empty-state">
<p>No notes yet</p>
</div>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
// Event listeners
modal.querySelector('#notes-close-btn').addEventListener('click', closeNotesModal);
modal.querySelector('#notes-save-btn').addEventListener('click', handleSaveNote);
modal.querySelector('#notes-cancel-btn').addEventListener('click', cancelEditNote);
// Close on backdrop click
modal.addEventListener('click', (e) => {
if (e.target === modal) {
closeNotesModal();
}
});
return modal;
}
async function openNotesModal(contactUserId, contactName, profilePicture = null) {
console.log('[Notes] Opening notes modal for:', contactName, contactUserId);
currentContactUserId = contactUserId;
currentContactName = contactName;
currentContactProfilePicture = profilePicture;
editingNoteId = null;
if (!notesModal) {
notesModal = createNotesModal();
}
// Update modal title
document.getElementById('notes-contact-name').textContent = contactName;
// Clear textarea
document.getElementById('notes-textarea').value = '';
document.getElementById('notes-save-text').textContent = 'Save';
document.getElementById('notes-cancel-btn').style.display = 'none';
// Show modal
notesModal.classList.add('active');
// Load notes
await loadAndDisplayNotes(contactUserId);
}
function closeNotesModal() {
if (notesModal) {
notesModal.classList.remove('active');
currentContactUserId = null;
currentContactName = null;
currentContactProfilePicture = null;
editingNoteId = null;
}
}
async function loadAndDisplayNotes(contactUserId) {
const container = document.getElementById('notes-list-container');
container.innerHTML = '<div style="text-align: center; padding: 20px;"><div class="notes-spinner"></div></div>';
try {
const notes = await loadNotesForContact(contactUserId);
if (notes.length === 0) {
container.innerHTML = `
<div class="empty-state">
<p>No notes yet</p>
</div>
`;
return;
}
container.innerHTML = notes.map(note => `
<div class="note-item" data-note-id="${escapeHtml(note.id)}">
<p class="note-text">${escapeHtml(note.text)}</p>
<div class="note-meta">
<div class="note-actions">
<button class="note-action-btn edit" data-note-id="${escapeHtml(note.id)}">Edit</button>
<button class="note-action-btn delete" data-note-id="${escapeHtml(note.id)}">Delete</button>
</div>
</div>
</div>
`).join('');
// Add event listeners to edit/delete buttons
container.querySelectorAll('.note-action-btn.edit').forEach(btn => {
btn.addEventListener('click', () => handleEditNote(btn.dataset.noteId, notes));
});
container.querySelectorAll('.note-action-btn.delete').forEach(btn => {
btn.addEventListener('click', () => handleDeleteNote(btn.dataset.noteId));
});
} catch (error) {
console.error('[Notes] Error displaying notes:', error);
container.innerHTML = `
<div class="empty-state">
<p>Error loading notes. Please try again.</p>
</div>
`;
}
}
async function handleSaveNote() {
const textarea = document.getElementById('notes-textarea');
const noteText = textarea.value.trim();
if (!noteText) {
alert('Please enter a note');
return;
}
const saveBtn = document.getElementById('notes-save-btn');
saveBtn.disabled = true;
saveBtn.innerHTML = '<div class="notes-spinner"></div>';
try {
const wasEditing = !!editingNoteId;
if (editingNoteId) {
// Update existing note
await updateNote(currentContactUserId, editingNoteId, noteText);
editingNoteId = null;
} else {
// Add new note
await addNote(currentContactUserId, currentContactName, noteText, currentContactProfilePicture);
}
// Clear textarea and reset edit state
textarea.value = '';
editingNoteId = null;
// Hide cancel button if was editing
if (wasEditing) {
document.getElementById('notes-cancel-btn').style.display = 'none';
}
// Reload notes
await loadAndDisplayNotes(currentContactUserId);
} catch (error) {
alert('Error saving note: ' + error.message);
} finally {
saveBtn.disabled = false;
// Restore button content
saveBtn.innerHTML = '<span id="notes-save-text">Save</span>';
}
}
function handleEditNote(noteId, notes) {
const note = notes.find(n => n.id === noteId);
if (!note) return;
// Populate textarea
document.getElementById('notes-textarea').value = note.text;
document.getElementById('notes-save-text').textContent = 'Update';
document.getElementById('notes-cancel-btn').style.display = 'inline-block';
editingNoteId = noteId;
// Scroll to top
document.getElementById('notes-textarea').scrollIntoView({ behavior: 'smooth' });
document.getElementById('notes-textarea').focus();
}
function cancelEditNote() {
document.getElementById('notes-textarea').value = '';
document.getElementById('notes-save-text').textContent = 'Save';
document.getElementById('notes-cancel-btn').style.display = 'none';
editingNoteId = null;
}
async function handleDeleteNote(noteId) {
if (!confirm('Are you sure you want to delete this note?')) {
return;
}
try {
await deleteNote(currentContactUserId, noteId);
await loadAndDisplayNotes(currentContactUserId);
} catch (error) {
alert('Error deleting note: ' + error.message);
}
}
/* ===============================
UTILITY FUNCTIONS
Shared helpers (HTML escaping) used across the notes UI.
=============================== */
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/* ===============================
MESSAGE LISTENERS
Handles chrome.runtime.onMessage from the extension popup: opening the notes
modal for a specific contact, and returning all contacts that have notes.
Exposes openNotesModal and getAllContactsWithNotes on the window object
so messengerInject.js can call them directly.
=============================== */
// Listen for messages from popup to open notes modal
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('[Notes] Content script received message:', message.type);
if (message.type === 'OPEN_NOTES_MODAL') {
openNotesModal(message.userId, message.userName, message.profilePicture || null);
sendResponse({ success: true });
} else if (message.type === 'GET_CONTACTS_WITH_NOTES') {
console.log('[Notes] Handling GET_CONTACTS_WITH_NOTES request');
// Get all contacts with notes
getAllContactsWithNotes().then(contacts => {
console.log('[Notes] Sending response to popup with contacts:', contacts);
sendResponse({ success: true, contacts });
}).catch(error => {
console.error('[Notes] Error getting contacts with notes:', error);
sendResponse({ success: false, error: error.message, contacts: [] });
});
return true; // Async response
}
return true;
});
// Export function for external use
window.openNotesModal = openNotesModal;
window.getAllContactsWithNotes = getAllContactsWithNotes;
console.log('[Notes] Notes feature ready (using direct API calls)');