How to set two inputs and ungrey a button?

爱⌒轻易说出口 提交于 2019-12-14 04:05:53

问题


I'm looking for a way to automatically fill in a username/password in a webpage using Greasemonkey.

The two tags are inputs, they have no ID but they're the only two inputs in the page, which appear when a 'log in' button is pressed.

I am a beginner in Greasemonkey (and javascript) and I was wondering how I could wait for these two inputs to appear.
Then there's two buttons, an 'ok' and a 'cancel' button. The 'ok' button is greyed, and setting the inputs with element0.value="password" doesn't ungrey the button.

How can I ungrey it using javascript?


回答1:


The question lacks the requested and required HTML, but in general, you would use techniques from "Choosing and activating the right controls on an AJAX-driven site".

Something like:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

waitForKeyElements ("input[type='password']", logIn);

function logIn (jNode) {
    var inputs  = $("input");

    //-- DO NOT HARDCODE LOGIN CREDENTIALS IN A SCRIPT!
    $(inputs[0]).val ("username");
    $(inputs[1]).val ("password");

    //-- Might need to trigger a change or mouse event here.

    waitForKeyElements (
        "JQUERY SELECTOR FOR THE OK BUTTON IN THE ACTIVE STATE",
        clickOK_Btn
    );
}

function clickOK_Btn (jNode) {
    triggerMouseEvent (jNode[0], "click");
}

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}


But do not hardcode login credentials. Use a sensitive information framework.



来源:https://stackoverflow.com/questions/16029674/how-to-set-two-inputs-and-ungrey-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!