Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Google Translate开发Google Chrome扩展_Javascript_Json_Rest_Google Chrome Extension - Fatal编程技术网

Javascript 使用Google Translate开发Google Chrome扩展

Javascript 使用Google Translate开发Google Chrome扩展,javascript,json,rest,google-chrome-extension,Javascript,Json,Rest,Google Chrome Extension,您好,我正在开发Google Chrome扩展: 这是一本使用Google translate API的词典,当用户在页面上选择一个文本时,我希望出现一个弹出窗口,显示所选的文本定义, 我的js文件中有以下代码: var req = new XMLHttpRequest(); req.open( "GET", "https://www.googleapis.com/language/translate/v2?" + "format=html" + "q=" + g

您好,我正在开发Google Chrome扩展:
这是一本使用Google translate 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>

入门扩展的弹出窗口
身体{
最小宽度:357px;
溢出x:隐藏;
}
但它不起作用了?
我的问题在哪里?

谢谢你的建议。

首先
谷歌翻译API
现在是一个很好的工具。要使用
Google Translate API
,您需要从
Google
获取
API密钥,您可以从获取更多信息。在获得
API键后
您的
url
应该是

"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()
使用以下请求(使用浏览器地址栏中的有效密钥)

我得到了这个结果

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

相关:。但是您还需要知道如何在活动页面和弹出窗口之间进行通信。我肯定也有人问过这个问题,但我现在找不到。。。
{  "error": {   "errors": [    {
    "domain": "usageLimits",
    "reason": "dailyLimitExceeded",
    "message": "Daily Limit Exceeded"    }   ],
    "code": 403,   
    "message": "Daily Limit Exceeded"  
  }
}