-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpopup.js
58 lines (55 loc) · 2.03 KB
/
popup.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
document.addEventListener("DOMContentLoaded", () => {
const previewImage = document.getElementById("previewImage");
const captureButton = document.getElementById("captureButton");
const ocrResult = document.getElementById("ocrResult");
captureButton.addEventListener("click", () => {
chrome.runtime.sendMessage({ action: "captureTab" }, (response) => {
if (response && response.imageDataUrl) {
previewImage.src = response.imageDataUrl;
previewImage.style.display = "block";
ocrResult.textContent = "Processing...";
Tesseract.recognize(
response.imageDataUrl,
'eng',
{
logger: m => console.log(m),
workerPath: 'https://unpkg.com/[email protected]/dist/worker.min.js',
corePath: 'https://unpkg.com/[email protected]/tesseract-core.wasm.js'
}
).then(({ data: { text } }) => {
console.log("Extracted Text:", text);
const extractedData = extractData(text);
if (extractedData) {
console.log("Extracted Data:", extractedData);
// Display extracted data in the UI
ocrResult.textContent = `
Trading Symbol: ${extractedData.tradingSymbol}
Month: ${extractedData.month}
Strike Price: ${extractedData.strikePrice}
Strike Type: ${extractedData.strikeType}
`;
} else {
console.log("No matching data found.");
ocrResult.textContent = "No matching data found.";
}
}).catch(err => {
console.error("Error during text extraction:", err);
ocrResult.textContent = "Error during text extraction: " + err.message;
});
}
});
});
});
function extractData(extractedText) {
const match = extractedText.match(/BANKNIFTY\s+(\w+)\s+(\d+)\s+(CE|PE)/);
if (match) {
return {
tradingSymbol: "BANKNIFTY",
month: match[1],
strikePrice: match[2],
strikeType: match[3],
};
} else {
return null;
}
}