HyperFlow / extension /src /content.ts
Gaurav711's picture
Deploy 891f179
2b86c60 verified
Raw History Blame Contribute Delete
1.05 kB
// Runs on supported store pages. Reads only the page the user opened and sends parsed, validated
// data to the extension worker; it never makes network requests.
import { PARSERS, storeForUrl } from './parsers';
async function sha256(text: string): Promise<string> {
const bytes = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)));
return [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('');
}
async function capture(): Promise<void> {
const store = storeForUrl(new URL(location.href));
if (!store) return;
const parser = PARSERS[store];
const now = new Date();
const locationKey = await sha256(store + '|' + (parser.deliveryArea(document) ?? 'unknown'));
const prices = parser.parseSeen(document, locationKey, now);
if (prices.length) await chrome.runtime.sendMessage({ type: 'seen', prices });
if (/\/(cart|checkout)/.test(location.pathname)) {
await chrome.runtime.sendMessage({ type: 'cart', cart: parser.parseCart(document, now), locationKey });
}
}
void capture();