Files
chrome-extensions-samples/functional-samples/ai.gemini-on-device-alt-texter/background.js
Sebastian Benz a42c4121cc Add new AI samples and update existing to use new API shape (#1474)
* Add new AI samples and update existing to use new API shape

* address comments
2025-05-16 20:43:25 +02:00

42 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'generateAltText',
title: 'Generate alt text',
contexts: ['image']
});
});
async function generateAltText(imgSrc) {
// Create the model (we're not checking availability here, but will simply fail with an exception
const session = await self.LanguageModel.create({
temperature: 0.8,
topK: 1.0,
expectedInputs: [{ type: 'image' }]
});
// Create an image bitmap to pass it to the prompt
const response = await fetch(imgSrc);
const blob = await response.blob();
const imageBitmap = await createImageBitmap(blob);
// Run the prompt
const prompt = [
`Please provide a functional, objective description of the provided image in no more than around 30 words so that someone who could not see it would be able to imagine it. If possible, follow an “object-action-context” framework. The object is the main focus. The action describes whats happening, usually what the object is doing. The context describes the surrounding environment. If there is text found in the image, do your best to transcribe the important bits, even if it extends the word count beyond 30 words. It should not contain quotation marks, as those tend to cause issues when rendered on the web. If there is no text found in the image, then there is no need to mention it. You should not begin the description with any variation of “The image”.`,
{ type: 'image', content: imageBitmap }
];
return await session.prompt(prompt);
}
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === 'generateAltText' && info.srcUrl) {
// Start opening the popup
const [result] = await Promise.allSettled([
generateAltText(info.srcUrl),
chrome.action.openPopup()
]);
chrome.runtime.sendMessage({
action: 'alt-text',
text: result.value === 'fulfilled' ? result.value : result.reason.message
});
}
});