Search code examples
javascripthtmluserscripts

Automatically log in to website upon load - user script (JS)


I am writing a userscript for learning purposes that automatically logs me into my school website. I have an attempt in JavaScript that I am hoping is on track. Could someone help me edit it to work as intended (instead of not at all). I have programming experience in Java and Python but I am very new to web programming. Thanks!

I am using TamperMonkey with the following code

// ==UserScript==
// @name       MyMCLogin
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  Automatic login to My MC
// @match https://mymcprod.montgomerycollege.edu/cp/home/displaylogin
// @copyright  SJL 2015
// ==/UserScript==

document.getElementsByName('user')[0].value = "username";
document.getElementsByName('pass')[0].value = "password";
imageSubmit();

Essentially I want to find the variable for username and variable for password, replace them with my username and password, and then submit

Thank you for any assistance :)

Edit: I had a syntax error that I fixed. The only problem remaining is that it doesn't submit the username and password

Edit2: I have tried the following without success:

I noticed after inspecting the submit button that there is a function login() that occurs on press. Can I use this function somehow?

document.cplogin.submit();
document.userid.submit();
document.userid.login();

OK so i got it to work!! I copied the code from the html source for the login() function.

setQueryAsCookie();
document.cplogin.user.value=document.userid.user.value;
if ( document.cplogin.uuid )
{
    document.cplogin.uuid.value=(new Date()).getTime() - clientServerDelta;
}

Which is a very non-eloquent solution :P. I will continue trying variations of the suggestions you guys have made for learning purposes.

Thank you all very much :)

Final? Code:

// ==UserScript==
// @name       MyMCLogin
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  Automatic login to My MC
// @match https://mymcprod.montgomerycollege.edu/cp/home/displaylogin
// @copyright  SJL 2015
// ==/UserScript==

document.getElementsByName('user')[0].value = "username";
document.getElementsByName('pass')[0].value = "password";

setQueryAsCookie();
document.cplogin.user.value=document.userid.user.value;
if ( document.cplogin.uuid )
{
    document.cplogin.uuid.value=(new Date()).getTime() - clientServerDelta;
}

Solution

  • You need to find the form tag so you can call submit() on it. But you don't want to automatically submit all forms, so inspect the form's attributes like ID and action to see if they contain the word "login". This is going to be specific to the page you want to login to, look at the source code and find something unique about that form.

    Then you can find the user and pass exactly like you are doing, but you need to view the source to make sure the name attributes match what you are searching for in the JS.

    I suspect you might end up attempting to submit this on every page and attempting to re-login automatically all the time. I don't know if tampermonkey keeps your code in memory all the time, but if it does, consider setting a variable that keeps track if you've already logged in or not.

    //pseudo code
    var formList = document.getElementsByTag('form');
    var loginForm;
    for (form in formList) {
         if (form.getAttribute('action').indexOf('login')) {
            loginForm = form;
         }
    }
    if (loginForm) {
         //set username and password;
         form.submit();
    }