// ==UserScript==
// @name 校园网自动登录(安徽大学版)
// @namespace http://scriptcat.org/
// @version 1.0.2
// @description 可用于校园网快速登录(安徽大学自用版)
// @author yunmi
// @match http://172.16.253.3/a79.htm*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// 使用GM_getValue获取保存的账号密码,如果没有则返回空字符串
var user_account = GM_getValue('user_account', '');
var user_password = GM_getValue('user_password', '');
// 主窗口
var boxOfLogin='#edit_body > div:nth-child(3)';
// 用户名
var inputOfAccount='#edit_body > div:nth-child(3) > div.edit_loginBox.normal_box.random.loginuse.loginuse_pc.ui-resizable-autohide > form > input:nth-child(4)';
// 密码
var inputOfPassword='#edit_body > div:nth-child(3) > div.edit_loginBox.normal_box.random.loginuse.loginuse_pc.ui-resizable-autohide > form > input:nth-child(5)';
// 登录按钮
var buttonOfLogin='#edit_body > div:nth-child(3) > div.edit_loginBox.normal_box.random.loginuse.loginuse_pc.ui-resizable-autohide > form > input:nth-child(2)';
// 返回按钮
var buttonOfBack='#edit_body > div:nth-child(2) > div > form > input';
// 添加样式
GM_addStyle(`
#floating-widget {
position: fixed;
bottom: 20px;
right: 20px;
width: 50px;
height: 50px;
background: #4a6bdf;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 20px;
cursor: pointer;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
z-index: 10000;
transition: transform 0.3s;
}
#floating-widget:hover {
transform: scale(1.1);
}
#widget-panel {
position: fixed;
bottom: 80px;
right: 20px;
width: 300px;
background: white;
border-radius: 12px;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
padding: 20px;
z-index: 10000;
display: none;
}
#widget-panel h3 {
margin-bottom: 15px;
color: #4a6bdf;
text-align: center;
}
.close-btn {
position: absolute;
top: 10px;
right: 15px;
font-size: 20px;
cursor: pointer;
color: #888;
}
.widget-form-group {
margin-bottom: 15px;
}
.widget-form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.widget-form-group input {
width: 100%;
padding: 8px 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
#updateBtn {
background: #4a6bdf;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-weight: bold;
}
#updateBtn:hover {
background: #3a5bc7;
}
#login-status {
position: fixed;
top: 20px;
right: 20px;
padding: 10px 15px;
border-radius: 4px;
background: #f8f9fa;
border: 1px solid #ddd;
z-index: 10000;
font-size: 14px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
#countdown {
font-weight: bold;
color: #4a6bdf;
}
`);
// 创建悬浮按钮
var floatingWidget = document.createElement('div');
floatingWidget.id = 'floating-widget';
floatingWidget.innerHTML = '⚙️';
document.body.appendChild(floatingWidget);
// 创建设置面板
var widgetPanel = document.createElement('div');
widgetPanel.id = 'widget-panel';
widgetPanel.innerHTML = `
×
修改登录信息
`;
document.body.appendChild(widgetPanel);
// 创建登录状态提示
var statusPanel = document.createElement('div');
statusPanel.id = 'login-status';
statusPanel.innerHTML = '等待页面加载...';
document.body.appendChild(statusPanel);
// 显示/隐藏设置面板
floatingWidget.addEventListener('click', function() {
widgetPanel.style.display = 'block';
// 填充当前保存的值
document.getElementById('edit-username').value = user_account;
document.getElementById('edit-password').value = user_password;
});
document.getElementById('close-panel').addEventListener('click', function() {
widgetPanel.style.display = 'none';
});
// 更新按钮点击事件
document.getElementById('updateBtn').addEventListener('click', function() {
var newUsername = document.getElementById('edit-username').value.trim();
var newPassword = document.getElementById('edit-password').value.trim();
if (!newUsername || !newPassword) {
alert('账号和密码不能为空');
return;
}
// 保存新值
GM_setValue('user_account', newUsername);
GM_setValue('user_password', newPassword);
user_account = newUsername;
user_password = newPassword;
alert('登录信息已更新!');
widgetPanel.style.display = 'none';
// 如果已经在登录页面,重新尝试登录
if (document.querySelector(boxOfLogin)) {
statusPanel.innerHTML = '信息已更新,将在3秒后重新尝试登录...';
setTimeout(attemptLogin, 3000);
}
});
// 登录函数
function login() {
console.log("正在进行登录操作");
statusPanel.innerHTML = '正在登录...';
// 自动填写账号密码到输入框
document.querySelector(inputOfAccount).value = user_account;
document.querySelector(inputOfPassword).value = user_password;
window.setTimeout(function() {
document.querySelector(buttonOfLogin).click();
statusPanel.innerHTML = '登录请求已发送';
// 检查登录结果
window.setTimeout(function() {
// 有返回按钮存在表示登录可能有问题
if (document.querySelector(buttonOfBack) && document.querySelector(buttonOfBack).value == "返 回") {
document.querySelector(buttonOfBack).click();
console.log("存在返回按钮,立即返回,并执行登录操作");
statusPanel.innerHTML = '检测到返回按钮,重新尝试登录...';
login();
} else {
console.log("登录成功");
statusPanel.innerHTML = '登录成功!';
// 5秒后隐藏状态提示
setTimeout(function() {
statusPanel.style.display = 'none';
}, 5000);
}
}, 1000);
}, 200);
}
// 尝试登录的函数
function attemptLogin() {
// 判断主窗体是否存在
if (document.querySelector(boxOfLogin)) {
console.log("登录框存在,准备登录");
statusPanel.innerHTML = '检测到登录框,3秒后开始登录...';
// 倒计时显示(修改此处值为0,即可跳过等待时间!!!!!)
var countdown = 3;
var countdownInterval = setInterval(function() {
statusPanel.innerHTML = '检测到登录框,' + countdown + '秒后开始登录...';
countdown--;
if (countdown < 0) {
clearInterval(countdownInterval);
login();
}
}, 1000);
} else {
console.log("登录框不存在");
statusPanel.innerHTML = '未检测到登录框,请检查页面是否正确加载';
// 5秒后隐藏状态提示
setTimeout(function() {
statusPanel.style.display = 'none';
}, 5000);
}
}
// 主逻辑
if (!user_account || !user_password) {
// 如果没有保存的账号密码,显示设置面板
statusPanel.innerHTML = '请先设置登录信息';
widgetPanel.style.display = 'block';
} else {
// 有保存的账号密码,等待3秒后尝试自动登录
statusPanel.innerHTML = '页面加载完成,3秒后开始检测登录框...';
setTimeout(function() {
attemptLogin();
}, 3000);
}
})();