// ==UserScript==
// @name 中国机械工业出版社自动登录签到(终极版)
// @namespace http://tampermonkey.net/
// @version 3.0
// @description 全自动登录中国机械工艺出版社并完成签到
// @author Cumt-feng
// @match http://www.cmpedu.com/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
// 添加样式
GM_addStyle(`
#cmpedu-config-dialog {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
z-index: 9999;
width: 300px;
}
#cmpedu-config-dialog h3 {
margin-top: 0;
color: #333;
}
#cmpedu-config-dialog label {
display: block;
margin: 10px 0 5px;
}
#cmpedu-config-dialog input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
#cmpedu-config-dialog button {
margin-top: 15px;
padding: 8px 15px;
background: #4CAF50;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
#cmpedu-config-dialog button:hover {
background: #45a049;
}
#cmpedu-config-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.5);
z-index: 9998;
}
.cmpedu-status {
position: fixed;
bottom: 10px;
right: 10px;
background: #f8f8f8;
padding: 10px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0,0,0,0.3);
z-index: 9997;
font-size: 12px;
max-width: 300px;
}
`);
// 配置存储键名
const CONFIG_KEYS = {
USERNAME: 'cmpedu_username',
PASSWORD: 'cmpedu_password',
AUTO_SIGNIN: 'cmpedu_auto_signin',
LAST_SIGNIN: 'cmpedu_last_signin'
};
// 显示状态信息
function showStatus(message, persist = false) {
let statusEl = document.getElementById('cmpedu-status');
if (!statusEl) {
statusEl = document.createElement('div');
statusEl.id = 'cmpedu-status';
statusEl.className = 'cmpedu-status';
document.body.appendChild(statusEl);
}
statusEl.textContent = message;
console.log('中国教育出版网脚本:', message);
if (!persist) {
setTimeout(() => {
if (statusEl.textContent === message) {
statusEl.textContent = '';
}
}, 5000);
}
}
// 注册配置菜单
GM_registerMenuCommand('配置中国教育出版网账号', showConfigDialog);
// 显示配置对话框
function showConfigDialog() {
// 创建遮罩层
const overlay = document.createElement('div');
overlay.id = 'cmpedu-config-overlay';
// 创建配置对话框
const dialog = document.createElement('div');
dialog.id = 'cmpedu-config-dialog';
dialog.innerHTML = `
中国教育出版网账号配置
`;
// 添加保存按钮事件
dialog.querySelector('#cmpedu-save-config').addEventListener('click', saveConfig);
// 添加点击遮罩层关闭对话框
overlay.addEventListener('click', () => {
document.body.removeChild(overlay);
document.body.removeChild(dialog);
});
// 添加到页面
document.body.appendChild(overlay);
document.body.appendChild(dialog);
}
// 保存配置
function saveConfig() {
const username = document.getElementById('cmpedu-username').value;
const password = document.getElementById('cmpedu-password').value;
const autoSignin = document.getElementById('cmpedu-auto-signin').checked;
GM_setValue(CONFIG_KEYS.USERNAME, username);
GM_setValue(CONFIG_KEYS.PASSWORD, password);
GM_setValue(CONFIG_KEYS.AUTO_SIGNIN, autoSignin);
showStatus('账号配置已保存!');
// 关闭对话框
document.body.removeChild(document.getElementById('cmpedu-config-overlay'));
document.body.removeChild(document.getElementById('cmpedu-config-dialog'));
}
// 检查登录状态
function checkLoginStatus() {
// 检查个人主页或个人中心链接
const personalLink = document.querySelector('a[href="/blog/3067876/taokejian.htm"], a[href*="/user/index.htm"]');
if (personalLink) {
showStatus('检测到已登录状态');
return true;
}
return false;
}
// 检查是否需要签到
function checkNeedSignin() {
const lastSigninDate = GM_getValue(CONFIG_KEYS.LAST_SIGNIN, '');
const today = new Date().toDateString();
if (lastSigninDate === today) {
showStatus('今天已经签到过了');
return false;
}
return GM_getValue(CONFIG_KEYS.AUTO_SIGNIN, true);
}
// 执行签到操作
function performSignin() {
if (!checkNeedSignin()) {
return;
}
showStatus('正在尝试签到...', true);
// 查找签到按钮
const signinButton = document.querySelector('a.qiandao[href="javascript:void(0);"]');
if (signinButton) {
try {
// 模拟点击签到按钮
signinButton.click();
// 记录签到日期
GM_setValue(CONFIG_KEYS.LAST_SIGNIN, new Date().toDateString());
// 检查签到结果
setTimeout(() => {
// 这里可以根据实际签到成功的提示来改进检测逻辑
showStatus('签到成功!', true);
}, 2000);
} catch (e) {
showStatus('签到出错: ' + e.message, true);
}
} else {
showStatus('未找到签到按钮,可能已经签到过了', true);
}
}
// 跳转到个人中心并签到
function gotoPersonalCenterAndSignin() {
if (window.location.pathname.includes('/user/index.htm')) {
performSignin();
} else {
showStatus('正在跳转到个人中心...');
window.location.href = '/user/index.htm';
}
}
// 自动填充表单并登录函数
function autoFillAndLogin() {
const username = GM_getValue(CONFIG_KEYS.USERNAME, '');
const password = GM_getValue(CONFIG_KEYS.PASSWORD, '');
if (!username || !password) {
showStatus('未配置账号信息,请点击油猴菜单配置账号', true);
showConfigDialog();
return;
}
const usernameField = document.getElementById("j_username");
const passwordField = document.getElementById("j_password");
const loginButton = document.getElementById("login_save");
if (usernameField && passwordField && loginButton) {
// 填充账号密码
usernameField.value = username;
passwordField.value = password;
// 触发change事件
usernameField.dispatchEvent(new Event('change', { bubbles: true }));
passwordField.dispatchEvent(new Event('change', { bubbles: true }));
showStatus("自动填充账号密码完成");
// 点击登录按钮
showStatus("正在登录...", true);
loginButton.click();
// 登录后检查状态
setTimeout(() => {
if (checkLoginStatus()) {
gotoPersonalCenterAndSignin();
}
}, 3000);
} else {
showStatus("未找到登录表单元素或登录按钮,等待中...");
setTimeout(autoFillAndLogin, 1000);
}
}
// 主执行函数
function main() {
showStatus('脚本启动,正在检查状态...');
if (checkLoginStatus()) {
// 已登录,直接尝试签到
gotoPersonalCenterAndSignin();
} else {
// 未登录,尝试自动登录
autoFillAndLogin();
}
}
// 页面加载完成后执行
if (document.readyState === "complete" || document.readyState === "interactive") {
setTimeout(main, 1000);
} else {
window.addEventListener("DOMContentLoaded", main);
}
// 监听URL变化(应对单页应用)
let lastUrl = location.href;
setInterval(() => {
if (location.href !== lastUrl) {
lastUrl = location.href;
if (location.pathname.includes('/user/index.htm')) {
performSignin();
}
}
}, 1000);
})();