learningBOX Login Redirect

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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();
})();