2

Greasemonkeyスクリプトを使用してページ内のフォームを送信したい。フォーム内の名前とpwはFirefoxによって記憶されるため、ページが読み込まれると、名前とpwはすでに入力されています。したがって、自動ログインに送信するだけです。

フォームコード:

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

クリック機能は動作しますが、名前とpwが空白であるため、ログインは受け付けられません。

誰かが説明して助けてもらえますか?ありがとう。

4

1 に答える 1

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
);
于 2012-09-21T14:25:31.303 に答える