Inserting username and password at website using javascript in Chrome - javascript

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!

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

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 recommend 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)

Related

Prevent browser from remembering credentials (Password)

Is it possible after processing any login Form to prevent the browser from offering the option to remember the password?
I know it's been asked and answered here but none of the answers have worked so far, even one of them suggested using:
autocomplete="off"
But that also didn't worked.
I'm using AngularJS and Jade as templating engine (not sure if relevant or not anyhow), is there any way to do this?
if a site sets autocomplete="off" for a form, and the form includes username and password input fields, then the browser will still offer to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits this page.
https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
You should also set autocomplete="off" on your input as well as your form.
Google Chrome release notes:
The Google Chrome UI for auto-complete request varies, depending on whether autocomplete is set to off on input elements as well as their form. Specifically, when a form has autocomplete set to off and its input element's autocomplete field is not set, then if the user asks for autofill suggestions for the input element, Chrome might display a message saying "autocomplete has been disabled for this form." On the other hand, if both the form and the input element have autocomplete set to off, the browser will not display that message. For this reason, you should set autocomplete to off for each input that has custom auto-completion.
I would suggest using Javascript to create a random number. Pass that random number to your Server Action using a hidden field, then incorporate that random number into the names of the "login" and "password" fields.
E.g. (psuedo code, the exact syntax depends on whether you use PHP, jQuery, pure Javascript, etc.)
<script>
var number = Math.random();
var login_name = 'name_'+number;
var pass_word = 'pass_'+number;
</script>
<input name='number' value="number" type='hidden'>
<input name="login_name" type='text'>
<input name="pass_word" type='password'>
Your server reads the "number" field, then uses that to read "name_"number value and "pass_"number value.
It won't matter whether or not the user saves their password in the browser, since every time the user logs in, the name and password fields will be different.
Since you're using AngularJS, you can leave the field unnamed, and access the data it contains through the model :
Login: <input ng-model="login" type="text" />
Password: <input ng-model="password" type="password" autocomplete="off" />
and in your javascript file :
$scope.doLogin = function() {
var dataObj = {
login: $scope.login,
password: $scope.password
};
var res = $http.post('/login', dataObj);
}
Tested in IE10 and Chrome 54
This post is little bit old now, but sincce I found a solution that works for me (at least with Chrome version 56), I'll share it here.
If you remove name and password attributes on your input, then Chrome won't ask to save the password. Then you just have to add the missing attributes by code just before submitting the form:
<!-- Do not set "id" and "name" attributes -->
Login: <input type="text">
Password: <input type="password">
<!-- Handle submit action -->
<input type="submit" onclick="login(this)">
Then in Javascript:
function login(submitButton) {
var form = submitButton.form;
// fill input names by code before submitting
var inputs = $(form).find('input');
$(inputs[0]).attr('name', 'userName');
$(inputs[1]).attr('name', 'password');
form.submit();
}
I hope this will help. Tested on Chrome 56 only.
The problem I have is that while I understand the 'annoyance' to a user in not being able to have their browser remember their password and I don't want to 'disable' that feature completely, there are times when I want to disable it for just a certain password field. Example for me being a 'reset your password' dialogue box.
I want to force them to have to re-enter their old password and then of course type the new one twice.
It's been my experience that no matter what I name that 'old' password input, it is auto-filled with the 'remembered' password (in Firefox 49.0.1 anyway). Maybe this is where I'm getting this wrong, but it just fills it no matter the fact that this input's name is different from saying the login input field.
The behavior I see is basically that the browser seems to say "This user has remembered a password for this site, so now just fill every input type='password' box with that password no matter the name. It seems to me that this should be based on the name attribute, but for me (on multiple sites I've worked on) this just does not seem to be the case.
My solution:
Color this password field to the same color as the background of your input so the 'password dots' is essentially invisible on page load.
onload, onblur, after a timeout, or however you want to do it, use JQuery or JS to set the value of that password field to nothing (blank), then set the color of the field to whatever it is supposed to be.
$("#old_password").val('').css('color','black);
I've discovered that Firefox 52.0.2 is incredibly determined to remember the autocompletion values. I tried almost everything suggested above, including changing the name attributes to random values. The only thing that is working for me is setting the values to "" with Javascript after the browser has had its way with the form.
My use case is lucky in that I do not have to resort to CSS tricks to prevent a confusing and/or annoying flash of autocompleted form values (as proposed by #MinnesotaSlim above) because my form is hidden until they click a button; then it's displayed via a Bootstrap modal. Hence (with jQuery),
$('#my-button').on("click",function(){
$('#form-login input').val("");
});
// this also works nicely
$('#my-modal').on("show.bs.modal",function(){
$('#form-login input').val("");
})
And I imagine you might be able to get the same effect by having your form initially hidden, and in your document load event, manually override the browser and then display the form.
For me, the only solution that reliably worked was to empty username and password input element just before submitting form combined with replacing submit button for the regular button with onclick handler.
NOTE: We use Microsoft MVC so we needed to populate ViewModel with entered credentials. Therefore we created hidden input elements bound to model and copied credential values to them before emptying visible inputs.
<form>
<input id="UserName" name="UserName" type="hidden" value="">
<input id="Password" name="Password" type="hidden" value="">
</form>
<input id="boxUsr" name="boxUsr" type="text" value="" autocomplete="off">
<input id="boxPsw" name="boxPsw" type="password" autocomplete="off">
<input type="submit" value="Login" onclick="javascript:submitformforlogin()">
function submitformforlogin() {
$("[name='boxPsw'],[name='boxUsr']").attr('readonly','true');
var psw = document.getElementsByName('boxPsw')[0];
var usr = document.getElementsByName('boxUsr')[0];
if (psw.value != "false") {
$('#Password').val(psw.value);
$('#UserName').val(usr.value);
psw.value = "";
usr.value = "";
$("form").submit();
} else
window.alert("Error!");
}

Need help to test html form textbox and go to page if value is the same as a set value

Hello I am trying to make a simple form to test if the the textfield is equal to a variable, variable value example: ( "MyPassword123" ).
Then if it the textfield is the same as the variable than go to html document, example: ( "nextPage.html" ).
however if its NOT equal to variable then go to html document, example: ( "index.html" ).
the reason of the password is to restrict people that don't play on my game server form nextPage.html, it will have just like news feeds and game information on it, Its nothing like an profile or anything I just want to give out a password to only allow people that play on the server to view a page that's all.
I have tried many times to get this to work in javascript and I am sure its achievable for this simple task using if/else statements and validate the name of the text field but I am no good at java nor javascript.
Form Code:
<form name="accessForm">
Password: <input type="text" name="inputCode"/>
<input type="submit" value="Submit"/>
</form>
If someone could post some code of javascript to make this work, you would so awesome.
NOTE:
Not sure if it matters much but I am using HTML5 and CSS3, and for
Hosting I will be using GoogleDrive, so I cant use MySQL, it needs to
be javascript. I have not tested Drive to see if it allows PHP but I
know Javascript works fine.
You need not to use HTML form for it
<script>
function checkIt()
{
if(document.getElementById("inputCode").value=="MyPassword123")
location.href="nextPage.html";
else
location.href="index.html";
}
</script>
Password: <input id="inputCode" type="text"/>
<input type="button" value="Submit" onclick="checkIt()"/>
Like #Wes Foster mentioned, you should do password validation on the backend, but to compare a form input to a variable in vanilla JS you could do this:
var password = "magicWord";
var form = document.getElementById("formName");
form.onsubmit = function(e) {
e.preventDefault();
var data = document.forms.accessForm.inputCode.value;
if (data == password) location.href = "success.html";
else location.href = "fail.html";
};

Autocompletion doesn't autocomplete

Here is a page with:
<form action="action.php" method="get" autocomplete="on">
First name:<input type="text" name="fname"><br>
E-mail: <input type="email" name="email"><br>
<input type="submit">
</form>
Let's enter the 2 fields, and submit. Then when reloading the page, the fields are not autocompleted, why?
Should I use local-storage? cookies? to be sure that a user that comes back a few days after will still have its fields autocompleted?
(I'm using Firefox with default options about autocompletion, i.e. not disabled).
Firstly you need to realise that an attribute on the element without any coding on your part is entirely browser implemented.
Browsers implement these things differently.
You should check the spec. There you will notice that, actually,
By default, the autocomplete attribute of form elements is in the on state.
The only solution to the problem you are having is to say that, if you want a more reliable and consistent, user experience; don't rely on browser implementation.
The short answer: Use Javascript
There are hundreds of plugins that will do this for you. I will point you to one that has been popular on github recently but will need you to actually need to tell this plugin what you expect it to do: https://leaverou.github.io/awesomplete/
Updated Answer:
I see that you just want to prefill the field. This can be accomplished with something like this (untested):
var form = document.forms["name_of_your_form"];
form.onsubmit = function(){
localStorage.setItem("fname", form.elements["fname"].value);
localStorage.setItem("email", form.elements["email"].value);
}
window.onload = function() {
form.elements["fname"].value = localStorage.getItem("fname");
form.elements["email"].value = localStorage.getItem("email");
}

Enter username in input inside of label using Mechanize in Python

I am working on a project which requires a login using Mechanize in python.
It is having trouble accessing the login in the form by its name. I am not too familiar with HTML, but it appears to have something to do with the label that is referencing the login entry field.
For those who are not familiar with Mechanize, you can pretty much ignore that. Just know that br.form['xzy'] = '123' sets the field with a name of 'xyz' to '123' in whatever current form is selected. So in short the problem is finding either the correct name for the username entry field or a workaround.
This is what the python mechanize code looks like...
br.select_form(nr=0)
br.form['username'] = 'dave'
br.form['password'] = 'cats123'
session = br.submit()
This is what the input looks like in the HTML of the page I'm working on...
<label for="username" class="fl-label"><span class="accesskey">A</span>ccount:</label>
<input id="username" name="username" class="required" tabindex="1" accesskey="n" type="text" value="" size="32" autocomplete="false"/>
And here is the error I am running into...
File "vcl.py", line 12, in maristLogin
br.form['username'] = 'steh'
mechanize._form.ControlNotFoundError: no control matching name 'username'
are there any other forms on the page or is the entire page on that pastebin?
you could always give the following code a go to make sure you're getting the form you want.
for form in br.forms():
if form.attrs['id'] == 'fm1':
br.form = form

How to retrieve html form elements and display the values elsewhere on the page

I am inexperienced so sorry if this is a dumb question but I'm looking around and I haven't found an answer that works for me. I am using JavaScript and I have an html form with one text input. When someone enters a value for that input and submits the form I want to return to the same HTML page but with what they entered displayed elsewhere on the page. Here is my form:
<form name="chatInput" action="index.html" method="get">
chat:<input type="text" name="chat"/>
</form>
I saw something about having:
var chatText = document.forms[0].elements[0];
But when I try to use this I get [object HTMLInputElement]
Also I would only want to create that variable if the form has been entered once already but not initially. How would I check for this?
Thanks for any info.
var chatText = document.forms[0]['chat'].value;
http://jsfiddle.net/XLeAn/
If you don't want to re-update the html, I'd recommend not using the form tag.
Instead, I'd create a button with the onclick attribute.
<input type="button" value="Update" onclick="someFunction()">
Then in the JS portion, have something like this.
var someFunction = function(){
var chatText = document.getElementById("this-is-the-element-I-want").value;
}
Of course, make sure that the element you are trying to retrieve info from has an id.
<input type="text" name="chat" id="this-is-the-element-I-want">
EDIT: Code indents and clarity
Try this:
var chatText = document.forms[0].elements[0].value;

Categories

Resources