-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AmolSoans
committed
Nov 26, 2024
0 parents
commit 44659d6
Showing
29 changed files
with
12,150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
.DS_Store | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import { NseIndia } from 'stock-nse-india'; | ||
//const nseIndia = new NseIndia(); | ||
// Constants | ||
const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours in milliseconds | ||
|
||
// Cache structure | ||
const CACHE_CONFIG = { | ||
key: 'stockCache', | ||
version: '1.0', | ||
maxItems: 500 // Limit cache size | ||
}; | ||
|
||
// Initialize extension | ||
chrome.runtime.onInstalled.addListener(async () => { | ||
console.log('Trade Call Widget installed/updated'); | ||
await initializeCache(); | ||
await cacheAllStockSymbols(); // Cache stock symbols on installation | ||
}); | ||
|
||
// Function to cache all stock symbols | ||
/** | ||
* Fetches all stock symbols from the NseIndia instance and caches them in local storage. | ||
*/ | ||
async function cacheAllStockSymbols() { | ||
try { | ||
const symbols = await nseIndia.getAllStockSymbols(); | ||
console.log(symbols); | ||
|
||
// Store symbols in local storage | ||
await chrome.storage.local.set({ [CACHE_CONFIG.key]: { symbols, lastUpdated: Date.now() } }); | ||
console.log('Stock symbols cached successfully.'); | ||
} catch (error) { | ||
console.error('Failed to fetch and cache stock symbols:', error); | ||
} | ||
} | ||
|
||
// Listen for extension icon clicks | ||
chrome.action.onClicked.addListener(async (tab) => { | ||
// Open the side panel in the current window | ||
await chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }) | ||
.catch((error) => console.error(error)); | ||
}); | ||
|
||
|
||
chrome.sidePanel | ||
.setPanelBehavior({ openPanelOnActionClick: true }) | ||
.catch((error) => console.error(error)); | ||
// Initialize side panel options when extension is installed or updated | ||
chrome.runtime.onInstalled.addListener(async () => { | ||
await chrome.sidePanel.setOptions({ | ||
enabled: true, | ||
path: 'popup.html' | ||
}); | ||
}); | ||
|
||
// Handle messages from popup | ||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { | ||
switch (request.type) { | ||
case 'SEARCH_STOCKS': | ||
handleStockSearch(request.query).then(sendResponse); | ||
return true; // Will respond asynchronously | ||
|
||
case 'FETCH_STOCK_DETAILS': | ||
//add fetch logic if needed | ||
updateCache() | ||
return true; | ||
|
||
case 'CLEAR_CACHE': | ||
clearCache().then(sendResponse); | ||
return true; | ||
} | ||
}); | ||
|
||
// Cache Management | ||
/** | ||
* Initializes the cache in local storage if it does not already exist. | ||
*/ | ||
async function initializeCache() { | ||
const cache = await chrome.storage.local.get(CACHE_CONFIG.key); | ||
if (!cache[CACHE_CONFIG.key]) { | ||
await chrome.storage.local.set({ | ||
[CACHE_CONFIG.key]: { | ||
version: CACHE_CONFIG.version, | ||
lastUpdated: Date.now(), | ||
data: {}, | ||
searchIndex: {} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the current cache from local storage. | ||
*/ | ||
async function getCache() { | ||
const cache = await chrome.storage.local.get(CACHE_CONFIG.key); | ||
return cache[CACHE_CONFIG.key]; | ||
} | ||
|
||
/** | ||
* Updates the cache with new stock data and refreshes the search index. | ||
* @param {Object} newData - The new stock data to be added to the cache. | ||
*/ | ||
async function updateCache(j) { | ||
const cache = await getCache(); | ||
const updatedCache = { | ||
...cache, | ||
lastUpdated: Date.now(), | ||
data: { | ||
...cache.data, | ||
...newData | ||
} | ||
}; | ||
|
||
// Update search index | ||
Object.entries(newData).forEach(([symbol, details]) => { | ||
const searchTerms = generateSearchTerms(symbol, details.name); | ||
searchTerms.forEach(term => { | ||
if (!updatedCache.searchIndex[term]) { | ||
updatedCache.searchIndex[term] = []; | ||
} | ||
if (!updatedCache.searchIndex[term].includes(symbol)) { | ||
updatedCache.searchIndex[term].push(symbol); | ||
} | ||
}); | ||
}); | ||
|
||
await chrome.storage.local.set({ [CACHE_CONFIG.key]: updatedCache }); | ||
return updatedCache; | ||
} | ||
|
||
/** | ||
* Clears the cache and reinitializes it. | ||
*/ | ||
async function clearCache() { | ||
await initializeCache(); | ||
return { success: true }; | ||
} | ||
|
||
// Search functionality | ||
/** | ||
* Generates a set of search terms from a stock symbol and its name. | ||
* @param {string} symbol - The stock symbol. | ||
* @param {string} name - The stock name. | ||
* @returns {Array} - An array of search terms. | ||
*/ | ||
|
||
/** | ||
* Searches the cache for stocks matching the provided query. | ||
* @param {string} query - The search query. | ||
* @returns {Array} - An array of matching stocks. | ||
*/ | ||
async function handleStockSearch(query) { | ||
try { | ||
if (!query || query.length < 2) return []; | ||
|
||
const cache = await getCache(); | ||
query = query.toLowerCase(); | ||
|
||
// Check if cache needs refresh | ||
if (Date.now() - cache.lastUpdated > CACHE_DURATION) { | ||
await refreshStockList(); | ||
} | ||
|
||
// Search in cache first | ||
const matches = new Set(); | ||
Object.entries(cache.searchIndex).forEach(([term, symbols]) => { | ||
if (term.includes(query)) { | ||
symbols.forEach(symbol => matches.add(symbol)); | ||
} | ||
}); | ||
|
||
// Return matched stocks from cache | ||
return Array.from(matches).map(symbol => ({ | ||
symbol, | ||
...cache.data[symbol] | ||
})).slice(0, 10); // Limit results | ||
|
||
} catch (error) { | ||
console.error('Stock search error:', error); | ||
return []; | ||
} | ||
} | ||
|
||
|
||
// Error handling and logging | ||
/** | ||
* Logs errors to the console with an optional context message. | ||
* @param {Error} error - The error to log. | ||
* @param {string} context - Optional context for the error. | ||
*/ | ||
function logError(error, context = '') { | ||
console.error(`Trade Call Widget Error ${context}:`, error); | ||
// Could implement error reporting service here | ||
} | ||
|
||
// Optional: Performance monitoring | ||
const performanceMetrics = { | ||
searches: 0, | ||
cacheHits: 0, | ||
apiCalls: 0 | ||
}; | ||
|
||
// Optional: Debug logging in development | ||
if (process.env.NODE_ENV === 'development') { | ||
chrome.storage.local.onChanged.addListener((changes, namespace) => { | ||
console.log('Storage changes:', changes); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"manifest_version": 3, | ||
"name": "Trade Call Widget", | ||
"version": "1.0.0", | ||
"description": "A widget for creating and managing trade calls", | ||
"action": { | ||
"default_icon": { | ||
"16": "assets/icons/icon.png", | ||
"48": "assets/icons/icon.png", | ||
"128": "assets/icons/icon.png" | ||
} | ||
}, | ||
"side_panel": { | ||
"default_path": "popup.html" | ||
}, | ||
"permissions": [ | ||
"storage", | ||
"clipboardWrite", | ||
"sidePanel" | ||
], | ||
"web_accessible_resources": [{ | ||
"resources": ["dist/popup.css"], | ||
"matches": ["<all_urls>"] | ||
}], | ||
"icons": { | ||
"16": "assets/icons/icon.png", | ||
"48": "assets/icons/icon.png", | ||
"128": "assets/icons/icon.png" | ||
}, | ||
"background": { | ||
"service_worker": "background.js", | ||
"type": "module" | ||
}, | ||
"author": "Your Name", | ||
"homepage_url": "https://your-website.com" | ||
} |
Oops, something went wrong.