import getLogger from "@/utils/getLogger"; import { sendMessage } from "@/utils/messaging"; // This content script is injected into each browser tab. // It's purpose is to retrive an OpenGraph thumbnail URL from the metadata export default defineUnlistedScript({ main }); const logger = getLogger("contentScript"); async function main(): Promise { logger("init"); // This method tries to sequentially retrieve thumbnails from all know meta tags. // It stops on the first thumbnail found. // The order of search is: // 1. // 2. // 3. // 4. const thumbnailUrl: string | undefined = document.querySelector("head meta[property='og:image']")?.content ?? document.querySelector("head meta[name='twitter:image']")?.content ?? document.querySelector("head link[rel=thumbnail]")?.href ?? document.querySelector("head link[rel=image_src]")?.href; if (thumbnailUrl) { logger(`Found thumbnail for "${document.location.href}"`, thumbnailUrl); await sendMessage("addThumbnail", { url: document.location.href, thumbnail: thumbnailUrl }); } else logger(`No thumbnail found for "${document.location.href}"`); logger("done"); }