Skip to content Skip to sidebar Skip to footer

How To Open Popup In A Loop And Execute Callback Accordingly To Return Status? (greasemonkey Script)

I couldn't formulate proper title for this questions so I'll describe the idea/problem here. So, currently I have 2 scripts for a task: First script: Loops all images on specific w

Solution 1:

Popup windows load and close asynchronously, so things like "wondows.getElementById("vte_mark_5")" will not work right away. You need wait for the popup's load event.

In Greasemonkey, you would use something like: PopupWin.addEventListener("load",...) (Which is best practice anyway, versus onload=....)

Likewise, an ordinary loop would just open all of the popups at once. To open the popups sequentially, use a queue. Since you are already using jQuery, switch to version 1.5.1 to use jQuery's queue functions. Note that Greasemonkey works with jQuery 1.5.1 as of GM version 0.9.

~~~ Suppose you had an array of popup URL's...

var URL_Array   = [ "http://jsbin.com/omuvu5/#111",
                    "http://jsbin.com/omuvu5/#222",
                    "http://jsbin.com/omuvu5/#333"
                ];

Then you could establish a queue with:

varPopupQueue  = $({});    //-- jQuery on an empty object - a perfect queue holder//--- Load up the queue.
$.each (URL_Array, function (PopupNum, PopupURL) {

    PopupQueue.queue ('Popups', function (NextQ_Item) {
        OpenPopupFromQueue (NextQ_Item, PopupNum+1, PopupURL);
    } );
} );

OpenPopupFromQueue opens a popup and sets up the open and close event handlers...

functionOpenPopupFromQueue (NextQ_Item, PopupNum, PopupURL)
{
    varPopupWin    = window.open (PopupURL, "_blank");

    /*--- Only after the popup has loaded can we do any processing.
        See PopupMain() for examples of manipulation via jQuery.
    */PopupWin.addEventListener (
        "load",
        function () {
            /*--- Setup the listener for when the popup has closed.
                We fire the next popup from the queue, there.
            */PopupWin.addEventListener (
                "unload",
                function () {
                    PopupClosed (NextQ_Item);
                },
                false
            );

            //--- Now process the popup, as desired.PopupMain (PopupWin, PopupNum);
        },
        false
    );
}

The load-event handler shows how you can manipulate the popup window using the main page's (GM's) jQuery...

functionPopupMain (PopupWin, PopupNum)
{
    //--- This manipulates the main window.
    $("#PopupStatus").append ("<li>Popup Loaded.</li>");

    //--- This manipulates the popup window.
    $("#PayloadTarget", PopupWin.document).prepend ('<h3>This is popup number: ' + PopupNum + '.</h3>');
}

The close-event handler must trigger the next item in the queue...

functionPopupClosed(NextQ_Item)
{
    NextQ_Item ();
}

Finally, you fire the queue off with:

PopupQueue.dequeue ('Popups');

You can see a demo that puts it all together at jsBin.com.

Solution 2:

This is tantamount to spam o.O But ehh, been there.

wnd=window_open('etc');
if(wnd.getElementById('recaptcha_response_field')){
}else{
};

Assuming recaptcha is your problem and that's how you open the popup window :-?

Post a Comment for "How To Open Popup In A Loop And Execute Callback Accordingly To Return Status? (greasemonkey Script)"