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