// ==UserScript==
// @name XY助手脚本
// @namespace http://tampermonkey.net/
// @version 2.0
// @description 双击复制网页内容 + XY自动提交表单
// @author You
// @match *://*/*
// @match https://hw.jst2024.cn/web/2024/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// 屏蔽所有弹窗
window.alert = function() { return true; };
window.confirm = function() { return true; };
window.prompt = function() { return ''; };
window.onbeforeunload = null;
window.onunload = null;
// 脚本开关状态(从缓存读取)
let copyEnabled = localStorage.getItem('copyEnabled') !== 'false';
let xyScriptEnabled = localStorage.getItem('xyScriptEnabled') !== 'false';
let isXYSite = window.location.href.includes('hw.jst2024.cn');
// 创建统一控制框
function createControlBox() {
if (document.getElementById('unified-control-box')) {
return;
}
const controlBox = document.createElement('div');
controlBox.id = 'unified-control-box';
controlBox.style.cssText = 'position:fixed;top:10px;right:10px;background:#fff;border:1px solid #ccc;padding:5px;z-index:999999;';
controlBox.innerHTML = `
${copyEnabled ? '复制开' : '复制关'}
${isXYSite ? `
${xyScriptEnabled ? '运行中' : '已停止'}
监控中
` : ''}
`;
document.body.appendChild(controlBox);
// 绑定复制功能开关
const toggleCopyBtn = document.getElementById('toggle-copy-btn');
const copyStatus = document.getElementById('copy-status');
toggleCopyBtn.addEventListener('click', function(e) {
e.stopPropagation();
copyEnabled = !copyEnabled;
localStorage.setItem('copyEnabled', copyEnabled);
toggleCopyBtn.textContent = copyEnabled ? '关闭复制' : '开启复制';
copyStatus.textContent = copyEnabled ? '复制开' : '复制关';
});
// 绑定XY脚本开关
if (isXYSite) {
const toggleXYBtn = document.getElementById('toggle-xy-btn');
const xyStatus = document.getElementById('xy-status');
const currentStatus = document.getElementById('current-status');
toggleXYBtn.addEventListener('click', function() {
xyScriptEnabled = !xyScriptEnabled;
localStorage.setItem('xyScriptEnabled', xyScriptEnabled);
if (xyScriptEnabled) {
toggleXYBtn.textContent = '停止XY';
xyStatus.textContent = '运行中';
currentStatus.textContent = '监控中';
} else {
toggleXYBtn.textContent = '启动XY';
xyStatus.textContent = '已停止';
currentStatus.textContent = '已暂停';
}
});
}
}
// 复制文本到剪贴板
async function copyToClipboard(text) {
try {
// 优先使用现代 Clipboard API
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
return true;
} else {
// 降级方案:使用 execCommand
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
textArea.style.top = '-999999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
const result = document.execCommand('copy');
document.body.removeChild(textArea);
return result;
}
} catch (err) {
console.error('复制失败:', err);
return false;
}
}
// 获取选中的文本
function getSelectedText() {
const selection = window.getSelection();
return selection.toString().trim();
}
// XY脚本:自动填写并提交表单
function autoFillAndSubmit() {
if (!xyScriptEnabled || !isXYSite) return;
const currentStatus = document.getElementById('current-status');
if (currentStatus) currentStatus.textContent = '填写中';
setTimeout(function() {
try {
var contentInput = document.querySelector('input[name="content"]');
if (contentInput) {
if (currentStatus) currentStatus.textContent = '提交中';
contentInput.value = '1';
contentInput.setAttribute('value', '1');
contentInput.dispatchEvent(new Event('input', { bubbles: true }));
contentInput.dispatchEvent(new Event('change', { bubbles: true }));
setTimeout(function() {
var form = document.querySelector('form');
if (form) {
try {
var formData = new FormData(form);
formData.set('content', '1');
fetch(form.action || window.location.href, {
method: 'POST',
body: formData,
keepalive: true
}).catch(function() {});
setTimeout(function() {
try {
var submitBtn = document.querySelector('input[type="submit"]');
if (submitBtn) {
submitBtn.click();
if (currentStatus) currentStatus.textContent = '完成';
}
} catch (e) {}
}, 50);
} catch (e) {}
}
}, 50);
}
} catch (error) {}
}, 200);
}
// 处理双击事件
function handleDoubleClick(event) {
if (!copyEnabled) return;
// 避免在输入框等表单元素中触发
const tagName = event.target.tagName.toLowerCase();
if (['input', 'textarea', 'select'].includes(tagName)) {
return;
}
if (event.target.closest('#unified-control-box')) {
return;
}
// 获取双击位置的文本
setTimeout(() => {
const selectedText = getSelectedText();
if (selectedText.length > 0) {
copyToClipboard(selectedText);
} else {
const range = document.caretRangeFromPoint(event.clientX, event.clientY);
if (range) {
const textNode = range.startContainer;
if (textNode.nodeType === Node.TEXT_NODE) {
const text = textNode.textContent;
const start = range.startOffset;
const end = range.endOffset;
let wordStart = start;
let wordEnd = end;
while (wordStart > 0 && /\w/.test(text[wordStart - 1])) {
wordStart--;
}
while (wordEnd < text.length && /\w/.test(text[wordEnd])) {
wordEnd++;
}
const word = text.substring(wordStart, wordEnd).trim();
if (word.length > 0) {
copyToClipboard(word);
}
}
}
}
}, 10);
}
// XY脚本:等待页面加载
function waitForXYPage() {
if (!document.getElementById('unified-control-box')) {
createControlBox();
}
if (isXYSite) {
const currentStatus = document.getElementById('current-status');
if (window.location.href.includes('private_edit.php')) {
if (currentStatus) currentStatus.textContent = '编辑页';
autoFillAndSubmit();
} else {
if (currentStatus) currentStatus.textContent = '监控中';
}
}
}
// 初始化脚本
function initScript() {
if (!document.getElementById('unified-control-box')) {
createControlBox();
}
// 双击复制功能(所有网站)
document.addEventListener('dblclick', handleDoubleClick);
// XY脚本功能(仅在XY网站)
if (isXYSite) {
waitForXYPage();
// 监听页面变化
let lastUrl = location.href;
new MutationObserver(() => {
if (location.href !== lastUrl && location.href.includes('private_edit.php')) {
lastUrl = location.href;
setTimeout(autoFillAndSubmit, 200);
}
}).observe(document, {subtree: true, childList: true});
}
}
// 页面加载完成后执行
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initScript);
} else {
initScript();
}
})();