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 website and if they was not voted on before we open voting popup provided by portal (max 20 images at time).
Second script: Sets vote for image and submits form, after form submits closes popup.
The problem here is that after some time popup window sets captcha field to prevent this kind of stuff :). So the real question is: Can I combine two scripts into one and in my loop wait for popup to open and check if captcha field is there? I could then stop loop and tell user to manually enter captcha and reload page after that. Currently (ofc) script will just run and open popups (how can i wait in loop for popup to open and load DOM) ?
Bah, really hard to explain, please tell me if need any more info.
EDIT: Ok let's change it up: How to open popup in loop and wait for callback from popup script and only then depending on return status proceed?
All GM script:
// ==UserScript==
// @name Open image popups and vote
// @namespace Popuopener
// @description Open image popups and vote
// @include http://example.com/friend/*
// @include http://example.com/window/friend/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
// Loop options
var start_Script = true;
var loop = 0;
// Photo rating options
var formProcessedAlready = false;
if(start_Script){
$('form[name="form_gallery"] .img img').each(function(i,e){
if($(this).closest('.object').children('.phs_voted_count').length == 0){
var string = e.src;
var nowBrake = string.substring(string.length-7,7);
var splited = nowBrake.split('/');
var urlStr = '/window/friend/gallery_view/'+splited[3]+'/'+splited[4]+'.html';
wondows = window_open(urlStr, 700, 630);
if(wondows.getElementById("vte_mark_5")){ // Trows me with wondows.getElementById is not a function
alert("Popup loaded"); // Does not getting so far
}else if(wondows.getElementsByName("captcha")[0]){
alert("Captcha inside"开发者_如何转开发);
return false;
}
if($('input[name="captcha"]').length > 0){
alert('Enter captcha then (F5)!');
return false;
}else{
if($('#vte_mark_5').length > 0){
$('#vte_mark_5').attr('checked', true);
$('form[name="popup_form"]').submit();
}else{
formProcessedAlready = true;
}
if(formProcessedAlready){
window.close();
}
}
}else{
loop++;
}
alert("End");
}
}
function window_open(url, width, height, name, scroll_bar, location){
_top = Math.ceil((screen.height - height)/2);
_left = Math.ceil((screen.width - width)/2);
if(scroll_bar != true){
var scroll = 'no';
}else{
var scroll = 'yes';
}
if(typeof location != "undefined" && location == true){
var location = 'yes';
}else{
var location = 'no';
}
if(name == undefined){
name = Math.round(999999 * Math.random());
}
return window.open(url, name , 'width=' + width + ',height=' + height + ',location='+ location +',status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=' + scroll + ',top=' + _top + ',left=' + _left);
}
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:
var PopupQueue = $({}); //-- 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...
function OpenPopupFromQueue (NextQ_Item, PopupNum, PopupURL)
{
var PopupWin = 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...
function PopupMain (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...
function PopupClosed (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.
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 :-?
精彩评论