learningBOX Login Redirect

Directly log in to learningBOX, bypassing the Supported devices page, in case your device is not supported.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         learningBOX Login Redirect
// @namespace    http://tampermonkey.net/
// @license      Unlicense
// @version      1.3
// @description  Directly log in to learningBOX, bypassing the Supported devices page, in case your device is not supported.
// @match        https://lms.learningbox.online/?action=login
// @match        https://lms.learningbox.online/index.php?action=login
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // Define constants for URLs and selectors
    const LOGIN_URL = 'https://lms.learningbox.online/?action=login';
    const DASHBOARD_URL = 'https://lms.learningbox.online/user/dashboard';
    const SELECTORS = {
        loginForm: 'form',
        username: '#user-name',
        password: '#password',
        bid: 'input[name="bid"]'
    };

    // Function to get form values
    const getFormValues = () => ({
        username: document.querySelector(SELECTORS.username).value,
        password: document.querySelector(SELECTORS.password).value,
        bid: document.querySelector(SELECTORS.bid).value
    });

    // Function to prepare form data
    const prepareFormData = ({ username, password, bid }) => {
        const formData = new URLSearchParams();
        formData.append('bid', bid);
        formData.append('userName', username);
        formData.append('password', password);
        return formData.toString();
    };

    // Function to handle login response
    const handleLoginResponse = (response) => {
        console.log('Login request completed');
        if (response.finalUrl.includes('loginCheckInfo') || response.responseText.includes('loginCheckInfo')) {
            console.log('Login successful, redirecting to dashboard');
            window.location.href = DASHBOARD_URL;
        } else {
            console.log('Login failed, staying on login page');
            alert('Login failed. Please check your credentials and try again.');
        }
    };

    // Function to send login request
    const sendLoginRequest = (formData) => {
        GM_xmlhttpRequest({
            method: 'POST',
            url: LOGIN_URL,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Referer': LOGIN_URL,
                'Origin': 'https://lms.learningbox.online',
                'Cookie': document.cookie
            },
            data: formData,
            onload: handleLoginResponse,
            onerror: (error) => {
                console.log('Error during login request:', error);
                alert('An error occurred during login. Please try again.');
            }
        });
    };

    // Main function to intercept the login form submission
    const interceptLogin = () => {
        const loginForm = document.querySelector(SELECTORS.loginForm);
        if (!loginForm) {
            console.log('Login form not found');
            return;
        }

        loginForm.addEventListener('submit', (e) => {
            e.preventDefault();
            console.log('Login form submission intercepted');
            const formValues = getFormValues();
            const formData = prepareFormData(formValues);
            sendLoginRequest(formData);
        });
    };

    // Call the main function
    interceptLogin();
})();