Movatterモバイル変換


[0]ホーム

URL:


  1. Mozilla
  2. Add-ons
  3. Browser extensions
  4. JavaScript APIs
  5. tabs
  6. tabs.detectLanguage()

tabs.detectLanguage()

Detects the primary language of the content in a tab, using theCompact Language Detector (CLD).

This is an asynchronous function that returns aPromise.

Syntax

js
let detecting = browser.tabs.detectLanguage(  tabId,                  // optional integer  callback                // optional function)

Parameters

tabIdOptional

integer. Defaults to the active tab of the current window.

callbackOptional

function. Currently, if atabId is specified, this method uses this callback to return the results instead of returning a promise. The callback receives as its only input parameter a string containing the detected language code such asen orfr.

Return value

APromise that will be fulfilled with a string representing an ISO language code such asen orfr. For a complete list of languages supported by this method, seekLanguageInfoTable. For an unknown language,"und" will be returned (but seebug 1288263). If any error occurs the promise will be rejected with an error message.

Examples

Detect and log the language of the active tab when the user clicks a browser action:

js
function onLanguageDetected(lang) {  console.log(`Language is: ${lang}`);}function onError(error) {  console.log(`Error: ${error}`);}browser.browserAction.onClicked.addListener(() => {  browser.tabs.detectLanguage().then(onLanguageDetected, onError);});

Detect and log the language of every open tab when the user clicks a browser action (note that this example requires the "tabs"permission):

js
function onLanguageDetected(url, lang) {  console.log(`Language in ${url} is: ${lang}`);}function onError(error) {  console.log(`Error: ${error}`);}function detectLanguages(tabs) {  for (const tab of tabs) {    browser.tabs      .detectLanguage(tab.id)      .then((lang) => onLanguageDetected(tab.url, lang), onError);  }}browser.browserAction.onClicked.addListener(() => {  browser.tabs.query({}).then(detectLanguages, onError);});

Browser compatibility

Note:This API is based on Chromium'schrome.tabs API. This documentation is derived fromtabs.json in the Chromium code.

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp