最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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  /
// @version    0.1
// @description  Automatic login to My MC
// @match 
// @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  /
// @version    0.1
// @description  Automatic login to My MC
// @match 
// @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;
}

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;
}
Share Improve this question edited Oct 31, 2015 at 20:54 RenderedNonsense asked Oct 31, 2015 at 20:09 RenderedNonsenseRenderedNonsense 1361 silver badge11 bronze badges 3
  • you have an extra } at the end... – doublesharp Commented Oct 31, 2015 at 20:12
  • LOL thank you i am a moron. It still doesn't submit though. That is the last remaining issue. – RenderedNonsense Commented Oct 31, 2015 at 20:16
  • 1 You probably want document.getelementById('formid').submit(). Calling the onsubmit function directly will not pass some expected Event objects... maybe, but it's still best to call the form's submit() function. Or call the button's click function. document.getElementById('theSubmitButton').click(); – chugadie Commented Oct 31, 2015 at 20:45
Add a ment  | 

1 Answer 1

Reset to default 3

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();
}
发布评论

评论列表(0)

  1. 暂无评论