Skip to content

Instantly share code, notes, and snippets.

@ineentho
Created October 10, 2019 00:23
Show Gist options
  • Save ineentho/ad37dff604f680b1feaef747f13aa84f to your computer and use it in GitHub Desktop.
Save ineentho/ad37dff604f680b1feaef747f13aa84f to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name T4C Automatic Login
// @namespace http://tampermonkey.net/
// @version 0.1
// @author Henrik Karlsson
// @author You
// @match http://192.168.0.55/*
// @grant none
// ==/UserScript==
'use strict'
;(function() {
const createAutomaticLoginCheckbox = () => {
const loginTBody = document.querySelector('#fields > table > tbody')
const validationResultTr = document.querySelector('#fields > table > tbody > tr:nth-child(5)')
const loginButton = document.querySelector('#btnLogin_5')
const tr = document.createElement('tr')
const spacerTd = document.createElement('td')
spacerTd.innerHTML = ' '
tr.appendChild(spacerTd)
const td = document.createElement('td')
const input = document.createElement('input')
input.type = 'checkbox'
input.id = 'automatic_login'
input.checked = localStorage.getItem('automatic_login') === 'true'
td.appendChild(input)
const label = document.createElement('label')
label.innerText = 'Logga in automatiskt'
label.htmlFor = 'automatic_login'
td.appendChild(label)
tr.appendChild(td)
loginTBody.insertBefore(tr, validationResultTr)
loginButton.addEventListener('click', () => {
localStorage.setItem('automatic_login', input.checked ? 'true' : 'false')
})
}
const hasVisibleErrorDialog = () => document.querySelector('.popup')
const tryAutomaticLogin = () => {
if (localStorage.getItem('automatic_login') === 'true') {
const loginButton = document.querySelector('#btnLogin_5')
loginButton.click()
}
}
const tryAugmentLogoutLink = () => {
const logoutLink = document.querySelector('#NavMenu1_lblLogout_11')
if (!logoutLink) {
return
}
logoutLink.addEventListener('click', () => {
localStorage.removeItem('automatic_login')
})
}
if (window.location.pathname === '/T4C/Content/Login.aspx') {
createAutomaticLoginCheckbox()
if (!hasVisibleErrorDialog()) {
tryAutomaticLogin()
}
}
tryAugmentLogoutLink()
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment