Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language detection sample #1354

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
Original file line number Diff line number Diff line change
@@ -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"
}
}