【发布时间】: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