需要帮助调试greasemonkey脚本

时间:2011-05-30 20:30:58

标签: javascript jquery debugging greasemonkey

我在这里很新,就像GM脚本一样新。在这里的一些成员,Brock和其他人的帮助下,我正在取得进展。

我目前遇到调试Greasemonkey脚本的问题,但由于某种原因我没有掌握它。我的第一个问题,使用console.log调试firebug。

有时候我会找到日志,大多数时候我都找不到任何东西,我猜错了。然后尝试使用警报以查看变量值...相同的故事。

我目前正试图在Brock Adams的帮助下获得一个脚本来处理网站上的一些拍卖Trada.net,我们在那里得到了一半以上,但我仍然在JS上打我的脑袋脚本...安静的新体验,如果你采取我习惯于15年前的turbo pascal。:)

嗯,目前这是我在剧本上聚集的内容:

 // ==UserScript==
// @name           bid up to test3
// @include         http://www.trada.net/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==


//--- Create a cell for transmitting the date from page scope to GM scope.
$('body'). prepend ('<div id="LatestJSON_Data"></div>');

var J_DataCell          = $('#LatestJSON_Data');


//--- Eavesdrop on the page's AJAX calls and paste the data into our special div.
unsafeWindow.$('body').ajaxSuccess (
    function (event, requestData)
    {
        J_DataCell.text (requestData.responseText);
    }
);

// **bid function and var's
// **var interval            = 50;
// **var bidClickTimer       = setInterval (function() {BidClick (); }, interval);
// **var numBidClicks        = 0;
// **var A1reset_go          = false;



// **function BidClick1 ()
// **{var //bidBtn1=document.getElementById("ctl00_mainContentPlaceholder_AirtimeAuctionItem1_btn_BidButton");


//**    numBidClicks++;
//**    if (numBidClicks > 10)
//**    {   Alert("check10");
//**        clearInterval (bidClickTimer);
//**        bidClickTimer   = "";
//**    }
//**    else
//**    {   Alert("check11");
//**        bidBtn1.click (1);

//**    }
//**};

//**end bid function

//--- Listen for changes to the special div and parse the data.
J_DataCell.bind ('DOMSubtreeModified', ParseJSON_Data);

function ParseJSON_Data ()
{

//**my var
//**var auction_type ;A1_upto;A1_start;A1_current;A1_reset;
//**end my var

    //--- Get the latest data from the special cell and parse it.
    var myJson              = J_DataCell.text ();
    var jsonObj             = $.parseJSON (myJson);

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

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

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

//** my part
//**   Alert("check1");
//**  auction_type=parseInt (rowValue[4]);
//**   if (auction_type== 1)
//**
//**     {Alert("check2");
//**      A1_upto=parseInt (rowValue[12]);
//**       Alert("check3");
//**      A1_current=parseInt (rowValue[8]);
//**       Alert("check4");
//**      A1_reset=rowValue[16];
//**       if (A1_reset != "null")
//**         {Alert("check5");
//**          A1reset_go='true';
//**          };
//**       if (A1_reset == "null") and (A1reset_go=='true')
//**         {Alert("check6");
//**          A1reset_go=false;
//**          Alert("check7"); 
//**          A1_start=rowValue[8];
//**          };
//**        if  ((A1_current - A1_start) <= (A1_upto - 10))
//**          {Alert("check8");
//**           BidClick1 ();
//**           };
//**      };

//** end my part

      };
    );
}


//--- Format our special cell with CSS.  Add "visibility: hidden;" or "display: none;", if desired.
GM_addStyle ( (<><![CDATA[
    #LatestJSON_Data
    {
        background:         gold;
        border:             3px ridge #0000DD;
        font-size:          10px;
        margin:             0 2em;
        padding:            1ex 1em;
        width:              94%;
        opacity:            0.8;
        overflow:           hidden;
        z-index:            666;
        position:           absolute;
        color:              black;
    }
]]></>).toString () );

基本上,到目前为止,它确实创建了一个单元格,其中显示了放入数组BidDataArray的所有拍卖数据。

我想使用数组中的数据(每秒更新一次)来获取某些数据,然后决定是否点击出价按钮。

在第一次拍卖中,我站了起来。计时器拍卖我让它工作,每隔几秒钟点击一次。

在第一次拍卖中,我基本上想:

  1. 检查它是哪个拍卖,
  2. 了解这是否是第一次竞标,
  3. 获取金额,
  4. 进行计算以开始点击拍卖的最后10次点击。
  5. 重置起始金额。
  6. 听起来很简单,但没有调试器,对Js和GM知之甚少,这让我很忙。我已经尝试将我的Var放在控制台日志中,但无法跟踪它们。可能已经错误地声明了它们,或者错误地使用了它们......但我看不出错误,而且我没有调试器来测试它们。

    java调试器会工作吗?但它没有链接到网站......

    此外,当我将我的部件添加到Brock的代码中的那一刻,它不再显示带有信息的单元格...所以在某处我打破了他的代码我无法找到问题...在我添加任何内容之前代码,它运作良好,然后我添加了我的部分,它不再工作,所以我通过使用“//”把它拿出来。所以脚本应该跳过它,但他的部分不再工作了。我尝试添加“提醒”,但似乎无法找到问题。我的所有部分都标有“// **”,目前应处于非活动状态。

    任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

好吧,我没有从这里得到很多回复,但我已经尝试了...... :(我设法让它调试了90%,但这是我的回答sofar ...将在我找到一个后更新它引起小故障的方式..:

// ==UserScript==
// @name            let's try 3.42
// @include         http://www.trada.net/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==
var auctiontyp = 0;var aupto = 0;var A1_start  = 0;var A1_current = 0;var A1_rest= 'x';
// **bid function and var's
var interval            = 50;
var bidClickTimer       = setInterval (function() {BidClick1 (); }, interval);
var numBidClicks        = 0;
var A1reset_go          = false;

function BidClick1 ()

{var bidBtn1=document.getElementById("ctl00_mainContentPlaceholder_AirtimeAuctionItem1_btn_BidButton");


    numBidClicks++;
    if (numBidClicks > 3)
    {   alert("check10");
       clearInterval (bidClickTimer);
        bidClickTimer   = "";
    }
    else
    {   alert('check11');
        //bidBtn1.click (1);

    }
};



// end bid function

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]);
alert('test3.41');
auctiontyp=parseInt (singleAuctionData[4]);
   if (auctiontyp== 1)

     {
   aupto=parseInt (singleAuctionData[15]);alert('check2.6');
       alert('check3 '+(singleAuctionData[8]));
      A1_current=parseFloat (singleAuctionData[8]);
      alert('check4 '+(singleAuctionData[16]));
      A1_rest=singleAuctionData[16];
       alert(A1_rest);
       if (A1_rest != 'null')
         {alert('check5');
          A1reset_go=true;
          };
       alert('check5.1');
       alert(A1reset_go);
       if (A1_rest == 'null') and (A1reset_go==true)
         {alert('check6');
          A1reset_go=false;
          alert('check7'); 
          A1_start=singleAuctionData[8];
         };
          alert('check7.3');
         alert((A1_current) );
         alert(( A1_start));
         alert((aupto) );
        if  (((A1_current - A1_start)*100) < (aupto - 10))
          {alert('check8');
           //BidClick1 ();
           };

     };
    }
);

我目前在这个剧本上遇到的问题:

1.“BidClick1”函数在脚本的开头运行而没有被调用,我只是在接近结束时调用它,但它没有运行.. 这一部分:

A1_rest=singleAuctionData[16];
           alert(A1_rest);
           if (A1_rest != 'null')
             {alert('check5');
              A1reset_go=true;
              };
           alert('check5.1');
           alert(A1reset_go);
           if (A1_rest == 'null') and (A1reset_go==true)
             {alert('check6');
              A1reset_go=false;
              alert('check7'); 
              A1_start=singleAuctionData[8];
             };

没有正确执行。对于这个数组,“arrayOfAuctions”,在第一个segmant上,我正在进行所有测试,“A1_rest”应该是=到“null”,但它不是,因此它执行:“

if (A1_rest != 'null')
                 {alert('check5');
                  A1reset_go=true;
                  };" 

而且,它不执行此语句: “

 if (A1_rest == 'null') and (A1reset_go==true)
                 {alert('check6');
                  A1reset_go=false;

正确地说,它只应在A1_rest =“null”且A1_reset_go为真时执行。它执行无论哪一个是真的,如果我把它作为:if ((A1_rest == 'null') and (A1reset_go==true)),它根本不运行脚本。

如果any1得到完整答案,我会将其标记为。 谢谢。                   警报( 'check7');                   A1_start = singleAuctionData; [8]                  };“始终执行