开发者

Need to Click Popup with Gm script that is not always there

i'm still having a problem clicking a popup button on an auction site,that appears only if u won an auction. This popup seems tbe a problem. Ive managed to get help partially in Need to click a bid button with Grease monkey script, i'm able to get the bid buttons clicked, but the popu is stil a problem. The xpath for the popup is:

.//*[@id='ctl00_mainContentPlaceholder_Button3']

And the script i'm using currently is:

// 开发者_开发技巧==UserScript==
// @name           click popup try1.3
// @include         http://www.trada.net/*

// ==/UserScript==


//  ctl00_mainContentPlaceholder_Button3

function PopClick ()
{var PopBtn1=document.getElementById("ctl00_mainContentPlaceholder_Button3");




alert('try1');      
PopBtn1.click (1);
alert('finished');
};

PopClick();

But the problem seems to be that the script doesnt stay active on the page waiting for the popup, I think if i can get it to "wait" for the popup to appear, it should work. I'm very new to GM, so excuse if there is simple errors. I had exelent help from people like Brock sofar, who is showing me the ropes. Slowly but surely I'm gettin the hang of it. Remove the alerts, i just used them to see if it executing.


The simplest solution would be to run this function, say every second, thus "waiting" for the popup to appear:

setInterval(PopClick, 1000);

It is also better to rewrite PopClick to check if the element is there, before calling click, like this:

function PopClick () {
    var PopBtn1=document.getElementById("ctl00_mainContentPlaceholder_Button3");
    if(PopBtn1) {
        PopBtn1.click ();
        // It is also makes sense to clear interval here. see docs for setInterval/clearInterval please :)
    }
};

May be this will help you with the freezing issue.

The more proper way, however, would be to set up MutationEvent listener. Since you are using Firefox, it should work OK:

function click_if_popup(evt) {
    if(evt.target.hasAttribute('id') && evt.target.getAttrubute('id') =="ctl00_mainContentPlaceholder_Button3")
    evt.target.click();
}

document.addEventListener('DOMNodeInsertedIntoDocument', click_if_popup);

Sorry, I didn't test any of this code: I wanted just to give you the general idea of where to dig.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜