Amar Kalabić
Amar Kalabić

Reputation: 898

Inserting username and password at website using javascript in Chrome

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!

Upvotes: 3

Views: 15906

Answers (4)

Bergi
Bergi

Reputation: 664936

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)

Upvotes: 0

Tony Brix
Tony Brix

Reputation: 4251

this 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

Upvotes: 2

Andres SK
Andres SK

Reputation: 10974

This should do it:

var MyUsername = 'markzuck';
document.getElementById('username').value = MyUsername;

Check the demo here.

Also, I would strongly recommend you start using jQuery. It makes the coding simpler and faster.

Upvotes: 0

epascarello
epascarello

Reputation: 207527

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'; })();

Upvotes: 0

Related Questions