2012-09-21 26 views
1

我想使用Greasemonkey腳本在頁面中提交表單。 表單中的名稱和密碼是由Firefox記住的,所以當頁面加載時,名稱和密碼已經填滿。所以只需要提交自動登錄。Greasemonkey提交登錄表單,但用戶名和密碼未填寫?

形式代碼:

<form class="enableAutoFocus" method="post" id="login_form" action="http://localhost/plan/login_form"> 
    <div id="login-form"> 
     <input name="came_from" value="" type="hidden"> 
     <input name="next" type="hidden"> 
     <input name="ajax_load" type="hidden"> 
     <input name="ajax_include_head" type="hidden"> 
     <input name="target" type="hidden"> 
     <input name="mail_password_url" type="hidden"> 
     <input name="join_url" type="hidden"> 
     <input name="form.submitted" value="1" type="hidden"> 
     <input name="js_enabled" id="js_enabled" value="0" type="hidden"> 
     <input name="cookies_enabled" id="cookies_enabled" value="" type="hidden"> 
     <input name="login_name" id="login_name" value="" type="hidden"> 
     <input name="pwd_empty" id="pwd_empty" value="0" type="hidden"> 
     <div class="field"> 
      <label for="__ac_name">Login Name</label> 

      <input style="margin-right: 0px; padding-right: 0px;" size="15" name="__ac_name" value="" id="__ac_name" type="text"><img title="Max field length is unknown" style="position:relative; z-index: 999; cursor:pointer; vertical-align: bottom; border: 0; width: 14px; height: 19px; display:none;" class="ife_marker" src="chrome://informenter/skin/marker.png" id="__ac_name_ife_marker_1"> 
    </div> 
    <div class="field"> 
      <label for="__ac_password">Password</label> 
      <input style="margin-right: 0px; padding-right: 0px;" size="15" name="__ac_password" id="__ac_password" type="password"><img title="Max field length is unknown" style="position:relative; z-index: 999; cursor:pointer; vertical-align: bottom; border: 0; width: 14px; height: 19px; display:none;" class="ife_marker" src="chrome://informenter/skin/marker.png" id="__ac_password_ife_marker_2"> 
    </div> 
     <div class="formControls"> 
      <input class="context" name="submit" value="Log in" type="submit"> 
     </div> 
    </div> 
</form> 

在頁面第一種形式是搜索的形式,所以首先我想document.forms[0].submit();
它的工作原理。

然後我的代碼更改爲:

document.forms[1].submit(); 

錯誤控制檯報告:

Error: document.forms[1].submit is not a function 

然後我搜索,發現下面的代碼,並嘗試了。

function ClicktheButton(obj) { 
var evt = document.createEvent("MouseEvents"); 
evt.initMouseEvent("click", true, true, window, 
    0, 0, 0, 0, 0, false, false, false, false, 0, null); 
var canceled = !obj.dispatchEvent(evt);  
} 
var StupidButton = document.querySelector('input[type="submit"][value="Log in"]'); 
ClicktheButton(StupidButton); 

點擊功能起作用,但名稱和密碼都是空白的,所以登錄不被接受。

有人可以做一些解釋和幫助嗎?謝謝。

回答

0

瀏覽器需要一點時間填寫用戶名和密碼。
頁面首次加載並且Greasemonkey腳本觸發時,它們在該瞬間仍然是空白的。

所以你需要等待這些字段被填充。這樣的事情:

var numIntervals = 0;  
var pwFilledTimer = setInterval (function() { 
     var usrNameInp = document.getElementById ("__ac_name"); 
     if (usrNameInp && usrNameInp.value != "") { 

      var passWrdInp = document.getElementById ("__ac_password"); 
      if (passWrdInp && passWrdInp.value != "") { 

       clearInterval (pwFilledTimer); 

       var submitButton = document.querySelector (
        'input[type="submit"][value="Log in"]' 
       ); 
       var clickEvent = document.createEvent ('MouseEvents'); 
       clickEvent.initEvent ('click', true, true); 
       submitButton.dispatchEvent (clickEvent); 
      } 
     } 
     numIntervals++; 
     if (numIntervals > 10) { 
      /*--- Stop the timer after about 2 seconds so it doesn't 
       interfere with manual logins. 
      */ 
      clearInterval (pwFilledTimer); 
     } 
    }, 
    200 
); 
+0

它的工作,再次感謝,:D。我也隨着時間的推移嘗試了代碼。 ()函數(){setTimeout(submitFirstPasswordForm,200);},false);}} window.addEventListener(「load」,function 函數submitFirstPasswordForm(){ document.forms [1] .submit(); }'** 但不起作用。 – user1663601

+0

不客氣。樂意效勞。 :) –

相關問題