Skip to content

Instantly share code, notes, and snippets.

@ProProgrammer
Last active April 23, 2024 07:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProProgrammer/8534489 to your computer and use it in GitHub Desktop.
Save ProProgrammer/8534489 to your computer and use it in GitHub Desktop.
A Quick JavaScript written to automate login on a page for testing / fun purpose.
/*
The actual javascript code starts from document.forms[0].username.value.... however to run it in the URL bar (address bar) of a browser you have to put the entire Javascript Code in this format javascript:{<YOUR JAVASCRIPT CODE>}
I have tested it in Chrome, Firefox, Internet Explorer and Safari and it works perfectly fine. If you notice any different behaviour with any other browsers, please test and contribute.
Simply replace <YOUR USER NAME> with your actual username and <YOUR PASSWORD> with your actual login password and run this in browser address bar (no need to input actual URL in the JavaScript code)
------------------------------------------------------------------------------------------------------------
CAUTION: PLEASE ENSURE YOU KEEP YOUR PASSWORD SECURE AS YOU ACTUALLY GIVE YOUR PASSWORD IN TEXT FORMAT INSIDE THIS JAVASCRIPT CODE, SO IF ANYONE GETS ACCESS TO THIS CODE THEY WILL GET ACCESS TO YOUR PASSWORD
------------------------------------------------------------------------------------------------------------
EXPLAINING THE CODE:
documents.forms -- returns an array of all form elements on the page
documents.forms[0] - means first form element in the array of forms
username in documents.forms[0].username.value basically looks at the id of the text input area which is generally kept as 'username' for username area and 'password' for password area
password in documents.forms[0].password.value -- Same as above
.value sets the value of that field as you desire
*/
javascript:{document.forms[0].username.value = '<YOUR USER NAME>'; document.forms[0].password.value = '<YOUR PASSWORD>'; document.forms[0].submit();}
@aemxn
Copy link

aemxn commented Sep 8, 2020

I hope I'm not too late, but thanks for sharing this script.

I'm using this for web scraping purpose. However I'm stuck at thinking how to use it in a javascript code aka automate the login task with nodejs and then capture the header of the response after login; because the website doesn't provide any APIs to do auth.

I understand that the script above can only be executed in browser's URL bar or a bookmark, but do you have any idea on how to do it in code? Just asking if you could share some input I could research on later.

@ProProgrammer
Copy link
Author

@aemxn - I haven't been very active in JS per se, however to automate login in nodejs (assuming headless browser) you could use a library like selenium which allows you to obtain the DOM structure as an object (a data structured in a particular format) that JS would understand.
Using that you could further your login attempt by interacting with those elements.

In case of Selenium, if you are using JS as the driving language, you would essentially manipulate your DOM just as I am manipulating it, using the forms component of the DOM.

Another tool you could experiment with is Puppeteer, however I haven't worked extensively with that in production level code hence could not add a lot of color to its usability, scalability and so on.

@aemxn
Copy link

aemxn commented Sep 12, 2020

Thanks for your suggestions @ProProgrammer! I'll try it out next time I'm on my computer.

@krjc
Copy link

krjc commented Apr 23, 2024

I'm looking for a way to pay my members tiny amounts into their member accounts, when they login to my site. I hope you can help me either with a script of some sort or, if java script won't do this job, what language will do it?
Thanks for listening. Looking forward to your answer, either way.
Best
Ken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment