admin管理员组文章数量:1336414
I was trying to make script which will auto-login me to website (/), but I can't get it to insert my username and password inside form. I tried this code:
javascript:document.forms['cssforms'].elements[username].value = MyUsername;
but nothing happens actually...
Also I tried this code:
javascript:document.getElementById('username').value = 'MyUsername';
but then I just get new blank page with "MyUsername" text at left upper corner...
This is part of code when I inspect that element in my Chrome:
<form method="post" action="" class="cssform">
<div class="control-group">
<label for="username">Username</label>
<input style="width:355px;" type="text" name="username" id="username" value="">
Any ideas?
Thanks in advance!
I was trying to make script which will auto-login me to website (http://gamers.ba/), but I can't get it to insert my username and password inside form. I tried this code:
javascript:document.forms['cssforms'].elements[username].value = MyUsername;
but nothing happens actually...
Also I tried this code:
javascript:document.getElementById('username').value = 'MyUsername';
but then I just get new blank page with "MyUsername" text at left upper corner...
This is part of code when I inspect that element in my Chrome:
<form method="post" action="" class="cssform">
<div class="control-group">
<label for="username">Username</label>
<input style="width:355px;" type="text" name="username" id="username" value="">
Any ideas?
Thanks in advance!
Share asked Nov 11, 2014 at 18:58 Amar KalabićAmar Kalabić 8984 gold badges17 silver badges34 bronze badges 2-
2
The form would need an
id
or aname
to be referred to like that. – Bergi Commented Nov 11, 2014 at 19:00 - HOW are you calling it? A bookmarklet? – epascarello Commented Nov 11, 2014 at 19:01
4 Answers
Reset to default 2this worked for me on http://gamers.ba/ as a bookmarklet
javascript:(function(){document.getElementById('username').value='MyUsername';document.getElementById('password').value='MyPassword';document.forms.loginForma.submit.click();})();
Expanded:
javascript:(function(){
document.getElementById('username').value='MyUsername';
document.getElementById('password').value='MyPassword';
document.forms.loginForma.submit.click();
})();
Obviously replace 'MyUsername' and 'MyPassword' with your username and password
This one fails:
document.forms['cssforms'].elements[username].value = MyUsername;
Because
- The form does not have the name "cssforms". A class does not make it a form
- And
[username]
is an undefined variable.
And your second one fails because you are not cancelling the default action. I am assuming you are using a bookmarklet. You can either use void() or wrap it in a function
javascript:(function(){ document.getElementById('username').value = 'MyUsername'; })();
This should do it:
var MyUsername = 'markzuck';
document.getElementById('username').value = MyUsername;
Check the demo here.
Also, I would strongly remend you start using jQuery. It makes the coding simpler and faster.
The form would need an id
or a name
to be referred to like that. And username
is a variable in your script, you need to use a string literal or dot notation instead.
To select the form by its class name, use document.querySelector
:
document.querySelector("form.cssform").elements.username.value = Myusername;
If you want to select the hidden form (<form class="formLoginTop" name="loginForma" action="/q/user/login" method="post">
), then you can still use
document.forms.loginForma.elements.username.value = Myusername;
But you could simply access the input element by its id:
document.getElementById("username").value = Myusername;
(this will not work on http://gamers.ba/q/user/login, because there are two elements with the id username
in that page)
本文标签: htmlInserting username and password at website using javascript in ChromeStack Overflow
版权声明:本文标题:html - Inserting username and password at website using javascript in Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742403283a2468312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论