Storing or loggin data with greasemonkey which can be processed later

江枫思渺然 提交于 2019-12-11 00:26:18

问题


I'm curently busy on a site for which i want to write a greasemonkey script that logs all the data, or at least certain postions of it, and saves in an file that will be interperatable fo ms excell. Is that possible? say for example i take this snippet of data:

 {"d":[["","","y","ZAR","1","49517","6458, 8270, 8270, 8270, 7635",null,"1.40","6458","0:13:30","","12","","C","30",null],["y","-00:00","y","ZAR","2","49593","6458, 6458, 6458, 6458, 6458",null,"2.92","6458","0:13:37","","12","","L","12","Ve4mYdrvkkQMKxBH1\/1VMtDTCDQBRspg5jB8jjY08zg="],["","","y","ZAR","3","49058","7456, 9216, 6458, 5153, 7456",null,"194.40","7456","0:00:31","","1100","","T",null,null],["","","y","ZAR","4","49597","2935, 6554",null,"1.22","2935","0:01:16","","12","","T",null,null],["","","y","ZAR","5","49590","4440, 0518, 5343, 2625, 4848",null,"0.95","4440","0:15:58","","5","","L",null,null],["","","y","ZAR","6","49591","4848, 4440, 4440, 0518, 2625",null,"1.81","4848","0:16:05","","12","","L",null,null],["","","y","ZAR","7","49595","6458",null,"5.55","6458","0:04:13","","55","","T",null,null],["","","y","ZAR","8","49596","",null,"2.90","NONE","0:04:35","","29","","T",null,null],["","","y","ZAR","9","49496","6458, 2427, 2427, 7863, 5845",null,"2.56","6458","0:06:07","","10","","B",null,null],["","","y","ZAR","10","49524","6458, 2427, 7863, 7863, 5845",null,"1.67","6458","0:06:00","","5","","B",null,null],["","","y","ZAR","11","49539","6458, 2427, 7863, 7863, 0764",null,"2.02","6458","0:04:25","","10","","B",null,null]]}

, Read it into an 2d array, which Brock Adams helped me with.

var myJson              = '{"d":[["","","y","ZAR","1","49517","6458, 8270, 8270, 8270, 7635",null,"1.40","6458","0:13:30","","12","","C","30",null],["y","-00:00","y","ZAR","2","49593","6458, 6458, 6458, 6458, 6458",null,"2.92","6458","0:13:37","","12","","L","12","Ve4mYdrvkkQMKxBH1\/1VMtDTCDQBRspg5jB8jjY08zg="],["","","y","ZAR","3","49058","7456, 9216, 6458, 5153, 7456",null,"194.40","7456","0:00:31","","1100","","T",null,null],["","","y","ZAR","4","49597","2935, 6554",null,"1.22","2935","0:01:16","","12","","T",null,null],["","","y","ZAR","5","49590","4440, 0518, 5343, 2625, 4848",null,"0.95","4440","0:15:58","","5","","L",null,null],["","","y","ZAR","6","49591","4848, 4440, 4440, 0518, 2625",null,"1.81","4848","0:16:05","","12","","L",null,null],["","","y","ZAR","7","49595","6458",null,"5.55","6458","0:04:13","","55","","T",null,null],["","","y","ZAR","8","49596","",null,"2.90","NONE","0:04:35","","29","","T",null,null],["","","y","ZAR","9","49496","6458, 2427, 2427, 7863, 5845",null,"2.56","6458","0:06:07","","10","","B",null,null],["","","y","ZAR","10","49524","6458, 2427, 7863, 7863, 5845",null,"1.67","6458","0:06:00","","5","","B",null,null],["","","y","ZAR","11","49539","6458, 2427, 7863, 7863, 0764",null,"2.02","6458","0:04:25","","10","","B",null,null]]}'
var jsonObj             = $.parseJSON (myJson);

//--- The JSON should return a 2-D array, named "d".
var arrayOfAuctions     = jsonObj.d;

//--- Loop over each row in the array.
$.each (
    arrayOfAuctions,
    function (rowIndex, singleAuctionData) {

        //--- Print the 7th column.
        console.log ('Row: ' + (parseInt (rowIndex) + 1) + ' Column: 7  Value: ' + singleAuctionData[6]);
    }
);

now i want to write this data of the array over into an file which i can later go and open in excell and go over it. By using GM_setValue, can it be done? How would the easiest way be to go on with it?

Thanks.


回答1:


Greasemonkey cannot write to a file, and using GM_setValue or local storage would be a pain to get into a file. You do not want to store very many values using GM_setValue anyway, since these are stored in the browser preferences.

You mentioned a remote server.
Yes, GM can send the data to a remote server (or even your local machine if it is running something like XAMPP). With a little more work, you could also send the data to Google docs, or similar.

It's not clear what data you want to send (assuming you don't just send the entire arrayOfAuctions).

So, say, you've analyzed the data and determined that you want these columns:

Column 
Index       Meaning / Purpose
------      --------------------------------
   4        Auction display number
   5        Auction ID
   8        Current Price
   9        High bidder ID
  10        Time Remaining
  12        Up to???


Then you could isolate the data of interest like so:

//--- Start custom, 2-D array.
var myAuctionData       = [ [ 'DisplayNumber', 'AuctionID', 'CurrentPrice',
                            'HighbidderID', 'TimeRemaining', 'Upto'
                        ] ];

//--- Loop over each row in the array, storing desired data.
$.each (
    arrayOfAuctions,
    function (rowIndex, singleAuctionData)
    {
        myAuctionData.push ( [
            singleAuctionData[4], singleAuctionData[5],  singleAuctionData[8],
            singleAuctionData[9], singleAuctionData[10], singleAuctionData[12]
        ] );
    }
);


And send it to your server like so:

SerializedAuctionData  = JSON.stringify (myAuctionData);

GM_xmlhttpRequest ( {
    method:     "POST",
    url:        "http://localhost/YourDir/LogAuctionData.php",
    data:       SerializedAuctionData,
    headers:    {"Content-Type": "application/json"}
} );


The server can be using whatever technology you are comfortable with.
If it was a PHP page, it could extract the JSON data like so:

$AuctionData = json_decode($HTTP_RAW_POST_DATA);
print_r ($AuctionData);


The server page could write a csv or xls file, but since lots of data will be posted, once per second, it would probably be smarter to use a mySQL database.



来源:https://stackoverflow.com/questions/6297356/storing-or-loggin-data-with-greasemonkey-which-can-be-processed-later

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!