我对浏览器扩展完全陌生。我已经成功地完成了一个自动更改网页内容的简单扩展,现在我希望它能够在查询服务器的真值或假值之后更改该内容。我试图用AJAX请求(纯javascript)来实现这一点。由于某些原因,我无法从与浏览器扩展交互的服务器上的PHP脚本中获取任何信息。
清单:
{
"manifest_version": 2,
"name": "Truth Seeker",
"version": "1.0",
"description": "Returns true.",
"icons": {
"48": "icons/icon.png"
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["script.js"]
}
]
}剧本:
theBool = false;
url = /* PHP URL HERE */;
string = "";
request(MakeOutput, url, string);
if(theBool == true){
alert("is true");
}
function MakeOutput(x){
if(x == "true"){
theBool = true;
}
}
function request(fix, url, string){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
fix(xhttp.responseText);
}
}
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(string);
}服务器上的PHP文件目前只是回响“真”。如果我直接访问该PHP脚本(通过在浏览器中键入URL ),它将发出警报“是真的”。为什么它会这样做,但没有其他网页会这样做?
发布于 2017-12-10 20:09:04
嗯,我把我的头发扯了一会儿,放弃了自己,问了一个问题,然后在几分钟内自己想出了答案。为了在未来帮助其他任何人,我在清单中添加了以下内容:
"permissions": [
"webRequest",
"<all_urls>"
],https://stackoverflow.com/questions/47742888
复制相似问题