Krual / Hohai University Campus Network Auto-login

// ==UserScript==
// @name         Hohai University Campus Network Auto-login
// @namespace    https://github.com/Krual-T
// @version      1.9.0
// @description  Automatically clicks the login button with id=loginLink after credentials are filled
// @author       Krual-T
// @license      GPL-3.0-only
// @match        http://eportal.hhu.edu.cn/eportal/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  // 【核心配置区 - 可手动修改】
  const config = {
    loginButtonId: 'loginLink',        // 登录按钮ID(通常无需修改)
    passwordFieldId: 'pwd',            // 密码输入框ID(通常无需修改)
    usernameFieldId: 'username',       // 用户名输入框ID(通常无需修改)
    successPagePath: '/eportal/success.jsp', // 登录成功页路径(通常无需修改)
    successRedirectUrl: 'https://www.google.com', // 登录成功后跳转地址【可修改】
    checkInterval: 10,                // 元素检测间隔(毫秒,通常无需修改)
    maxWaitTime: 15000,                // 最大等待时间(毫秒,15秒,通常无需修改)
  };

  // 当前页面URL
  const currentUrl = window.location.href;

  // 1. 处理成功页 - 跳转至固定地址
  if (currentUrl.includes(config.successPagePath)) {
    handleSuccessPageRedirect();
    return;
  }

  // 2. 处理登录页 - 自动检测并点击登录
  if (currentUrl.includes('/eportal/index.jsp')) {
    waitAndClickLogin();
    return;
  }

  /**
   * 登录成功页跳转处理
   */
  function handleSuccessPageRedirect() {
    // 验证跳转地址有效性
    if (isValidUrl(config.successRedirectUrl)) {
      console.log(`Redirecting to: ${config.successRedirectUrl}`);
      window.location.href = config.successRedirectUrl;
    } else {
      console.log(`Invalid redirect URL, using default blank page`);
      window.location.href = 'about:blank';
    }
  }

  /**
   * 基础URL格式验证
   */
  function isValidUrl(url) {
    if (url.startsWith('about:')) return true;
    try {
      new URL(url);
      return true;
    } catch (e) {
      return false;
    }
  }

  /**
   * 等待登录元素并自动点击登录
   */
  function waitAndClickLogin() {
    let startTime = Date.now();
    const checkInterval = setInterval(() => {
      // 超时处理
      if (Date.now() - startTime > config.maxWaitTime) {
        clearInterval(checkInterval);
        console.log('Timeout waiting for login elements or valid credentials');
        return;
      }

      // 获取登录所需元素
      const usernameField = document.getElementById(config.usernameFieldId);
      const passwordField = document.getElementById(config.passwordFieldId);
      const loginButton = document.getElementById(config.loginButtonId);

      // 验证元素存在且账号密码已填写
      if (usernameField && passwordField && loginButton) {
        const hasValidCredentials = usernameField.value.trim() !== '' && 
                                   passwordField.value.trim() !== '';

        if (hasValidCredentials) {
          clearInterval(checkInterval);
          console.log('Detected valid credentials, clicking login button');

          // 模拟真实点击事件
          const clickEvent = new MouseEvent('click', {
            bubbles: true,
            cancelable: true,
            view: window
          });
          loginButton.dispatchEvent(clickEvent);
          console.log('Triggered login button click');
        } else {
          console.log('Waiting for username and password to be filled...');
        }
      } else {
        console.log('Waiting for login elements to load...');
      }
    }, config.checkInterval);
  }

})();