2013-02-23 132 views
0

我正在創建一個Greasemonkey腳本,用於計算六個變量(時間,移動,滾動,sav,prin,book和url)。如何將數據從Greasemonkey發送到PHP(WAMP)服務器?

我需要將這些變量的數據發送到我的PHP頁面,以便可以使用WAMP服務器將這些數據插入到MySQL表格中。

請問,任何人都可以提供確切的代碼給它,因爲我是新手嗎?

我的Greasemonkey腳本是:

{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. 
} } 


和PHP方面是:

$jsonData = json_decode($HTTP_RAW_POST_DATA); 

echo jsonData.u; 


這個代碼不運行..另外,我嘗試檢查,如果我的變量u一直通過使用jsonData.u,但它只是回聲「jsonData.u」。

+0

爲什麼你在greasemonkey中實現它,而不是在正常的javascipt(你把它放在你的wamp服務器上)呢? – wimh 2013-02-23 17:07:08

+0

@Wimmel,那將是因爲GM腳本運行在他沒有服務/控制的頁面上。 – 2013-02-23 23:44:54

+1

請參閱http://stackoverflow.com/questions/9401009/greasemonkey-ajax-request-is-not-sending-data – 2013-02-24 02:52:12

回答

0

我只是創建userscript,使Greasemonkey從網頁打開和POST參數刮取數據到我的本地XAMPP服務器上託管的PHP腳本,該腳本可以運行本地Python腳本來自動化作品。

這也爲從Javascript scrping數據陰涼方法生成的網頁,這是很難的Python刮刀,比硒甚至更好:P

的參數由&在這個PHP示例網址分隔:

http://www.sciencedirect.com/search?qs=Vascular%20stent&authors=&pub=&volume=&issue=&page=&origin=home&zone=qSearch&offset=1200 

GM腳本部分:

// @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素文字:

<?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測試腳本:

#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") 

希望它可以幫助!

相關問題