6

我想问是否可以构建 Chrome 或 Greasemonkey 脚本女巫可以打开队列中的所有弹出窗口。到目前为止,我为此有 2 个单独的脚本,但这效果不佳,因为弹出窗口具有反垃圾邮件功能,不允许同时使用太多。

我想做的是以队列方式处理弹出链接数组,并且只有在关闭前一个时才打开下一个。当它归结为队列和任何类型的事件绑定时,我没有经验。

所以我得到的资源:

1)已经准备好的链接数组

var URL_Array = [];

$('form[name="form_gallery"] .img img').each(function(i,e){
    // Format URL array here
    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';
        URL_Array[i] = urlStr;
    }
});

2) 在弹出窗口中对图像进行投票的脚本

    /*######################################################*/  
    var voteBy            = '#vte_mark_12';            // Prefered vote icon
    var voteDefault       = '#vte_mark_5';             // Default vote icon
    var voteFormLoc       = 'image_voting';            // Image voting popups form
    var buyExtraVote      = 'image_voting_buy';        // If run out of votes buy more
    var captchaLoc        = 'input[name="captcha"]';   // Captcha input field
    var captchaTxt        = 'Enter captcha text!';     // Captcha alert text
    var simpatyFormId     = '#sym_send';               // Simpaty window form

    var startScript          = true; 
    var formProcessedAlready = false; // Used to check if image already was voted
    /*######################################################*/  

$(function(){
    if(startScript){
        if($(captchaLoc).length > 0){
            alert(captchaTxt);
            $(captchaLoc).focus().css('border', '2px solid red');
            return false;
        }else{
            if($('#50').length > 0){
                $('#50').attr('checked', true);
                $('form').attr('id', buyExtraVote);
                $('#'+buyExtraVote).submit();
            }else{
                $('form').attr('id', voteFormLoc);
                if($(voteBy).length > 0){
                    $(voteBy).attr('checked', true);
                    setTimeout("$('#"+voteFormLoc+"').submit()", 2000);
                }else if($(voteDefault).length > 0){
                    $(voteDefault).attr('checked', true);
                    setTimeout("$('#"+voteFormLoc+"').submit()", 2000);
                }else{
                    // If we have simpaty box autocast submit
                    if($(simpatyFormId).length > 0){
                        if($(captchaLoc).length > 0){
                            alert(captchaTxt);
                            $(captchaLoc).focus().css('border', '2px solid red');
                            return false;
                        }else{
                            $(simpatyFormId).submit();
                            formProcessedAlready = true;
                        }
                    }else{
                        formProcessedAlready = true;
                    }
                }
            }
        }

        if(formProcessedAlready){
            self.close();
        }
    }
});

据我所知,它应该是这样的:
1)获取所有未投票的网址并形成数组(完成)
2)将所有弹出窗口排队以打开
3)启动第一个弹出窗口
4)投票完成并关闭弹出窗口(完成)
5)开始第二个popup
6)数组完成后切换到下一页(完成)

你认为呢?

4

2 回答 2

2

您可以执行以下操作:

var links = get_your_links();
function process_one() {
    if(links.length > 0) {
        show_popup(links.pop(), process_one);
    }
}
function show_popup(link, callback) {
   var popup = window.open(link, "mywindow", "width=100,height=100");
   $(popup).bind("beforeunload", function() { 
     process_one();
     return true;
   })

}

我希望它有帮助...

于 2011-04-26T17:25:34.823 回答
2
  • 主页和弹出窗口的确切 URL 是什么?
  • 你使用的是什么版本的 jQuery,你是如何包含它的?

确切的 URL 很重要,因为脚本需要同时处理主页和弹出窗口,并且在每个页面上都进行不同的操作。

他们是处理这个问题的两种主要方法。任何一个:

  1. 使用include指令确保脚本在主页和弹出窗口上都运行,但会根据页面类型切换其行为。这将有两个不同的脚本实例同时运行,这不是问题。

  2. 使用include和可能exclude的指令来确保脚本在主页上运行。然后让弹出式打开代码操作表单。


以下是方法1的方法:

(1) 假设主页是这样
    somewhere.com/main/*
    的:并且弹出页面是这样的:
    somewhere.com/window/friend/gallery_view/*
    确保脚本的包含指令在两组页面上都触发。

(2) 确保 jQuery 在这两种页面上都可用。推荐使用 jQuery 1.5.1。jQuery 1.3.2 可能不适用于以下代码。

(3) 然后像下面这样的代码应该可以工作:

var URL_Array   = [];
var PopupQueue  = $({});    //-- jQuery on an empty object - a perfect queue holder

//--- Is this a popup window or the main page?

if ( /\/window\/friend\/gallery_view\//i.test (window.location.href) )
{
    //--- This is a popup page

    /*######################################################*/  
    var voteBy            = '#vte_mark_12';            // Prefered vote icon
    var voteDefault       = '#vte_mark_5';             // Default vote icon
    var voteFormLoc       = 'image_voting';            // Image voting popups form
    var buyExtraVote      = 'image_voting_buy';        // If run out of votes buy more
    var captchaLoc        = 'input[name="captcha"]';   // Captcha input field
    var captchaTxt        = 'Enter captcha text!';     // Captcha alert text
    var simpatyFormId     = '#sym_send';               // Simpaty window form

    var startScript          = true; 
    var formProcessedAlready = false; // Used to check if image already was voted
    /*######################################################*/  

    $(function(){
        if(startScript){
            if($(captchaLoc).length > 0){
                alert(captchaTxt);
                $(captchaLoc).focus().css('border', '2px solid red');
                return false;
            }else{
                if($('#50').length > 0){
                    $('#50').attr('checked', true);
                    $('form').attr('id', buyExtraVote);
                    $('#'+buyExtraVote).submit();
                }else{
                    $('form').attr('id', voteFormLoc);
                    if($(voteBy).length > 0){
                        $(voteBy).attr('checked', true);
                        setTimeout("$('#"+voteFormLoc+"').submit()", 2000);
                    }else if($(voteDefault).length > 0){
                        $(voteDefault).attr('checked', true);
                        setTimeout("$('#"+voteFormLoc+"').submit()", 2000);
                    }else{
                        // If we have simpaty box autocast submit
                        if($(simpatyFormId).length > 0){
                            if($(captchaLoc).length > 0){
                                alert(captchaTxt);
                                $(captchaLoc).focus().css('border', '2px solid red');
                                return false;
                            }else{
                                $(simpatyFormId).submit();
                                formProcessedAlready = true;
                            }
                        }else{
                            formProcessedAlready = true;
                        }
                    }
                }
            }

            if(formProcessedAlready){
                self.close();
            }
        }
    });
}
else
{   //--- This is a main page

    $('form[name="form_gallery"] .img img').each(function(i,e){
        // Format URL array here
        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';
            URL_Array[i] = urlStr;
        }
    });

    //--- Load up the queue.
    $.each (URL_Array, function (PopupNum, PopupURL) {

        PopupQueue.queue ('Popups', function (NextQ_Item) {

            OpenPopupFromQueue (NextQ_Item, PopupNum+1, PopupURL);
        } );
    } );

    //--- Launch the Popups, one at a time.
    PopupQueue.dequeue ('Popups');
}


function OpenPopupFromQueue (NextQ_Item, PopupNum, PopupURL)
{
    var PopupWin    = window.open (PopupURL, "_blank");
    if (!PopupWin)
    {
        console.log ('Bad URL ' + PopupURL)
        setTimeout (function() { NextQ_Item (); }, 2003);
        return;
    }

    /*--- Only after the popup has loaded can we do any processing.
    */
    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
            );

            /*--- We could process the popup here, but it's better to let another instance of this
                script do it, instead.
            */
        },
        false
    );
}


function PopupClosed (NextQ_Item)
{
    //--- Launch the next popup from the queue.
    NextQ_Item ();
}
于 2011-05-01T06:04:54.607 回答