Google chrome extension 第二节将会是什么

Google chrome extension 第二节将会是什么,google-chrome-extension,Google Chrome Extension,我已经创建了一个谷歌浏览器扩展。 它在清单版本1中工作正常 manifest.json { "browser_action": { "default_icon": "icon.png", "default_title": "cc.cr URL shortner", "popup": "popup.html" }, "description": "Simple URL shortener with really short URLs."

我已经创建了一个谷歌浏览器扩展。 它在清单版本1中工作正常 manifest.json

    {
   "browser_action": {
      "default_icon": "icon.png",
      "default_title": "cc.cr URL shortner",
      "popup": "popup.html"
   },
   "description": "Simple URL shortener with really short URLs.",
   "icons": {
      "128": "logo.png"
   },
     "name": "CC.Cr Url shortener extansion",
   "permissions": [ "tabs", "contextMenus", "http://cc.cr/" ],
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "0.1"
}
{
  "name": "cc.cr",
  "version": "1.0",
  "manifest_version": 2,
  "description": "URL shortner",
  "icons": { "128": "logo.png", "16": "icon.png" },
  "browser_action": {
      "default_icon": "icon.png",
      "default_title": "cc.cr URL shortner",
      "default_popup": "popup.html"
   },
  "permissions": [ "tabs", "contextMenus", "http://cc.cr/" ],
  "homepage_url": "http://cc.cr"
}
但当我在浏览器中使用清单版本2时 manifest.json

    {
   "browser_action": {
      "default_icon": "icon.png",
      "default_title": "cc.cr URL shortner",
      "popup": "popup.html"
   },
   "description": "Simple URL shortener with really short URLs.",
   "icons": {
      "128": "logo.png"
   },
     "name": "CC.Cr Url shortener extansion",
   "permissions": [ "tabs", "contextMenus", "http://cc.cr/" ],
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "0.1"
}
{
  "name": "cc.cr",
  "version": "1.0",
  "manifest_version": 2,
  "description": "URL shortner",
  "icons": { "128": "logo.png", "16": "icon.png" },
  "browser_action": {
      "default_icon": "icon.png",
      "default_title": "cc.cr URL shortner",
      "default_popup": "popup.html"
   },
  "permissions": [ "tabs", "contextMenus", "http://cc.cr/" ],
  "homepage_url": "http://cc.cr"
}
有人能告诉我清单2中的错误在哪里吗? 我在这个小文件上花了15个多小时( 这是 popup.html

<html>
<script>
/*   XML request to get shortened URL    */
function shoretenUrl(url, responseFunction) {
        // verify URL
        if (url.length < 5 || !url.match(/^https?:\/\//)) {
           (responseFunction)({"error": true, "message": "Invalid URL."});
        return;
}
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://cc.cr/crx.php", true);
        xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            try {
                var response = JSON.parse(xhr.responseText);
                if (response.surl == undefined) {
                    throw response.message;
                }
            } catch (e) {
                (responseFunction)({"error": true, "message": e});
                return;
            }

            (responseFunction)({"error": false, "url": response.surl});
        }
    };
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("url="+ encodeURIComponent(url));
}

/*   * Shorten tab URL   */
function shortenTabUrl(tab) {
    var loader = document.getElementById("loader");
    var error = document.getElementById("error");
    var input = document.getElementById("shortUrl");
    var copied = document.getElementById("copied");
    loader.style.display = "";
    copied.style.display = "none";
    shoretenUrl(tab.url, function(response) {
        loader.style.display = "none"; // hide loader

        // error message
        if (response.error) {
            error.style.display = "block";
            error.innerText = response.message;
            input.value = "";
            return;
        }   error.style.display = "none";

        // get response URL and copy
        input.style.display = "";
        input.focus();
        input.value = response.url;
        input.select();
        try {
            document.execCommand("Copy");
            copied.style.display = "";
        } catch(e) {}       });     }

/*   * Initiate shortening process   */
function init() {
    chrome.tabs.getSelected(null, shortenTabUrl);
}

</script> 

</head>
  <body onload="init();">
<h1>CC.Cr</h1>
<p>Short URL:</p>
<p id="error"></p>
<p>
    <input type="text" id="shortUrl"/>
    <img src="loader.gif" id="loader" alt="" />
</p>
<p id="copied">
    Copied to clipboard.
</p>
  </body>
</html>

/*获取缩短URL的XML请求*/
函数shoretenUrl(url,responseFunction){
//验证URL
如果(url.length<5 | | |!url.match(/^https?:\/\/)){
(responseFunction)({“error”:true,“message”:“Invalid URL.”});
返回;
}
var xhr=new XMLHttpRequest();
xhr.open(“POST”http://cc.cr/crx.php“,对);
xhr.onreadystatechange=函数(){
if(xhr.readyState==4){
试一试{
var response=JSON.parse(xhr.responseText);
if(response.surl==未定义){
抛出响应消息;
}
}捕获(e){
(responseFunction)({“error”:true,“message”:e});
返回;
}
(responseFunction)({“error”:false,“url”:response.surl});
}
};
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
发送(“url=“+encodeURIComponent(url));
}
/**缩短选项卡URL*/
功能短柱(选项卡){
var loader=document.getElementById(“加载器”);
var error=document.getElementById(“错误”);
var input=document.getElementById(“shortUrl”);
var copied=document.getElementById(“复制”);
loader.style.display=“”;
copied.style.display=“无”;
shoretenUrl(tab.url,函数(响应){
loader.style.display=“无”//隐藏加载程序
//错误消息
if(response.error){
error.style.display=“block”;
error.innerText=response.message;
input.value=“”;
返回;
}error.style.display=“无”;
//获取响应URL并复制
input.style.display=“”;
input.focus();
input.value=response.url;
input.select();
试一试{
文件。执行命令(“副本”);
已复制。style.display=“”;
}捕获(e){}};}
/**启动缩短流程*/
函数init(){
chrome.tabs.getSelected(null,shortentabur);
}
CC.Cr
短网址:

复制到剪贴板。


一个潜在的问题是,使用清单2时,您需要在线脚本。让它工作的一种方法是使用脚本的内容创建一个单独的
popup.js
文件,然后将
popup.html
更改为如下所示:

<html>
<head>
    <script type="text/javascript" src="popup.js"></script>
</head>
  <body>
    <h1>CC.Cr</h1>
    <p>Short URL:</p>
    <p id="error"></p>
    <p>
      <input type="text" id="shortUrl"/>
      <img src="loader.gif" id="loader" alt="" />
    </p>
    <p id="copied">
      Copied to clipboard.
    </p>
  </body>
</html>