【问题标题】:XMLHTTPREQUEST chrome extension not workingXMLHTTPREQUEST chrome 扩展不起作用
【发布时间】:2017-05-26 02:12:03
【问题描述】:

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

它有什么作用:

用户进入一个权限定义的tab,后台脚本等待hot 键,按下时会启动 content_script 并生成一个字符串, 字符串被发送回后台脚本,然后后台 脚本应该接收字符串并将其发送到一个 php 文件,即 php 文件应该打印你好,它很简单,只是想看看在哪里 问题,后面的php会有更多的代码。

但它完全不起作用!

更新

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

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

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

更新 2

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

extensions::sendRequest:41:未捕获的 TypeError:无法读取未定义的属性“回调”{TypeError:无法读取属性“回调”的 未定义

Manifest.json

{

  "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"]

}

Background.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);

  });

content_script.js

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

test.php

echo "hello";

【问题讨论】:

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


    【解决方案1】:

    您只能从 chrome 扩展向您在代码中的 manifest.json 中定义的 URL 发出 XHR 请求,您应该将 http://localhost 添加到您的 manifest.json

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

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


    更多信息:

    【讨论】:

    • 添加了警报它可以工作,但是 php 文件没有 os 我猜 xmlhttprequest 有问题
    【解决方案2】:

    如果有自 Chrome 33 以来已弃用的 chrome.extension.sendRequest,您可能需要检查您的代码。请改用 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 或 sendResponse 回调被垃圾回收,sendMessage 函数的回调将被自动调用。

    有关如何发送请求和设置事件侦听器的更多信息,您可能需要查看Message Passing

    【讨论】:

      【解决方案3】:

      XMLHttpRequest 在后台脚本中不起作用,因为后台脚本是服务工作者:

      查看相关:https://stackoverflow.com/a/37114241/6942857

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-24
        • 2012-01-22
        • 2012-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-20
        相关资源
        最近更新 更多