NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name AutoLogin
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Auto login after session expired
// @license MIT
// @author Yuhui
// @match https://auth.nfjd.gmcc.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var DefaultTimeout = 5;// by seconds
var autologin_in_action = false;
var page_type = getPageType();
if (page_type == 'welcome') {
var form = document.querySelector('form[name="frmLogin"]');
var username = document.querySelector('input[name="username"]');
var password = document.querySelector('input[name="password"]');
var ptr = password.parentNode.parentNode;
var tbody = ptr.parentNode;
showAutoLogin();
trigger(isAutoLoginEnabled());
function login() {
if (!!username.value && !!password.value) {
console.log('tring to auto login at ' + new Date().toTimeString());
form.submit();
} else {
alert('用户名或密码为空,自动登录脚本不保存用户名密码,请开启浏览器自带的自动补全功能');
}
}
function trigger(enabled) {
autologin_in_action = enabled;
if (enabled) {
startCount(DefaultTimeout);
}
}
function startCount(timeout) {
var label = document.getElementById('notify_msg');
var msg = timeout + ' 秒后自动登录';
if (!autologin_in_action) {
label.innerHTML = '';
} else {
label.innerHTML = msg;
if (timeout <= 0) {
autologin_in_action = false;
login();
label.innerHTML = '';
} else {
setTimeout(startCount.bind(null,timeout - 1),1000);
}
}
}
function showAutoLogin() {
var tr = document.createElement("tr");
tr.insertCell(-1).innerHTML = "自动登录";
tr.insertCell(-1).innerHTML = " ";
tr.insertCell(-1).appendChild(createCheckbox(trigger));
tr.insertCell(-1).innerHTML = "<label id=notify_msg></label>"
tbody.insertBefore(tr,ptr.nextSibling);
}
} else if (page_type == 'infranet') {
var clock = document.getElementById('liveclock2');
var tr = clock.parentNode.parentNode;
tr.insertCell(0).appendChild(createCheckbox());
tr.insertCell(0).innerHTML = "自动登录";
}
function getPageType() {
var href = location.href;
if (href.indexOf('welcome.cgi') != -1) {
if (href.indexOf('session-limit') == -1) {
return 'welcome';
}
} else if (href.indexOf('infranet.cgi') != -1) {
return 'infranet';
}
return 'ignore';
}
function isAutoLoginEnabled() {
if (localStorage.getItem("autologin") == 'true') {
return true;
}
return false;
}
function createCheckbox(handler) {
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.id = "autologin";
if (isAutoLoginEnabled()) {
checkbox.checked = true;
}
checkbox.addEventListener('change', function() {
localStorage.setItem('autologin',this.checked);
if(typeof handler == 'function') {
handler(this.checked);
}
});
return checkbox;
}
})();