问题
I am creating a Greasemonkey script where I am calculating six variables (time, move, scroll, sav, prin, book and url).
I need to send these variables' data to my PHP page so that these could be inserted in a MySQL table using a WAMP server.
Please, can anyone give the exact code to it as I am new to all this?
My Greasemonkey script is:
{var ajaxDataObj = {
s: sav,
p: prin,
b: book,
t: finalTime,
u: url,
a: totalScroll,
b: tot
};
var serializedData = JSON.stringify (ajaxDataObj);
GM_xmlhttpRequest ( {
method: "POST",
url: "localhost/anuja/greasemonkey.php",
data: serializedData,
headers: {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
} }
and php side is:
$jsonData = json_decode($HTTP_RAW_POST_DATA);
echo jsonData.u;
this code is not running.. Plus I try to check if my variable u
has been passed using jsonData.u
, but it just echoes "jsonData.u".
回答1:
I just create userscript to make Greasemonkey scrape data from webpage opened and POST parameters to the PHP script hosted on my local XAMPP server which could run local Python scripts to automate works.
This also a cool method for scrping data from Javascript rendered pages, which is hard for Python scraper, even better than Selenium :P
The parameters are seperated by &
in this PHP sample url:
http://www.sciencedirect.com/search?qs=Vascular%20stent&authors=&pub=&volume=&issue=&page=&origin=home&zone=qSearch&offset=1200
GM script part:
// @grant GM_xmlhttpRequest
unsafeWindow.sendPhp2Py = function(){
//var ytitle = 'Youtube - ' + document.querySelector('div.yt-user-info').textContent.trim();
var yurl = document.location.href;
//console.info(ytitle);
//console.info(yurl);
var ret = GM_xmlhttpRequest({
method: "POST",
url: "http://localhost/php_run_py.php",
//data: "ytitle="+ ytitle + "&yurl="+ yurl,
data: "yurl="+ yurl,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
console.log(response);
// readyState 4 = complete
// status = 200 OK
if(response.readyState == 4 && response.status == 200){
document.querySelector('#myPhpPyBtn').textContent = 'Sent to PHP!';
}
},
onerror: function(e){
console.log(e);
document.querySelector('#myPhpPyBtn').textContent = 'PHP not connected';
}
});
};
PHP scipt:
<?php
echo $_POST['yurl'];
//传递多个参数如下可以行得通 参数里包含空格哦也可以!!赞 Multiple parameters pass
//echo shell_exec("python test.py \"".$_POST['ytitle']."\" \"".$_POST['yurl']."\"");
//echo shell_exec("python GreaseMonkey_Php_Youtube_srt_generator.py ".$_POST['yurl']);
//更安全 Safer
//system(escapeshellcmd("python test.py \"".$_POST['ytitle']."\" \"".$_POST['yurl']."\""));
system(escapeshellcmd("python GreaseMonkey_Php_Youtube_srt_generator.py ".$_POST['yurl']));
?>
Python test script:
#coding=utf-8
import sys
f = open("test.txt", "a+")
f.write(sys.argv[1] + "\n" + sys.argv[2]+ "\n")
f.close()
print ("some output")
Hope it can help!
来源:https://stackoverflow.com/questions/15040849/how-to-send-data-from-greasemonkey-to-a-php-wamp-server