简体   繁体   English

Chrome扩展程序:chrome.runtime.sendMessage和XMLHttpRequest();

[英]Chrome extension: chrome.runtime.sendMessage and XMLHttpRequest();

I'm having troubles for make a http request using chrome.runtime.sendMessage ( in my content script ) and chrome.runtime.onMessage.addListener ( in my backgroud html page ). 我在使用chrome.runtime.sendMessage (在我的内容脚本中)和chrome.runtime.onMessage.addListener (在我的backgroud html页面中)发出http请求时遇到了麻烦。

The trouble here is that http resquest not is made and i never receives callback responseText correctly, always comes as undefined in chrome.runtime.sendMessage . 这里的麻烦是没有进行http resquest,我从来没有正确接收回调responseText ,总是在chrome.runtime.sendMessage 未定义

So, i want any help for try solve this. 所以,我想要任何帮助尝试解决这个问题。

Here is all my code: 这是我的所有代码:

Content script 内容脚本

chrome.runtime.sendMessage({
    method: "GET",
    action: "xhttp",
    url: "http://www.example.net/echo.php?getecho",
    data: ""
}, function(responseText) {
    alert(responseText);

});

Background page html 背景页面html

<!DOCTYPE html>
<html style=''>
<head>
chrome.runtime.onMessage.addListener(function(request, sender, callback) {
    if (request.action == "xhttp") {
        var xhttp = new XMLHttpRequest();
        var method = request.method ? request.method.toUpperCase() : 'GET';

        xhttp.onload = function() {
            callback(xhttp.responseText);
        };
        xhttp.onerror = function() {

            callback();
        };
        xhttp.open(method, request.url, true);
        if (method == 'POST') {
            xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        xhttp.send(request.data);

        return true; 
    }
});
</script>
</head>
<body>
</body>
</html>

PHP script PHP脚本

<?php
if (isset($_GET["getecho"]))
{       
  echo "Hello i'm php script!";
}
?>

Manifest file 清单文件

{
   "background": {

      "page": "popup.html",
      "persistent": true
   },

"description": "Foo example",
   "manifest_version": 2,
   "name": "Foo",  
   "icons": {
    "128" : "picture/wmp128.png", 
     "48" : "picture/wmp48.png" 
},

"web_accessible_resources": [

   "popup.js"
],

"content_scripts": [ 

{  

   "matches": ["<all_urls>", "*://*/*", "http://*/*", "https://*/*"],
   "js": ["popup.js"],
   "run_at": "document_end",
   "all_frames": true
}

],

   "permissions": [ "tabs", "background", "activeTab", "<all_urls>", "webNavigation", "webRequest", "http://*/*", "https://*/*", "*://*/*" ],
   "version": "2.0"
}

I'm not entirely sure, but it could be an sync vs. async issue. 我不完全确定,但它可能是同步与异步问题。 This explains the difference between the two. 解释了两者之间的区别。 It may be worth a shot. 这可能值得一试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 chrome.runtime.sendMessage在Chrome扩展程序中不起作用 - chrome.runtime.sendMessage not working in Chrome Extension Chrome.Runtime.SendMessage不起作用 - Chrome.Runtime.SendMessage Not Workking Web 扩展 - chrome.runtime.sendMessage 在隐身标签中不起作用 - Web Extension - chrome.runtime.sendMessage not working in incognito tabs Chrome 扩展 :: 尝试将 chrome.runtime.sendmessage 的语法理解为 chrome.runtime.onMessage - Chrome Extension :: Trying to understand syntax of chrome.runtime.sendmessage to chrome.runtime.onMessage 让 Promise 等待 Chrome.runtime.sendMessage - Make Promise wait for a Chrome.runtime.sendMessage 重新加载Chrome扩展程序后,chrome.runtime.sendMessage会从内容脚本中抛出异常 - chrome.runtime.sendMessage throws exception from content script after reloading Chrome Extension Chrome扩展程序和Angular:我可以将chrome.runtime.sendMessage转换为同步功能吗? - Chrome extension and Angular: Can I turn chrome.runtime.sendMessage into a synchronous function? Chrome消息传递:chrome.runtime.sendMessage无法使用最新版本49 - Chrome messaging: chrome.runtime.sendMessage not working on the newest release 49 Chrome版本57.0.2987中的chrome.runtime.sendMessage错误 - chrome.runtime.sendMessage error in Chrome version 57.0.2987 使用chrome.runtime.sendMessage不会在Firefox Web Extension中产生响应 - Using chrome.runtime.sendMessage does not yield a response in Firefox Web Extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM