-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
125 lines (109 loc) · 4.29 KB
/
options.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
import { MARKETS } from './js/config/markets.js';
// Get enabled markets from storage
async function getEnabledMarkets() {
return new Promise((resolve) => {
chrome.storage.sync.get(['enabledMarkets'], (result) => {
resolve(result.enabledMarkets || ['SENSEX', 'NIFTY50']);
});
});
}
// Get primary market from storage
async function getPrimaryMarket() {
return new Promise((resolve) => {
chrome.storage.sync.get(['primaryMarket'], (result) => {
resolve(result.primaryMarket || '');
});
});
}
// Save settings to storage
async function saveSettings(enabledMarkets, primaryMarket) {
return new Promise((resolve) => {
chrome.storage.sync.set({
enabledMarkets: enabledMarkets,
primaryMarket: primaryMarket
}, resolve);
});
}
// Initialize the options page
async function initializeOptions() {
const marketGrid = document.getElementById('market-grid');
const primaryMarketSelect = document.getElementById('primary-market');
const saveButton = document.getElementById('save');
const enabledMarkets = await getEnabledMarkets();
const primaryMarket = await getPrimaryMarket();
// Populate primary market select
Object.entries(MARKETS).forEach(([key, market]) => {
const option = document.createElement('option');
option.value = key;
option.textContent = market.name;
if (key === primaryMarket) {
option.selected = true;
}
primaryMarketSelect.appendChild(option);
});
// Create market cards
Object.entries(MARKETS).forEach(([key, market]) => {
const card = document.createElement('div');
card.className = `market-card ${enabledMarkets.includes(key) ? 'selected' : ''}`;
card.innerHTML = `
<div class="market-info">
<div class="market-name">${market.name}</div>
<div class="market-location">${market.currency} • ${market.category}</div>
</div>
<input type="checkbox" class="checkbox" value="${key}" ${enabledMarkets.includes(key) ? 'checked' : ''}>
`;
marketGrid.appendChild(card);
// Add click handler for the entire card
card.addEventListener('click', (e) => {
if (e.target.type !== 'checkbox') {
const checkbox = card.querySelector('.checkbox');
checkbox.checked = !checkbox.checked;
card.classList.toggle('selected', checkbox.checked);
}
});
// Add change handler for checkbox
const checkbox = card.querySelector('.checkbox');
checkbox.addEventListener('change', () => {
card.classList.toggle('selected', checkbox.checked);
});
});
// Save button handler
saveButton.addEventListener('click', async () => {
const selectedMarkets = Array.from(marketGrid.querySelectorAll('.checkbox:checked'))
.map(checkbox => checkbox.value);
const selectedPrimaryMarket = primaryMarketSelect.value;
if (!selectedPrimaryMarket) {
alert('Please select a primary market');
return;
}
if (selectedMarkets.length === 0) {
alert('Please select at least one market to track');
return;
}
if (!selectedMarkets.includes(selectedPrimaryMarket)) {
alert('Primary market must be included in selected markets');
primaryMarketSelect.focus();
return;
}
await saveSettings(selectedMarkets, selectedPrimaryMarket);
// Show success message
const successMessage = document.createElement('div');
successMessage.textContent = 'Settings saved successfully!';
successMessage.style.cssText = `
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background: #4CAF50;
color: white;
padding: 12px 24px;
border-radius: 6px;
font-size: 14px;
animation: fadeOut 3s forwards;
`;
document.body.appendChild(successMessage);
setTimeout(() => successMessage.remove(), 3000);
});
}
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', initializeOptions);