XMLHTTPREQUEST chrome扩展无法正常工作

时间:2017-01-11 10:25:42

标签: javascript php ajax google-chrome google-chrome-extension

我无法使用XMLHTTPREQUEST从Chrome扩展程序后台脚本发送数据,我已启动并运行wampserver,我还尝试了外部链接,例如google。

它做了什么:

  

用户输入权限定义选项卡,后台脚本等待热门   key,当按下一个content_script时启动并生成一个字符串,   将字符串发送回后台脚本,然后发送回背景   脚本应该接收字符串并将其发送到php文件,php   文件应该打印你好,它的简单只是试试看看在哪里   问题,以后php会有更多代码。

     

但它完全不起作用!

更新

  

我试图打包扩展然后通过拖放运行它,它没有   启动php脚本。

     

我尝试卸载chrome,重启然后重新安装但是   没有运气。

     

我还允许 - allow-file-access-from-files

更新2

  

我在调试模式下收到以下错误:

     

extensions :: sendRequest:41:Uncaught TypeError:无法读取未定义的属性'callback'{TypeError:无法读取属性'callback'的   未定义

的manifest.json

style.css

Background.js

{

  "manifest_version": 2,
  "name": "Extractor",
  "version": "1",


  "description": "Extract from 144",
  "icons": { "16": "logo16.png",
           "48": "logo48.png",
          "128": "logo128.png" },


        "page_action": {
          "default_icon": {                    
            "16": "logo16.png",           
            "48": "logo48.png",           
            "128": "logo128.png"            
          },
          "default_title": "Extractor"          
        },

  "background": {

    "scripts": ["background.js"],
    "persistent": true
  },
  "content_scripts": [
    {
      "matches" : ["https://www.msn.com/*"],
      "js" : ["content_script.js"]
    }
  ],
 "permissions": [
    "tabs",
    "https://www.msn.com/*",
    "activeTab",
     "http://localhost/*"

  ],
  "commands": {
           "toggle-feature": {
            "suggested_key": {
              "default": "Ctrl+Shift+1",
              "windows": "Ctrl+Shift+2"
            },

            "description": "Extract now"
          }
        } ,
"web_accessible_resources": ["content_script.js"]

}

content_script.js

chrome.commands.onCommand.addListener(function(command) {
 if (command === "toggle-feature") { 
 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
     for(var i = 0; i<tabs.length;i++) {
           chrome.tabs.executeScript(tabs[i].id, {"file": "content_script.js"});
     }
   });
  }
}); 

chrome.runtime.onMessage.addListener(
  function(message, sender, sendResponse) {

    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://localhost/test/test.php");
    xhttp.send(message.url);

  });

test.php的

var url = 'this is just test' ;
chrome.runtime.sendMessage({ 'url' : url });

2 个答案:

答案 0 :(得分:0)

您只能将Chrome扩展程序的XHR请求发送到代码中manifest.json中定义的网址,您应该http://localhost添加manifest.json

 "permissions": [
    "tabs",
    "https://www.msn.com/*",
    "activeTab",
     "*://*/*",
     "http://localhost/"

  ],

此权限"*://*/*",无效。您必须指定协议(http或https)

更多信息:

答案 1 :(得分:0)

如果您的chrome.extension.sendRequest自Chrome 33以来已被删除,则可能需要检查您的代码。请改用runtime.sendMessage

除此之外,Simple one-time requests指出,如果您只需要将单个消息发送到扩展的另一部分(并且可选地获得响应),则应使用简化的runtime.sendMessage或tabs .sendMessage。

并且,从内容脚本发送请求如下所示:

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});

在接收端,您需要设置一个runtime.onMessage事件监听器来处理该消息。

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });

此外,有人指出:

  

sendResponse回调仅在同步使用时有效,或者如果事件处理程序返回true以指示它将异步响应。如果没有处理程序返回true或者sendMessage回调被垃圾收集,则会自动调用sendResponse函数的回调。

有关如何发送请求和设置事件监听器的详细信息,您可能需要检查Message Passing

相关问题