问题
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