3

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!

Amar Kalabić
  • 888
  • 4
  • 15
  • 33

4 Answers4

2

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

Tony Brix
  • 4,085
  • 7
  • 41
  • 53
0

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)

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Nothing happens when I enter that. Username field remains empty... I tried both ways... – Amar Kalabić Nov 11 '14 at 19:28
  • Are you sure `Myusername` is not the empty string? – Bergi Nov 11 '14 at 19:58
  • Tried adding it as 'Myusername', nothing changed... When I use second way you gave, if I surround Myusername with '', new blank page opens with "Myusername" text in upper left corner... You can try it yourself... – Amar Kalabić Nov 11 '14 at 20:05
  • If you use it as a bookmarklet, the expression after `javascript` needs to return `undefined`. In your case, you could do `javascript: document.getElementById("username").value = "Myusername"; void 0;` – Bergi Nov 11 '14 at 20:16
0

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.

Andres SK
  • 10,779
  • 25
  • 90
  • 152
  • You mean on your website? Or on jsfiddle? – Andres SK Nov 11 '14 at 19:32
  • On website... It works perfectly on jsfiddle... Actually you did same as I did in second way I described, which should work, but apparently it's not working on this website... – Amar Kalabić Nov 11 '14 at 19:36
  • Please check your Chrome console to check for specific errors and paste them here. – Andres SK Nov 11 '14 at 19:44
  • No errors in Chrome console but if I surround Myusername with apostrophes, like 'MyUsername', new blank page opens with "Myusername" text in upper left corner... You can try it yourself... – Amar Kalabić Nov 11 '14 at 20:06
0

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'; })();
epascarello
  • 204,599
  • 20
  • 195
  • 236