3

こんにちは, 私は Google Chrome 拡張機能を開発しています:
これは Google 翻訳 API を使用する辞書です
. ユーザーがページ上のテキストを選択すると、ポップアップが表示され、選択したテキスト定義が表示されます.

私はjsファイルにこのコードを持っています:

var req = new XMLHttpRequest();
req.open(
    "GET",
    "https://www.googleapis.com/language/translate/v2?" +
    "format=html" +
    "q=" + get_text_selection() + // Source Text
"source=en&" + // Source Language
"target=fa&" + // Target Language
true);
req.send(null);

function get_text_selection() {

    if (window.getSelection)

    return window.getSelection();

    if (document.getSelection)

    return document.getSelection();

    if (document.selection)

    return document.selection.createRange().text;

    return '';

}

私のManifest.jsonファイルのこのコード:

{
  "name": "Google Translator",
  "version": "1.0",
  "manifest_version": 2,
  "description": "This Extention helps you to translate text in your page",
  "browser_action": {
    "default_icon": "Dictionary-Book-icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [ "http://*/*", "https://*/*", "tabs" ]
}

そして、私のhtmlファイルのこのコード:

<!doctype html>
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
      body {
        min-width:357px;
        overflow-x:hidden;
      }

    </style>
    <!-- JavaScript and HTML must be in separate files for security. -->
    <script src="popup.js"></script>
  </head>
  <body>
  </body>
</html>

しかし、それは機能していませんか?
私の問題はどこですか?
アドバイスありがとうございます。

4

1 に答える 1

4

まず、現在Google Translate API有料サービスです。を使用するには、からGoogle Translate API取得する必要があります。詳細については、こちらから入手できます。あなたが得た後、あなたは次のようになるはずですAPI keyGoogleAPI keyurl

"https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=fa&q=Hello%20world" // "Hello world" is query here

あなたの場合はどれですか

"https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY" + "&format=html" + "&source=en" +"&target=fa" + "&q=" + get_text_selection()

次のリクエストを使用(ブラウザのアドレスバーからの有効なキーを使用)

"https://www.googleapis.com/language/translate/v2?key=my_valid_key&format=html&q=home&source=en&target=fa" // I've replaced my key with my_valid_key

こんな結果になりました

{  "error": {   "errors": [    {
    "domain": "usageLimits",
    "reason": "dailyLimitExceeded",
    "message": "Daily Limit Exceeded"    }   ],
    "code": 403,   
    "message": "Daily Limit Exceeded"  
  }
}

Google Translate API は無料ではなくなりTranslate API FAQ .

于 2012-11-14T21:10:26.177 に答える