CLD

运用 background_page , browser_action 以及 tabs

展示每个标签的语言

源码:

  • manifest.json
  {
    "name": "CLD",
    "description": "Displays the language of a tab",
    "version": "0.2",
    "background": {
      "scripts": ["background.js"]
    },
    "permissions": [
      "tabs"
    ],
    "browser_action": {
        "default_name": "Page Language"
    },
    "manifest_version": 2
  }
  • background.js
  // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
  // source code is governed by a BSD-style license that can be found in the
  // LICENSE file.
  var selectedId = -1;
  function refreshLanguage() {
    chrome.tabs.detectLanguage(null, function(language) {
      console.log(language);
      if (language == " invalid_language_code")
        language = "???";
      chrome.browserAction.setBadgeText({"text": language, tabId: selectedId});
    });
  }

  chrome.tabs.onUpdated.addListener(function(tabId, props) {
    if (props.status == "complete" && tabId == selectedId)
      refreshLanguage();
  });

  chrome.tabs.onSelectionChanged.addListener(function(tabId, props) {
    selectedId = tabId;
    refreshLanguage();
  });

  chrome.tabs.getSelected(null, function(tab) {
    selectedId = tab.id;
    refreshLanguage();
  });

资源下载

cld.zip