So, using the powers of ChatGPT (uh, oh), I managed to get this working in my service worker. Is this the best way, or is there something to do to improve it? My coding sensibilities are telling me this is pretty low-level api stuff that I would've expected to be abstracted into a more coder-friendly method call.
async function loadImageDataFromPath(imagePath: string): Promise<ImageData> {
// Fetch the image from the URL
const response = await fetch(imagePath);
if (!response.ok) {
throw new Error('Failed to load image');
}
// Convert the response into a blob (binary large object)
const blob = await response.blob();
// Create an image bitmap from the blob
const bitmap = await createImageBitmap(blob);
// Create an offscreen canvas to handle the image rendering
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
const ctx = canvas.getContext('2d');
if (!ctx) {
throw new Error('Failed to get canvas context');
}
// Draw the image onto the offscreen canvas
ctx.drawImage(bitmap, 0, 0);
// Extract and return the ImageData from the offscreen canvas
return ctx.getImageData(0, 0, canvas.width, canvas.height);
}
and how to use it:
const rule1 = {
conditions: createAllowedSitesConditions(allowedSites),
actions: [new chrome.declarativeContent.ShowAction(),new chrome.declarativeContent.SetIcon({imageData})]
}