从Chrome扩展访问API


Accessing API from Chrome extension

我有一个网站,我在Google Chrome中添加了它作为扩展。我需要的是,我必须从扩展名访问服务器文件。我需要的是,无论服务器文件输出什么,我都需要访问扩展名中的输出。这是我的宣言.json:

{
    "name": "Calpine Extension",
    "version": "1.0",
    "description": "Log on to calpinemate",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": "icon_128.png"
    },
    "background": {
        "persistent": false,
        "scripts": ["background.js"]
    },
    "browser_action": {
        "default_title": "Test Extension",
        "default_icon": "calpine_not_logged_in.png"
    },
    "externally_connectable": {
        "matches": ["http://calpinemate.com/"]
    }
}

这是我的背景.js

chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.create({ "url": "http://calpinemate.com" });
});

现在我需要的是我有一个服务器文件(index.php),它输出1。现在我需要的是,我想得到这个1的扩展。这就是我想要的文件输出的扩展名。我该怎么做?这是我的index.php

<?php echo 1 ;?>

我试过了。但它仍然只在点击事件上工作。请检查我的代码。这是我的背景.js

   function getGmailUrl() {
   return "http://calpinemate.com/";
   }
   function isGmailUrl(url) {
      return url.indexOf(getGmailUrl()) == 0;
      }
   chrome.browserAction.onClicked.addListener(gotopage);
      function gotopage(){    
      chrome.tabs.query({
       url: "http://calpinemate.com/*",
      currentWindow: true
     }, function(tabs) {
    if (tabs.length > 0) {
        var tab = tabs[0];
        console.log("Found (at least one) Gmail tab: " + tab.url);
        console.log("Focusing and refreshing count...");
        chrome.tabs.update(tab.id, { active: true });
         updateIcon();
    } else {
        console.log("Could not find Gmail tab. Creating one...");
        chrome.tabs.create({ url: getGmailUrl() });
         updateIcon();
    }
      });
    }
        if (chrome.runtime && chrome.runtime.onStartup) {
      chrome.runtime.onStartup.addListener(function() {
       updateIcon();
      });
      } else {
        chrome.windows.onCreated.addListener(function() {
        updateIcon();
           });
         }
     function updateIcon(){
   var req = new XMLHttpRequest();
     req.addEventListener("readystatechange", function() {
         if (req.readyState == 4) {
    if (req.status == 200) {
        localStorage.item=req.responseText;
        //alert(localStorage.item);
        if(localStorage.item==1){
          chrome.browserAction.setIcon({path:"calpine_logged_in.png"});
          chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
          chrome.browserAction.setBadgeText({text:""});   
        }
        else{
        chrome.browserAction.setIcon({path:"calpine_not_logged_in.png"});
        chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
        chrome.browserAction.setBadgeText({text:""}); 
        }
        // We received the data
        //alert("Data: " + req.responseText);
      } else {
        // Handle the error
        alert("ERROR: status code " + req.status);
      }
    }
   });
       req.open("GET", "http://blog.calpinetech.com/test/index.php", true);
      req.send(null);

}

首先请求访问服务器的权限:

// In manifest.json
...
permissions: [
   ...
   "*://calpinemate.com/index.php"
],

然后使用AJAX访问数据:

var req = new XMLHttpRequest();
req.addEventListener("readystatechange", function() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            // We received the data
            alert("Data: " + req.responseText);
        } else {
            // Handle the error
            alert("ERROR: status code " + req.status);
        }
    }
});
req.open("GET", "https://calpinemate.com/index.php", true);
req.send(null);