这是在点击浏览器操作后运行的代码,当我只是在浏览器窗口中粘贴GET请求网址时,php脚本工作正常,但如果使用扩展名,则扩展的所有其他部分工作正常无效。
chrome.extension.onMessage.addListener(function(request, sender)
{
if (request.action == "getSource")
{
message.innerText = request.source;
var httpRequest;
function makeRequest(url)
{
if (window.XMLHttpRequest)
{ // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{ // IE
try
{
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest)
{
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open('GET', 'http://example.com/Data.php?fname=Henry', true);
httpRequest.send();
}
function alertContents()
{
if (httpRequest.readyState === 4)
{
if (httpRequest.status === 200)
{
alert(httpRequest.responseText);
}
else
{
alert('There was a problem with the request.');
}
}
}
}
});
function onWindowLoad() {
var message = document.querySelector('#message');
chrome.tabs.executeScript(null, {
file: "getPagesSource.js"
}, function() {
// If you try and inject into an extensions page or the webstore/NTP you'll get an error
if (chrome.extension.lastError) {
message.innerText = 'There was an error injecting script : \n' + chrome.extension.lastError.message;
}
});
}
window.onload = onWindowLoad;
这是manifest.json文件
{
"name": "Get pages source",
"version": "1.0",
"manifest_version": 2,
"description": "Get pages source from a popup",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["activeTab", "<all_urls>","http://*/"]
}
编辑:这是我的php脚本,无论何时访问页面,它都能正常工作
<?php
$servername = "fdb13.biz.nf";
$username = "1806447_gurbhu";
$password = "ggth0719";
$dbname = "1806447_gurbhu";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$nH=$_GET["fname"];
//insert data to notesPr
$sql = "INSERT INTO derp (raceId, raceLink)
VALUES ('1', '".$nH."')";
$conn->query($sql);
$conn->close();
?>