2011-04-24 3 views
5

ChromeまたはGreasemonkeyスクリプトの魔女がすべてのポップアップをキューに開くことができるかどうか尋ねたいと思います。これまでは2つの別個のスクリプトがありますが、ポップアップには同時にスパム対策機能がないので、うまく機能しません。ポップアップを使用したChrome userscriptのjQueryキュー

私がしたいのは、キューファッションのポップアップリンクの配列を処理し、前のものが閉じられたときだけ次に開くことです。私はキューやイベントバインドの種類になるときにexpirienceを持っていません。だから、

私が得たリソース:

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)すべてのunvotedのURLを取得し、フォームの配列を(完了)
2)を開くためにすべてのポップアップをキュー
3)最初のポップアップを起動
4)投票行われ、ポップアップが閉じ(行う)
5)第二のポップアップ
6)アレイ終了スイッチ次ページにスタート(完了)

あなたはどう思いますか?

+1

私は良くそれを1つのポップアップが行われ、そのいずれかのウイルス対策プログラムがそれをブロックwilln't。最初のポップアップを開始 - >投票が完了し、ポップアップが新しいフォームで更新されます - > ... –

+0

毎回新しいソリューションがあります。私はこれを使用する人だけになるので、私はブロックすることについて心配しない。 – arma

答えて

2
  • は、正確なURLのメインページ(複数可)の、さらにポップアップのは何ですか?
  • あなたはどのバージョンのjQueryを使用していますか?どのように組み込んでいますか?

正確なURLは、メインページとポップアップの両方を処理する必要があり、それぞれが異なる動作をする必要があるため、重要です。

これを処理する主な方法は2つあります。次のいずれかの

  1. 使用includeディレクティブは、スクリプトがメインページやポップアップの両方で実行されますが、ページの種類に応じて、その動作を切り替えていることを確認します。これは同時に実行されているスクリプトの2つの異なるインスタンスを持ちますが、問題はありません。

  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(); 
} 
+0

ああ、Brock hello。はい、あなたのスクリプトはうまく見えましたが、クロムで私は単にUncaught TypeError:メソッド 'addEventListener' of undefined'を呼び出せません。私は1.5.2バージョンを使用しています。スクリプトに貼り付けました。 メインページのURL:http:// friends.example.com/friend/28r6afk4bf/gallery/3044094.html'と私は 'http://friends.example.com/friend/*/gallery/* 'ポップアップは' http:// friends.example.com/window/friend/gallery_view/28r6afk4bf/a4w01kb3i0bg.html'です。これは 'http:// friends.boomtime.lv/window/friend/*'と一致します – arma

+0

さて、私はコードを微調整しました。今は悪いURLでエラーになってはいけません。スクリプトはうまく見えましたが、エラーが発生しましたか?それはFirefoxでうまくいったのでしょうか?または、それはいくつかのポップアップのために働いてからエラーが発生しましたか?私のテストはうまくいきます。実際のターゲットページを知らなくても、私はさらにデバッグできません。 –

+0

これは、1つのポップアップを行い、コンソールの 'Bad URL/window/friend/gallery_view/28r6afk4bf/cxa4w065bnz1bg.html'に吐きます。ターゲットページの意味 – arma

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; 
    }) 

} 

を...

+0

すべてがうまく見えて、最初のポップアップで作業していますが、他のポップアップは処理されません。おそらく '$(window).bind'はポップアップではなくメインページである現在のウィンドウをバインドするためです。私は処理するためにリフレッシュする必要があります。 '$(popup).bind'のようにバインドする方法は? – arma

+0

あなたは正しいです、それはエラーだった、答えを更新しました。 – iwiznia

+0

最初のページが読み込まれるだけでまだ実行されます。ポップアップが失われていて、そのイベントを捕らえることができないように見えます。スクリプトのメインページとポップアップアドレスの両方に '// @ match'があります。 – arma