// --- 开始执行防切屏 ---
log("Anti screen-switch script starting...");
patchAddEventListener(window, "window");
patchRemoveEventListener(window, "window");
patchAddEventListener(document, "document");
patchRemoveEventListener(document, "document");
spoofProperties(document, spoofedDocumentProperties, "document");
nullifyEventHandlers(document, eventHandlersToNullifyDocument, "document");
nullifyEventHandlers(window, eventHandlersToNullifyWindow, "window");
const observer = new MutationObserver((mutations, obs) => {
if (document.body) {
patchAddEventListener(document.body, "document.body");
patchRemoveEventListener(document.body, "document.body");
nullifyEventHandlers(document.body, ["onmouseleave", "onmouseout", "onblur", "onfocus"], "document.body");
obs.disconnect();
}
});
if (document.body) {
patchAddEventListener(document.body, "document.body");
patchRemoveEventListener(document.body, "document.body");
nullifyEventHandlers(document.body, ["onmouseleave", "onmouseout", "onblur", "onfocus"], "document.body");
} else {
observer.observe(document.documentElement || document, { childList: true, subtree: true });
}
log("Anti screen-switch script loaded. Monitoring active.");
// =========================================================================
// Part 2: 华为云考试题目复制器模块
// Code from "华为云考试题目复制器" script.
// It waits for the page to load the necessary elements and then adds the button.
// =========================================================================
/**
* 创建并添加“复制题目”按钮到页面上
*/
function addCopyButton() {
const headerRight = document.querySelector('.header .right');
if (headerRight && !document.getElementById('copy-questions-btn')) {
const button = document.createElement('button');
button.innerHTML = '📋 复制题目 (JSON)';
button.id = 'copy-questions-btn';
button.className = 'red'; // 使用一个现有 class 以匹配基本风格
button.style.backgroundColor = '#007d40';
button.style.borderColor = '#007d40';
button.style.marginRight = '10px';
button.style.color = 'white';
button.style.padding = '0 15px'; // 调整内边距以匹配其他按钮
button.style.lineHeight = '30px'; // 调整行高
button.style.height = '32px'; // 调整高度
button.addEventListener('click', extractAndCopyQuestions);
headerRight.insertBefore(button, headerRight.firstChild);
console.log('[题目复制器]: 按钮已添加。');
}
}
/**
* 提取页面所有题目信息并复制到剪贴板
*/
function extractAndCopyQuestions() {
console.log("[题目复制器]: 开始提取题目...");
const allQuestions = [];
const questionElements = document.querySelectorAll('.exam .exam-content');
if (questionElements.length === 0) {
alert('未找到任何题目。请确保考试页面已完全加载。');
return;
}
questionElements.forEach((el, index) => {
const questionObj = {
number: index + 1,
type: 'Unknown',
question: '',
points: 0,
options: []
};
const questionTextEl = el.querySelector('.the-exam-page-html');
if (!questionTextEl) return;
let fullText = questionTextEl.textContent.trim();
const pointMatch = fullText.match(/\((\d+)分\)|((\d+)分)/);
if (pointMatch) {
questionObj.points = parseInt(pointMatch[1] || pointMatch[2], 10);
fullText = fullText.replace(pointMatch[0], '').trim();
}
questionObj.question = fullText;
const radioGroup = el.querySelector('.el-radio-group');
const checkboxGroup = el.querySelector('.el-checkbox-group');
if (radioGroup) {
const options = Array.from(radioGroup.querySelectorAll('.el-radio__label > div')).map(div => div.textContent.trim());
questionObj.options = options;
if (options.length === 2 && (options.includes('正确') && options.includes('错误') || options.includes('true') && options.includes('false'))) {
questionObj.type = 'True/False';
} else {
questionObj.type = 'Single Choice';
}
} else if (checkboxGroup) {
const options = Array.from(checkboxGroup.querySelectorAll('.el-checkbox__label > div')).map(div => div.textContent.trim());
questionObj.options = options;
questionObj.type = 'Multiple Choice';
}
allQuestions.push(questionObj);
});
if (allQuestions.length > 0) {
const jsonOutput = JSON.stringify(allQuestions, null, 2);
GM_setClipboard(jsonOutput, 'text');
const btn = document.getElementById('copy-questions-btn');
if (btn) {
btn.textContent = `✅ 已复制 ${allQuestions.length} 题`;
setTimeout(() => {
btn.textContent = '📋 复制题目 (JSON)';
}, 3000);
}
} else {
alert('提取题目失败,未找到有效题目内容。');
}
}
// 由于脚本在 document-start 运行,DOM 此时尚未构建完成。
// 我们必须等待 DOM 加载完毕后才能查找按钮容器。
// 使用定时器轮询是可靠的方法。
const checkInterval = setInterval(() => {
const headerRight = document.querySelector('.header .right');
if (headerRight) {
clearInterval(checkInterval);
addCopyButton();
}
}, 500);
})();