File size: 1,050 Bytes
2b86c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 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();