Skip to content

Instantly share code, notes, and snippets.

@venturqx
Last active October 14, 2022 13:24
Show Gist options
  • Save venturqx/6208fdb6f3943b89df334c2f3ffc4689 to your computer and use it in GitHub Desktop.
Save venturqx/6208fdb6f3943b89df334c2f3ffc4689 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name UNILIM-moodle-auto-login
// @namespace http://tampermonkey.net/
// @version 3.1415
// @description An automatic login script for Community.
// @author .
// @match https://community-ensil.unilim.fr/*
// @exclude https://community-ensil.unilim.fr/pluginfile.php/*
// @match https://cas.unilim.fr/*
// @match https://jazz.ensil.unilim.fr/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require http://crypto.stanford.edu/sjcl/sjcl.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// ==/UserScript==
var encKey = GM_getValue ("encKey", "");
var usr = GM_getValue ("lognUsr", "");
var pword = GM_getValue ("lognPwd", "");
encKey=null
if ( ! encKey) {
encKey = prompt (
'Script key not set for ' + location.hostname + '. Please enter a random string:',
''
);
GM_setValue ("encKey", encKey);
usr = pword = ""; // New key makes prev stored values (if any) unable to decode.
}
alert("zeb")
usr = decodeOrPrompt (usr, "U-name", "lognUsr");
pword = decodeOrPrompt (pword, "P-word", "lognPwd");
alert("zebi")
function decodeOrPrompt (targVar, userPrompt, setValVarName) {
if (targVar) {
targVar = unStoreAndDecrypt (targVar);
}
else {
targVar = prompt (
userPrompt + ' not set for ' + location.hostname + '. Please enter it now:',
''
);
GM_setValue (setValVarName, encryptAndStore (targVar) );
}
return targVar;
}
function encryptAndStore (clearText) {
return JSON.stringify (sjcl.encrypt (encKey, clearText) );
}
function unStoreAndDecrypt (jsonObj) {
return sjcl.decrypt (encKey, JSON.parse (jsonObj) );
}
//-- Add menu commands that will allow U and P to be changed.
GM_registerMenuCommand ("Change Username", changeUsername);
GM_registerMenuCommand ("Change Password", changePassword);
function changeUsername () {
promptAndChangeStoredValue (usr, "U-name", "lognUsr");
}
function changePassword () {
promptAndChangeStoredValue (pword, "P-word", "lognPwd");
}
function promptAndChangeStoredValue (targVar, userPrompt, setValVarName) {
targVar = prompt (
'Change ' + userPrompt + ' for ' + location.hostname + ':',
targVar
);
GM_setValue (setValVarName, encryptAndStore (targVar) );
}
function loginCommunity(){
document.querySelector(".form-control").value = loginDetails.userName;
document.querySelector(".form-control.key").value = loginDetails.password;
document.querySelector(".btn-primary").click()
}
function loginJazz(){
document.getElementsByName('login_ensil')[0].value = loginDetails.userName;
document.getElementsByName('passwd_ensil')[0].value = loginDetails.password;
document.querySelectorAll('input[type=submit]')[0].click()
}
function isLoggedInCommunity() {
return document.getElementsByClassName('usertext').length > 0
}
function isLoggedInJazz() {
return (document.documentElement.textContent || document.documentElement.innerText).indexOf('deconnexion') > -1
}
function redirectToLoginCommunity() {
window.location.href = 'https://community-ensil.unilim.fr/login/index.php?authCAS=CAS';
}
function ready(fn) {
if (document.readyState != 'loading'){
fn(); } else { document.addEventListener('DOMContentLoaded', fn); } }
var loginDetails = {
userName: usr,
password: pword,
}
alert("zebii")
var host = window.location.host;
if (host == "community-ensil.unilim.fr") {
if (!isLoggedInCommunity()) {
redirectToLoginCommunity()
}
} else if (host == "cas.unilim.fr") {
ready(loginCommunity())
} else if (host == "jazz.ensil.unilim.fr") {
if (!isLoggedInJazz()) {
ready(loginJazz())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment