洛谷私信锁
deep_sleep · · 科技·工程
// ==UserScript==
// @name 洛谷私信防窥锁
// @namespace http://tampermonkey.net/
// @version 1.7
// @description
// @match https://www.luogu.com.cn/chat*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const PASSWORD = "123456"; // 修改密码
let maskDiv = null;
let lockObserver = null;
let lockInterval = null;
// 用于解除绑定的事件处理函数
let keydownHandler = null;
let contextmenuHandler = null;
function clearWatchers() {
if (lockObserver) {
lockObserver.disconnect();
lockObserver = null;
}
if (lockInterval) {
clearInterval(lockInterval);
lockInterval = null;
}
}
// 移除全局事件监听(解锁时调用)
function removeGlobalListeners() {
if (keydownHandler) {
document.removeEventListener('keydown', keydownHandler, true);
keydownHandler = null;
}
if (contextmenuHandler) {
document.removeEventListener('contextmenu', contextmenuHandler, true);
contextmenuHandler = null;
}
}
function forceReload() {
location.reload(true);
}
function buildLockMask() {
if (maskDiv) {
clearWatchers();
maskDiv.remove();
maskDiv = null;
}
maskDiv = document.createElement('div');
maskDiv.style.cssText = `
position: fixed !important;
inset: 0 !important;
background-color: #1a1a1a !important;
z-index: 1 !important;
display: flex !important;
flex-direction: column !important;
align-items: center !important;
justify-content: center !important;
color: #ffffff !important;
gap: 18px !important;
`;
maskDiv.innerHTML = `
<h2>🔒 私信已锁定</h2>
<p>输入密码解锁查看私信</p>
<input id="pwd-input" type="password" placeholder="点击输入密码" style="
padding:10px 12px;
width:260px;
font-size:16px;
border-radius:6px;
border:none;
outline:none;
">
<button id="unlock-btn" style="
padding:9px 26px;
font-size:15px;
cursor:pointer;
border-radius:6px;
border:none;
background:#409eff;
color:white;
">解锁</button>
`;
document.documentElement.appendChild(maskDiv);
const input = document.getElementById("pwd-input");
const unlockBtn = document.getElementById("unlock-btn");
input.addEventListener("click", () => {
setTimeout(() => input.focus(), 10);
});
function checkPasswordAndUnlock() {
if (input.value.trim() === PASSWORD) {
// 正确:移除遮罩和所有监听
removeGlobalListeners();
clearWatchers();
maskDiv.remove();
maskDiv = null;
addLockButton();
} else {
forceReload();
}
}
unlockBtn.onclick = checkPasswordAndUnlock;
input.onkeydown = e => {
if (e.key === "Enter") checkPasswordAndUnlock();
};
setTimeout(() => input.focus(), 100);
// 遮罩背景中键/滚轮无条件刷新
maskDiv.addEventListener('mousedown', (e) => {
if (e.target !== maskDiv) return;
if (e.button === 1) {
e.preventDefault();
forceReload();
}
});
maskDiv.addEventListener('wheel', (e) => {
if (e.target !== maskDiv) return;
e.preventDefault();
forceReload();
});
maskDiv.addEventListener('contextmenu', (e) => {
if (e.target === maskDiv) e.preventDefault();
});
// ========== 全局禁用开发者工具快捷键 & 右键菜单 ==========
keydownHandler = function(e) {
if (!maskDiv) return;
// F12
if (e.key === 'F12' || e.keyCode === 123) {
e.preventDefault();
return false;
}
// Ctrl+Shift+I (检查)
if (e.ctrlKey && e.shiftKey && (e.key === 'I' || e.key === 'i')) {
e.preventDefault();
return false;
}
// Ctrl+Shift+J (控制台)
if (e.ctrlKey && e.shiftKey && (e.key === 'J' || e.key === 'j')) {
e.preventDefault();
return false;
}
// Ctrl+Shift+C (检查元素)
if (e.ctrlKey && e.shiftKey && (e.key === 'C' || e.key === 'c')) {
e.preventDefault();
return false;
}
// Ctrl+U (查看源代码)
if (e.ctrlKey && (e.key === 'U' || e.key === 'u')) {
e.preventDefault();
return false;
}
// Ctrl+S (保存页面,可能用于调试)
if (e.ctrlKey && (e.key === 'S' || e.key === 's')) {
e.preventDefault();
return false;
}
};
contextmenuHandler = function(e) {
if (!maskDiv) return;
e.preventDefault();
return false;
};
document.addEventListener('keydown', keydownHandler, true);
document.addEventListener('contextmenu', contextmenuHandler, true);
// ============================================================
// 防删除监视
if (!lockObserver) {
lockObserver = new MutationObserver(() => {
if (maskDiv && !document.contains(maskDiv)) {
buildLockMask();
}
});
lockObserver.observe(document.documentElement, { childList: true, subtree: false });
}
if (!lockInterval) {
lockInterval = setInterval(() => {
if (maskDiv && !document.contains(maskDiv)) {
buildLockMask();
}
}, 500);
}
}
function addLockButton() {
if (document.getElementById("relock-btn")) return;
const btn = document.createElement("button");
btn.id = "relock-btn";
btn.innerText = "🔒重新锁定";
btn.style.cssText = `
position:fixed !important;
bottom:16px !important;
right:16px !important;
z-index:0 !important;
padding:7px 14px;
border-radius:6px;
border:none;
background:#333;
color:white;
cursor:pointer;
`;
btn.onclick = () => {
const oldBtn = document.getElementById("relock-btn");
if (oldBtn) oldBtn.remove();
buildLockMask();
};
document.documentElement.appendChild(btn);
}
// 页面加载后自动锁定
const pageObserver = new MutationObserver(() => {
if (location.pathname.startsWith("/chat") && !maskDiv) {
buildLockMask();
pageObserver.disconnect();
}
});
pageObserver.observe(document.documentElement, { childList: true, subtree: true });
})();