// ==UserScript== // @name SuperLogin (Sandbox) // @namespace local.sxz.sandbox // @version 1.0.0 // @description 通过Super API自动化登录账号 // @match http://sxz.school.zykj.org/* // @grant GM_xmlhttpRequest // @connect sxz.api.zykj.org // @run-at document-idle // ==/UserScript== (function () { 'use strict'; const BUTTON_ATTRIBUTE = 'data-superlogin-button'; const API_ENDPOINT = 'http://sxz.api.zykj.org/api/TokenAuth/GetRandomPwdAsync'; const API_PASSWORD = 'Zykj@2022'; function isVisible(element) { if (!(element instanceof HTMLElement)) return false; const style = window.getComputedStyle(element); return style.display !== 'none' && style.visibility !== 'hidden' && element.getClientRects().length > 0; } function inputHint(input) { return [ input.getAttribute('placeholder'), input.getAttribute('aria-label'), input.getAttribute('name'), input.getAttribute('autocomplete'), ].filter(Boolean).join(' '); } function findControls() { const inputs = Array.from(document.querySelectorAll('input')).filter(isVisible); const password = inputs.find((input) => input.type === 'password') || inputs.find((input) => /密码|password/i.test(inputHint(input))); const username = inputs.find((input) => input !== password && /用户名|账号|username|user name|account/i.test(inputHint(input))) || inputs.find((input) => input !== password && ['text', 'email', ''].includes(input.type)); const loginButton = Array.from(document.querySelectorAll('button, input[type="submit"]')) .filter(isVisible) .find((button) => /^(立即登录|登录|登\s*录|log in|sign in)$/i.test((button.innerText || button.value || '').trim())); return { username, password, loginButton }; } function showMessage(button, message, isError) { const parent = button.parentElement; if (!parent) return; let status = parent.querySelector('[data-superlogin-status]'); if (!status) { status = document.createElement('div'); status.setAttribute('data-superlogin-status', ''); status.setAttribute('role', 'status'); status.style.cssText = 'margin-top:6px;font-size:12px;line-height:1.5;'; button.insertAdjacentElement('afterend', status); } status.textContent = message; status.style.color = isError ? '#cf1322' : '#595959'; } function setInputValue(input, value) { const prototype = input instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype; const setter = Object.getOwnPropertyDescriptor(prototype, 'value').set; setter.call(input, value); input.dispatchEvent(new Event('input', { bubbles: true })); input.dispatchEvent(new Event('change', { bubbles: true })); } function addSuperLoginButton() { const { username, password, loginButton } = findControls(); if (!username || !password || !loginButton || document.querySelector(`[${BUTTON_ATTRIBUTE}]`)) return; const button = document.createElement('button'); button.type = 'button'; button.textContent = 'SuperLogin'; button.setAttribute(BUTTON_ATTRIBUTE, ''); button.className = loginButton.className; button.style.display = 'block'; button.style.width = '100%'; button.style.marginTop = '8px'; button.addEventListener('click', () => { const current = findControls(); const userName = current.username && current.username.value.trim(); if (!userName) { showMessage(button, '请先输入用户名。', true); current.username && current.username.focus(); return; } button.disabled = true; showMessage(button, '正在请求SuperAPI接口…', false); const query = new URLSearchParams({ userName, password: API_PASSWORD }); GM_xmlhttpRequest({ method: 'GET', url: `${API_ENDPOINT}?${query.toString()}`, headers: { Accept: 'application/json' }, onload(response) { button.disabled = false; let payload; try { payload = JSON.parse(response.responseText); } catch (_) { showMessage(button, `接口返回的不是有效 JSON(HTTP ${response.status})。`, true); return; } if (response.status < 200 || response.status >= 300 || !payload || typeof payload !== 'object' || payload.success !== true || typeof payload.result !== 'string' || !payload.result) { const error = payload && payload.error; const detail = error && (error.message || error.details || error); showMessage(button, detail ? `获取失败:${String(detail)}` : `获取失败(HTTP ${response.status})。`, true); return; } const refreshed = findControls(); if (!refreshed.password || !refreshed.loginButton) { showMessage(button, '未找到当前登录表单,请确认仍处于账号密码登录页。', true); return; } setInputValue(refreshed.password, payload.result); showMessage(button, '已填入密码,正在提交登录…', false); requestAnimationFrame(() => { const latest = findControls(); if (latest.loginButton && !latest.loginButton.disabled) latest.loginButton.click(); else showMessage(button, '密码已填入,但登录按钮不可用。', true); }); }, onerror() { button.disabled = false; showMessage(button, '无法连接 sxz.api.zykj.org,请检查本地接口是否已启动。', true); }, ontimeout() { button.disabled = false; showMessage(button, '请求超时,请检查 sxz.api.zykj.org 接口。', true); }, timeout: 15000, }); }); loginButton.insertAdjacentElement('afterend', button); } const observer = new MutationObserver(addSuperLoginButton); observer.observe(document.documentElement, { childList: true, subtree: true }); addSuperLoginButton(); })();