diff --git a/functional-samples/ai.gemini-on-device-language-detection/background.js b/functional-samples/ai.gemini-on-device-language-detection/background.js new file mode 100644 index 0000000000..30ce7a66a1 --- /dev/null +++ b/functional-samples/ai.gemini-on-device-language-detection/background.js @@ -0,0 +1,46 @@ +chrome.runtime.onInstalled.addListener(function () { + chrome.contextMenus.create({ + id: 'context-menu-example', + title: 'Detect language', + contexts: ['selection'] + }); +}); + +chrome.contextMenus.onClicked.addListener(async (info, tab) => { + if (info.menuItemId === 'context-menu-example') { + // Get the selected text + const selectedText = info.selectionText; + console.log('Detecting language for', selectedText); + + if (!('translation' in self) || !('canDetect' in self.translation)) { + console.log('translation API not available'); + return; + } + const canDetect = await self.translation.canDetect(); + let detector; + if (canDetect === 'no') { + console.log('The language detector is not usable.'); + return; + } + if (canDetect === 'readily') { + console.log('The language detector is ready.'); + detector = await self.translation.createDetector(); + } else { + console.log('Downloading language detector.'); + // The language detector can be used after model download. + detector = await self.translation.createDetector(); + detector.addEventListener('downloadprogress', (e) => { + console.log(e.loaded, e.total); + }); + await detector.ready; + } + console.log('Detecting language for: ', selectedText); + const results = await detector.detect(selectedText); + for (const result of results) { + // Show the full list of potential languages with their likelihood, ranked + // from most likely to least likely. In practice, one would pick the top + // language(s) that cross a high enough threshold. + console.log(result.detectedLanguage, result.confidence); + } + } +}); diff --git a/functional-samples/ai.gemini-on-device-language-detection/manifest.json b/functional-samples/ai.gemini-on-device-language-detection/manifest.json new file mode 100644 index 0000000000..1523edcacd --- /dev/null +++ b/functional-samples/ai.gemini-on-device-language-detection/manifest.json @@ -0,0 +1,13 @@ +{ + "manifest_version": 3, + "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwXcgHmijBw+bghyCZXyerwlfzojzNcPpIUeQtI8OBhxLSyxNZgq4wl8v58uGTMMTzvqfNiLj3CT7j+JwG5MJTOCMXf1gSIGZaR53NmE53F5nR7VO/Vb/PhZkBhJbU7x5uQsfVyXyUeZH6BUaAaFRnc4kO5knrL3dfm3WQ9DTMRMIkJ62vx2ebGFV6Uiss3p99wwBbTvJahKpB0dPQZauF/rtBaqyeklaPqpTxVXdmrV3YgNEwA+n3CGnwRDVmhLyzDguOWo6ZI1xhEPxTlIq2GgpmUtbe59R43R/8Sg+nlPH+y6Wy9DvgJy3lUUOUVQDvu6NEqzUjKXtIpHyUcs+YwIDAQAB", + "trial_tokens": [ + "Asfzkynpxnpf5C8XTPv7mqCfsqgOraqVl6UK9VTWWEgMCCc/EpM0sbU+9ae0KxOBHMRg0oN+7v8nf5Z8yTl9ng0AAAB1eyJvcmlnaW4iOiJjaHJvbWUtZXh0ZW5zaW9uOi8va2NrbW1lcGdhaW1raGlranBlYWhlaG9obG1rZmNkYmQiLCJmZWF0dXJlIjoiTGFuZ3VhZ2VEZXRlY3Rpb25BUEkiLCJleHBpcnkiOjE3NDk1OTk5OTl9" + ], + "name": "Built-in AI Text Detection Sample", + "version": "1.0", + "permissions": ["contextMenus"], + "background": { + "service_worker": "background.js" + } +}