-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
539 lines (473 loc) · 18.6 KB
/
app.js
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
// Watchlist management
let watchlists = [];
let activeWatchlistId = null;
// Load watchlists from localStorage
function loadWatchlists() {
const savedWatchlists = localStorage.getItem('watchlists');
if (savedWatchlists) {
watchlists = JSON.parse(savedWatchlists);
activeWatchlistId = watchlists[0]?.id || null;
} else {
// Create default watchlist
const defaultWatchlist = {
id: 'default',
name: 'Default',
symbols: [
{
symbol: 'AAPL',
name: 'Apple Inc.',
price: 0.00,
change: 0.00,
changePercentage: 0.00,
high: 0.00,
low: 0.00,
open: 0.00,
lastUpdated: null
},
{
symbol: 'MSFT',
name: 'Microsoft Corporation',
price: 0.00,
change: 0.00,
changePercentage: 0.00,
high: 0.00,
low: 0.00,
open: 0.00,
lastUpdated: null
}
]
};
watchlists = [defaultWatchlist];
activeWatchlistId = defaultWatchlist.id;
saveWatchlists();
}
}
// Save watchlists to localStorage
function saveWatchlists() {
localStorage.setItem('watchlists', JSON.stringify(watchlists));
}
// Get active watchlist
function getActiveWatchlist() {
return watchlists.find(w => w.id === activeWatchlistId) || watchlists[0];
}
// Function to format numbers with commas
function formatNumber(number) {
return new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(number);
}
// Function to safely load URL in webview
function loadPerplexityURL(symbol) {
const perplexityView = document.getElementById('perplexityView');
if (!perplexityView) return;
try {
const url = `https://www.perplexity.ai/finance/${encodeURIComponent(symbol)}`;
// Check if the webview is already navigating to prevent unnecessary reloads
if (perplexityView.getAttribute('src') !== url) {
perplexityView.setAttribute('src', url);
}
} catch (error) {
console.error('Error loading URL:', error);
}
}
// Function to create a stock item element
function createStockItem(stock) {
const div = document.createElement('div');
div.className = 'stock-item';
div.innerHTML = `
<div class="stock-item-header">
<span class="stock-symbol">${stock.symbol}</span>
<div class="stock-header-right">
<span class="stock-price">$${formatNumber(stock.price)}</span>
<button class="remove-button" title="Remove ${stock.symbol}">×</button>
</div>
</div>
<div class="stock-item-details">
<span class="stock-name">${stock.name}</span>
<span class="stock-change ${stock.changePercentage >= 0 ? 'positive' : 'negative'}">
${stock.changePercentage >= 0 ? '+' : ''}${stock.changePercentage.toFixed(2)}%
</span>
</div>
<div class="stock-price-details">
<div class="price-summary">
<span class="label">Open:</span>
<span class="value">$${formatNumber(stock.open)}</span>
<span class="separator">|</span>
<span class="label">High:</span>
<span class="value">$${formatNumber(stock.high)}</span>
<span class="separator">|</span>
<span class="label">Low:</span>
<span class="value">$${formatNumber(stock.low)}</span>
</div>
</div>
`;
// Add click handler to load Perplexity Finance in webview
div.addEventListener('click', (e) => {
// Don't trigger if clicking the remove button
if (!e.target.classList.contains('remove-button')) {
document.querySelectorAll('.stock-item').forEach(item => {
item.classList.remove('selected');
});
div.classList.add('selected');
loadPerplexityURL(stock.symbol);
}
});
// Add remove button handler
const removeButton = div.querySelector('.remove-button');
removeButton.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent the click from triggering the stock item click
const index = getActiveWatchlist().symbols.findIndex(item => item.symbol === stock.symbol);
if (index !== -1) {
getActiveWatchlist().symbols.splice(index, 1);
saveWatchlists();
renderWatchlist();
}
});
return div;
}
// Function to render the watchlist
function renderWatchlist() {
const watchlistContainer = document.getElementById('watchlist');
if (!watchlistContainer) return;
const activeWatchlist = getActiveWatchlist();
watchlistContainer.innerHTML = '';
activeWatchlist.symbols.forEach(stock => {
watchlistContainer.appendChild(createStockItem(stock));
});
}
// Function to show create watchlist dialog
function showCreateWatchlistDialog() {
const dialog = document.createElement('div');
dialog.className = 'watchlist-dialog';
dialog.innerHTML = `
<div class="dialog-content">
<h3>Create New Watchlist</h3>
<input type="text" id="watchlistNameInput" placeholder="Enter watchlist name" class="dialog-input">
<div class="dialog-buttons">
<button id="createWatchlistBtn" class="dialog-button">Create</button>
<button id="cancelWatchlistBtn" class="dialog-button">Cancel</button>
</div>
</div>
`;
// Add event listeners
dialog.querySelector('#createWatchlistBtn').addEventListener('click', () => {
const nameInput = dialog.querySelector('#watchlistNameInput');
const name = nameInput.value.trim();
if (name) {
createWatchlist(name);
document.body.removeChild(dialog);
}
});
dialog.querySelector('#cancelWatchlistBtn').addEventListener('click', () => {
document.body.removeChild(dialog);
});
dialog.querySelector('#watchlistNameInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const name = e.target.value.trim();
if (name) {
createWatchlist(name);
document.body.removeChild(dialog);
}
}
});
document.body.appendChild(dialog);
dialog.querySelector('#watchlistNameInput').focus();
}
// Function to render watchlist tabs
function renderWatchlistTabs() {
const watchlistTabsContainer = document.querySelector('.watchlist-tabs');
if (!watchlistTabsContainer) return;
watchlistTabsContainer.innerHTML = '';
watchlists.forEach(watchlist => {
const tab = document.createElement('div');
tab.className = `watchlist-tab ${watchlist.id === activeWatchlistId ? 'active' : ''}`;
tab.innerHTML = `
<span class="tab-name">${watchlist.name}</span>
<span class="symbol-count">${watchlist.symbols.length}</span>
`;
tab.addEventListener('click', () => {
activeWatchlistId = watchlist.id;
renderWatchlistTabs();
renderWatchlist();
});
watchlistTabsContainer.appendChild(tab);
});
// Re-append the add watchlist button
const addWatchlistButton = document.createElement('button');
addWatchlistButton.className = 'add-watchlist-button';
addWatchlistButton.innerHTML = '+';
addWatchlistButton.title = 'Create new watchlist';
addWatchlistButton.addEventListener('click', () => {
showCreateWatchlistDialog();
});
watchlistTabsContainer.appendChild(addWatchlistButton);
}
// Create new watchlist
function createWatchlist(name) {
const newWatchlist = {
id: Date.now().toString(),
name: name,
symbols: []
};
watchlists.push(newWatchlist);
saveWatchlists();
renderWatchlistTabs();
return newWatchlist;
}
// Add symbol to watchlist
function addSymbolToWatchlist(symbol, watchlistId) {
const watchlist = watchlists.find(w => w.id === watchlistId);
if (!watchlist) return false;
if (watchlist.symbols.some(s => s.symbol === symbol)) return false;
const newStock = {
symbol: symbol,
name: symbol,
price: 0.00,
change: 0.00,
changePercentage: 0.00,
high: 0.00,
low: 0.00,
open: 0.00,
lastUpdated: null
};
watchlist.symbols.push(newStock);
saveWatchlists();
return true;
}
// Remove symbol from watchlist
function removeSymbolFromWatchlist(symbol, watchlistId) {
const watchlist = watchlists.find(w => w.id === watchlistId);
if (!watchlist) return false;
const index = watchlist.symbols.findIndex(s => s.symbol === symbol);
if (index === -1) return false;
watchlist.symbols.splice(index, 1);
saveWatchlists();
return true;
}
let updateInterval = null;
// Function to format timestamp
function formatLastUpdated(timestamp) {
const date = new Date(timestamp);
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
const ampm = hours >= 12 ? 'PM' : 'AM';
const hours12 = (hours % 12) || 12;
return `Updated at ${hours12}:${minutes}:${seconds} ${ampm}`;
}
// Function to format symbol for Finnhub
function formatSymbolForFinnhub(symbol) {
// For US stocks, we need to prefix with 'BINANCE:' for crypto or nothing for stocks
return symbol.includes('BTC') ? `BINANCE:${symbol}` : symbol;
}
// Wait for DOM to be ready
document.addEventListener('DOMContentLoaded', () => {
// Load saved watchlists
loadWatchlists();
// DOM Elements
const watchlistContainer = document.getElementById('watchlist');
const searchInput = document.getElementById('searchInput');
const addButton = document.getElementById('addButton');
const perplexityView = document.getElementById('perplexityView');
// Initialize webview with first stock if available
if (perplexityView) {
const activeWatchlist = getActiveWatchlist();
if (activeWatchlist.symbols.length > 0) {
const firstStock = activeWatchlist.symbols[0];
loadPerplexityURL(firstStock.symbol);
// Set up webview event listeners
perplexityView.addEventListener('dom-ready', () => {
console.log('Webview DOM Ready');
// Add class to mark the first stock as selected
const firstStockElement = document.querySelector('.stock-item');
if (firstStockElement) {
firstStockElement.classList.add('selected');
}
});
perplexityView.addEventListener('did-fail-load', (event) => {
console.error('Failed to load:', event);
});
perplexityView.addEventListener('did-finish-load', () => {
console.log('Finished loading');
});
}
}
// Add last updated span to header
const lastUpdatedSpan = document.createElement('span');
lastUpdatedSpan.className = 'header-last-updated';
lastUpdatedSpan.textContent = 'Just now';
// Update watchlist header structure
const watchlistHeader = document.querySelector('.watchlist-header');
watchlistHeader.innerHTML = `
<div class="watchlist-header-row">
<h2>My Symbols</h2>
<span class="header-last-updated">Just now</span>
</div>
`;
// Create watchlist tabs container
const watchlistTabsContainer = document.createElement('div');
watchlistTabsContainer.className = 'watchlist-tabs';
document.querySelector('.watchlist-sidebar').insertBefore(watchlistTabsContainer, document.querySelector('.watchlist-header'));
// Initial render of watchlist tabs
renderWatchlistTabs();
// Function to show watchlist selection dialog
function showWatchlistSelectionDialog(symbol) {
const dialog = document.createElement('div');
dialog.className = 'watchlist-dialog';
function renderDialogContent(isDeleteMode = false) {
dialog.innerHTML = `
<div class="dialog-content">
<div class="dialog-header">
<h3>${isDeleteMode ? 'Delete Watchlist' : `Add ${symbol} to watchlist:`}</h3>
<span class="dialog-mode-text">${isDeleteMode ? 'Release Alt/Option to cancel' : 'Hold Alt/Option to delete watchlists'}</span>
</div>
<div class="watchlist-options">
${watchlists.map(w => `
<div class="watchlist-option ${isDeleteMode ? 'delete-mode' : ''}" data-id="${w.id}">
<span>${w.name}</span>
${isDeleteMode ?
'<span class="delete-mode-text">Click to Delete</span>' :
`<span class="symbol-count">${w.symbols.length}</span>`
}
</div>
`).join('')}
</div>
</div>
`;
}
// Initial render
renderDialogContent();
// Handle Alt/Option key
function handleKeyChange(e) {
if (e.altKey) {
renderDialogContent(true);
} else {
renderDialogContent(false);
}
}
document.addEventListener('keydown', handleKeyChange);
document.addEventListener('keyup', handleKeyChange);
// Handle clicks
dialog.addEventListener('click', (e) => {
const option = e.target.closest('.watchlist-option');
if (option) {
const watchlistId = option.dataset.id;
const isDeleteMode = e.altKey;
if (isDeleteMode) {
// Don't delete if it's the last watchlist
if (watchlists.length <= 1) {
return;
}
// Delete watchlist
const index = watchlists.findIndex(w => w.id === watchlistId);
if (index !== -1) {
watchlists.splice(index, 1);
// If we deleted the active watchlist, switch to the first available one
if (watchlistId === activeWatchlistId) {
activeWatchlistId = watchlists[0].id;
}
saveWatchlists();
renderWatchlistTabs();
renderWatchlist();
}
} else {
// Add symbol to watchlist
if (addSymbolToWatchlist(symbol, watchlistId)) {
updateStockData(getActiveWatchlist().symbols.find(s => s.symbol === symbol)).then(() => {
renderWatchlist();
});
}
}
document.body.removeChild(dialog);
}
});
// Cleanup event listeners when dialog is closed
function cleanup() {
document.removeEventListener('keydown', handleKeyChange);
document.removeEventListener('keyup', handleKeyChange);
}
dialog.addEventListener('click', (e) => {
if (e.target === dialog) {
document.body.removeChild(dialog);
cleanup();
}
});
document.body.appendChild(dialog);
}
// Modify addStock function
function addStock() {
const symbol = searchInput.value.trim().toUpperCase();
if (symbol) {
showWatchlistSelectionDialog(symbol);
searchInput.value = '';
}
}
// Function to update stock data
async function updateStockData(stock) {
try {
console.log('Fetching quote for:', stock.symbol);
const quote = await window.finnhub.getQuote(stock.symbol);
console.log('Received quote:', quote);
stock.price = quote.c;
stock.change = quote.c - quote.pc;
stock.changePercentage = (stock.change / quote.pc) * 100;
stock.high = quote.h;
stock.low = quote.l;
stock.open = quote.o;
stock.lastUpdated = Date.now();
// Save after updating stock data
saveWatchlists();
} catch (error) {
console.error('Error updating stock data:', error);
}
}
// Function to update all stocks
async function updateAllStocks() {
console.log('Updating all stocks...');
const activeWatchlist = getActiveWatchlist();
for (const stock of activeWatchlist.symbols) {
await updateStockData(stock);
}
saveWatchlists(); // Save after all updates are complete
renderWatchlist();
updateHeaderTimestamp();
}
// Function to update header timestamp
function updateHeaderTimestamp() {
const activeWatchlist = getActiveWatchlist();
const lastUpdatedSpan = document.querySelector('.header-last-updated');
if (!lastUpdatedSpan) return;
if (activeWatchlist.symbols.length > 0) {
const mostRecentUpdate = Math.max(...activeWatchlist.symbols.map(stock => stock.lastUpdated || 0));
if (mostRecentUpdate) {
lastUpdatedSpan.textContent = formatLastUpdated(mostRecentUpdate);
}
} else {
lastUpdatedSpan.textContent = 'No symbols';
}
}
// Event listeners
addButton.addEventListener('click', addStock);
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addStock();
}
});
// Initial render
renderWatchlist();
updateAllStocks();
// Update stocks every 30 seconds instead of 15
updateInterval = setInterval(() => {
updateAllStocks();
}, 30000);
// Update timestamps every minute
setInterval(updateHeaderTimestamp, 60000);
// Clean up when the window is closed
window.addEventListener('beforeunload', () => {
if (updateInterval) {
clearInterval(updateInterval);
}
saveWatchlists(); // Save one final time before closing
});
});