-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
426 lines (353 loc) · 13.8 KB
/
content.js
File metadata and controls
426 lines (353 loc) · 13.8 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
let currentUrl = location.href;
let notesCache = {};
console.log("ZNT: Extension Loaded", window.location.href);
createModal(); // Initialize note edit modal
createExportModal(); // Initialize export output modal
// --- GENERIC HELPERS ---
function debounce(func, wait) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
function saveNote(zpid, text, callback) {
if (!zpid) return;
chrome.storage.local.set({ [zpid]: text }, () => {
notesCache[zpid] = text;
if (callback) callback();
});
}
// --- ZILLOW PARSING HELPERS ---
function getZpidFromUrl(url) {
if (!url) return null;
try {
const m1 = url.match(/\/(\d+)_zpid/);
if (m1) return m1[1];
const u = new URL(url, window.location.origin);
const param = u.searchParams.get('zpid');
if (param && /^\d+$/.test(param)) return param;
} catch (e) {}
const canonical = document.querySelector('link[rel="canonical"]');
if (canonical && canonical.href) {
const m2 = canonical.href.match(/\/(\d+)_zpid/);
if (m2) return m2[1];
}
return null;
}
function extractZpidFromCard(card) {
if (!card) return null;
const compareInput = card.querySelector('input[type="checkbox"][name]');
if (compareInput && /^\d+$/.test(compareInput.name)) return compareInput.name;
const link = card.querySelector('a[href*="_zpid"]');
if (link) {
const m = (link.getAttribute('href') || '').match(/(\d+)_zpid/);
if (m) return m[1];
}
const dz = card.getAttribute && card.getAttribute('data-zpid');
if (dz && /^\d+$/.test(dz)) return dz;
const m3 = (card.id || '').match(/zpid_(\d+)/);
if (m3) return m3[1];
return null;
}
// --- DATA LOADING ---
function loadNotesCache(done) {
chrome.storage.local.get(null, (all) => {
notesCache = all || {};
if (typeof done === 'function') done();
});
}
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== 'local') return;
Object.keys(changes).forEach((key) => {
if (changes[key].newValue === undefined) delete notesCache[key];
else notesCache[key] = changes[key].newValue;
});
injectListingIndicators();
const zpid = getZpidFromUrl(window.location.href);
const textarea = document.getElementById('znt-note-area');
if (textarea && zpid && document.activeElement !== textarea) {
textarea.value = notesCache[zpid] || '';
}
});
// --- UI COMPONENT FACTORY (DRY) ---
function createNoteBoxUI(zpid, initialText) {
const container = document.createElement('div');
container.id = 'znt-details-container';
container.className = 'znt-note-container';
container.innerHTML = `
<div class="znt-title">My Private Note</div>
<textarea id="znt-note-area" class="znt-textarea" placeholder="Enter your thoughts..."></textarea>
<div style="display:flex; align-items:center; gap:8px;">
<button id="znt-save-btn" class="znt-save-btn">Save Note</button>
<span id="znt-msg" class="znt-status-msg" aria-live="polite">Note Saved!</span>
</div>
`;
const textarea = container.querySelector('#znt-note-area');
const saveBtn = container.querySelector('#znt-save-btn');
const msg = container.querySelector('#znt-msg');
textarea.value = initialText || '';
saveBtn.addEventListener('click', () => {
saveNote(zpid, textarea.value, () => {
msg.classList.add('visible');
setTimeout(() => msg.classList.remove('visible'), 2000);
});
});
return container;
}
// --- FEATURE 1: DETAILS PAGE ---
async function injectDetailsNote() {
const zpid = getZpidFromUrl(window.location.href);
if (!zpid) return;
const existing = document.getElementById('znt-details-container');
if (existing) {
const ta = existing.querySelector('#znt-note-area');
if (ta && ta.value !== (notesCache[zpid] || '') && document.activeElement !== ta) {
ta.value = notesCache[zpid] || '';
}
return;
}
const placementTargets = [
{
sel: '.layout-sticky-content',
method: 'append',
refine: (el) => el.querySelector('div[class*="Flex"]') || el
},
{
sel: '.layout-static-column-container',
method: 'after_child',
refine: (el) => el.firstElementChild
},
{ sel: '.ds-data-col', method: 'prepend' },
{ sel: '[data-testid="property-overview"]', method: 'after' }
];
let target = null;
let method = 'append';
let referenceNode = null;
for (const strategy of placementTargets) {
const el = document.querySelector(strategy.sel);
if (el) {
if (strategy.method === 'after_child') {
const child = strategy.refine ? strategy.refine(el) : null;
if (child) {
target = el;
referenceNode = child.nextSibling;
method = 'insertBefore';
} else {
target = el;
method = 'prepend';
}
} else {
target = strategy.refine ? strategy.refine(el) : el;
method = strategy.method;
}
break;
}
}
if (!target) return;
const ui = createNoteBoxUI(zpid, notesCache[zpid]);
ui.classList.add('znt-sidebar-style');
if (method === 'insertBefore' && referenceNode) {
target.insertBefore(ui, referenceNode);
} else if (method === 'after') {
target.parentNode.insertBefore(ui, target.nextSibling);
} else if (method === 'prepend') {
target.prepend(ui);
} else {
target.appendChild(ui);
}
}
// --- FEATURE 2: LISTING CARDS & MODAL ---
function createModal() {
if (document.getElementById('znt-modal-overlay')) return;
const overlay = document.createElement('div');
overlay.id = 'znt-modal-overlay';
overlay.className = 'znt-modal-overlay';
overlay.innerHTML = `
<div class="znt-modal-content">
<div class="znt-modal-header">
<div class="znt-title">Edit Note</div>
<button id="znt-modal-close" class="znt-modal-close">×</button>
</div>
<textarea id="znt-modal-textarea" class="znt-textarea" placeholder="Enter your note..."></textarea>
<div style="text-align: right;">
<button id="znt-modal-save" class="znt-save-btn">Save Note</button>
</div>
</div>
`;
document.body.appendChild(overlay);
const closeBtn = overlay.querySelector('#znt-modal-close');
const saveBtn = overlay.querySelector('#znt-modal-save');
const textarea = overlay.querySelector('#znt-modal-textarea');
const closeModal = () => {
overlay.classList.remove('open');
overlay.dataset.activeZpid = '';
};
closeBtn.addEventListener('click', closeModal);
overlay.addEventListener('click', (e) => { if (e.target === overlay) closeModal(); });
saveBtn.addEventListener('click', () => {
const zpid = overlay.dataset.activeZpid;
if (zpid) saveNote(zpid, textarea.value, closeModal);
});
}
function openModal(zpid) {
const overlay = document.getElementById('znt-modal-overlay');
const textarea = document.getElementById('znt-modal-textarea');
if (!overlay || !textarea) return;
overlay.dataset.activeZpid = zpid;
textarea.value = notesCache[zpid] || '';
overlay.classList.add('open');
textarea.focus();
}
function injectListingIndicators() {
const selector = 'article, li[class*="ListItem-"], [data-test="property-card"], [class*="result-list-card"], [class*="search-list-item"], [data-testid="PropertyListCard-wrapper"]';
const cards = document.querySelectorAll(selector);
cards.forEach(card => {
const zpid = extractZpidFromCard(card);
if (!zpid) return;
let annotation = card.querySelector('.znt-card-annotation');
if (!annotation) {
annotation = document.createElement('div');
annotation.className = 'znt-card-annotation';
annotation.innerHTML = `
<div class="znt-card-icon" title="Click to edit note">✎</div>
<span class="znt-snippet"></span>
<div class="znt-tooltip"></div>
`;
const imageWrapper = card.querySelector('[class*="Photo"]') || card.querySelector('.list-card-top') || card;
if (window.getComputedStyle(imageWrapper).position === 'static') {
imageWrapper.style.position = 'relative';
}
imageWrapper.appendChild(annotation);
annotation.querySelector('.znt-card-icon').addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
openModal(zpid);
});
}
const noteText = notesCache[zpid] || '';
const icon = annotation.querySelector('.znt-card-icon');
const snippet = annotation.querySelector('.znt-snippet');
const tooltip = annotation.querySelector('.znt-tooltip');
if (noteText.trim().length > 0) {
if (!icon.classList.contains('has-note')) icon.classList.add('has-note');
const newSnippetText = noteText.length > 25 ? noteText.substring(0, 25) + '...' : noteText;
if (snippet.textContent !== newSnippetText) snippet.textContent = newSnippetText;
if (!snippet.classList.contains('visible')) snippet.classList.add('visible');
if (tooltip.textContent !== noteText) tooltip.textContent = noteText;
} else {
if (icon.classList.contains('has-note')) icon.classList.remove('has-note');
if (snippet.classList.contains('visible')) snippet.classList.remove('visible');
const emptyText = "Click to add a note";
if (tooltip.textContent !== emptyText) tooltip.textContent = emptyText;
}
});
}
// --- FEATURE 3: EXPORT TOOL ---
function createExportModal() {
if (document.getElementById('znt-export-modal')) return;
const modal = document.createElement('div');
modal.id = 'znt-export-modal';
modal.className = 'znt-modal-overlay';
modal.innerHTML = `
<div class="znt-modal-content" style="max-width: 600px;">
<div class="znt-modal-header">
<div class="znt-title">Export Listings</div>
<button id="znt-export-close" class="znt-modal-close">×</button>
</div>
<p style="margin-bottom:10px; color:#666; font-size:14px;">
Ready to copy! Paste this directly into your email.
</p>
<textarea id="znt-export-area" class="znt-textarea" style="height: 300px; font-family: monospace; font-size: 12px; white-space: pre;"></textarea>
<div style="text-align: right;">
<button id="znt-copy-btn" class="znt-save-btn">Copy to Clipboard</button>
</div>
</div>
`;
document.body.appendChild(modal);
const closeBtn = modal.querySelector('#znt-export-close');
const copyBtn = modal.querySelector('#znt-copy-btn');
const area = modal.querySelector('#znt-export-area');
const close = () => modal.classList.remove('open');
closeBtn.addEventListener('click', close);
modal.addEventListener('click', (e) => { if (e.target === modal) close(); });
copyBtn.addEventListener('click', () => {
area.select();
document.execCommand('copy');
copyBtn.innerText = "Copied!";
setTimeout(() => copyBtn.innerText = "Copy to Clipboard", 2000);
});
}
function collectListingData() {
const selector = 'article, li[class*="ListItem-"], [data-test="property-card"], [class*="result-list-card"]';
const cards = document.querySelectorAll(selector);
let exportLines = [];
cards.forEach(card => {
const zpid = extractZpidFromCard(card);
if (!zpid) return;
// Scrape Data
// const priceEl = card.querySelector('[data-test="property-card-price"], .list-card-price, span[class*="PropertyCardWrapper__StyledPrice"]');
const priceEl = card.querySelector('[data-testid="data-price-row"]');
const addrEl = card.querySelector('address, [data-test="property-card-addr"], .list-card-addr');
const linkEl = card.querySelector('a[href*="_zpid"]');
const price = priceEl ? priceEl.textContent.trim() : "Price N/A";
const address = addrEl ? addrEl.textContent.trim() : "Address N/A";
const link = linkEl ? linkEl.href : `https://www.zillow.com/homedetails/${zpid}_zpid/`;
const myNote = notesCache[zpid] ? notesCache[zpid].trim() : null;
// Only export if we have a note OR it's a valid card (Optional: remove 'true' to only export commented homes)
if (true) {
let block = `${address} (${price})\n${link}`;
if (myNote) {
block += `\nMY NOTES: ${myNote}`;
}
exportLines.push(block);
}
});
if (exportLines.length === 0) return "No listings found on this page.";
return `Here is the list of homes I'm interested in:\n\n` + exportLines.join('\n\n---------------------------------\n\n');
}
function createExportButton() {
if (document.getElementById('znt-export-trigger')) return;
const btn = document.createElement('button');
btn.id = 'znt-export-trigger';
btn.innerText = "Export Page";
btn.className = 'znt-save-btn'; // Reuse existing style
btn.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
z-index: 9999;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
padding: 12px 20px;
font-weight: bold;
`;
btn.addEventListener('click', () => {
const text = collectListingData();
const modal = document.getElementById('znt-export-modal');
const area = document.getElementById('znt-export-area');
area.value = text;
modal.classList.add('open');
});
document.body.appendChild(btn);
}
// --- INIT & OBSERVER ---
function init() {
loadNotesCache(() => {
if (window.location.href.includes('/homedetails/')) injectDetailsNote();
injectListingIndicators();
// Only add export button on search results/map pages, not individual details pages
if (!window.location.href.includes('/homedetails/')) createExportButton();
});
}
const observer = new MutationObserver(debounce(() => {
if (location.href !== currentUrl) {
currentUrl = location.href;
init();
} else {
injectListingIndicators();
if (window.location.href.includes('/homedetails/')) injectDetailsNote();
if (!document.getElementById('znt-export-trigger') && !window.location.href.includes('/homedetails/')) createExportButton();
}
}, 500));
observer.observe(document.body, { childList: true, subtree: true });
init();