还颓?
看到这篇文章的人,应该都需要这个(不然你咋不去写题?
花了半个小时写了这个油猴脚本,作用是彻底防止谷民摸鱼。
具体效果的话,这里不好说,试试就知道了。
Code
// ==UserScript==
// @name 还颓?
// @namespace http://tampermonkey.net/
// @version 6.2.0
// @description 横扫犇犇,做回自己 诶,专栏好像也是哦
// @author Super_Diu(doubao)
// @match https://www.luogu.com.cn/*
// @exclude https://www.luogu.com.cn/api/*
// @exclude https://www.luogu.com.cn/auth/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// 基础配置
const targetDomain = 'https://www.luogu.com.cn';
const problemReg = /^https:\/\/www\.luogu\.com\.cn\/problem\/?/; // 匹配所有洛谷题目页
const indexPath = '/';
const triggerArticleCats = [5, 7]; // 生活·游记5、休闲·娱乐7
let isTriggered = false; // 全局防重复触发,整站仅一次
const LOG_PREFIX = '[洛谷防颓脚本]';
// 生成随机P题URL:P1000-P15195
function getRandomProblemUrl() {
const type = 'P';
const num = Math.floor(Math.random() * (15195 - 1000 + 1)) + 1000;
return `${targetDomain}/problem/${type}${num}`;
}
// 核心跳转逻辑:随机P题
function jumpToProblem() {
const randomUrl = getRandomProblemUrl();
window.location.href = randomUrl;
}
// 阻止原生行为 + 触发跳转(所有按钮拦截复用此方法,避免冗余)
function interceptAndJump(e, triggerName) {
// 四重拦截:确保原生功能完全失效
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
e.target.onclick = () => false;
// 触发跳转
jumpToProblem();
}
// 触发条件1:首页点击「发射犇犇!」- 拦截+跳转
const isIndexPage = window.location.host === 'www.luogu.com.cn' && window.location.pathname === indexPath;
if (isIndexPage) {
document.addEventListener('click', function(e) {
let cur = e.target;
while (cur && cur !== document.body) {
const text = cur.textContent?.trim() || '';
if (text === '发射犇犇!') {
interceptAndJump(e, '发射犇犇!');
return;
}
cur = cur.parentElement;
}
}, true);
}
// 触发条件2:生活·游记/休闲·娱乐专栏页 - 打开即跳转(含URL变化监听)
function checkArticlePageAndJump() {
const isPureArticlePage = window.location.pathname === '/article' && !/\/article\/\w+/.test(window.location.href);
if (!isPureArticlePage) return false;
const urlParams = new URLSearchParams(window.location.search);
const articleCat = Number(urlParams.get('category'));
if (!Number.isFinite(articleCat) || !triggerArticleCats.includes(articleCat)) return false;
const catName = articleCat === 5 ? '生活·游记' : '休闲·娱乐';
jumpToProblem();
return true;
}
const isArticleJumped = checkArticlePageAndJump();
// 监听URL变化,适配洛谷无刷新跳转
let lastHref = window.location.href;
setInterval(() => {
if (window.location.href !== lastHref) {
lastHref = window.location.href;
checkArticlePageAndJump();
}
}, 200);
// 触发条件3:犇犇区【回复】按钮 - 精准匹配+拦截+跳转
// 匹配特征:a标签 name="feed-reply" 且文本为「回复」
document.addEventListener('click', function(e) {
const target = e.target;
// 精准匹配犇犇回复按钮的DOM特征,避免误拦截
if (target.tagName === 'A' && target.name === 'feed-reply' && target.textContent.trim() === '回复') {
interceptAndJump(e, '犇犇回复');
}
}, true);
// 触发条件4:犇犇区【点击查看更多...】按钮 - 精准匹配+拦截+跳转
// 匹配特征:id="feed-more" 的div下的a标签,文本为「点击查看更多...」
document.addEventListener('click', function(e) {
let cur = e.target;
while (cur && cur !== document.body) {
// 精准匹配犇犇查看更多的DOM特征
if (cur.tagName === 'DIV' && cur.id === 'feed-more') {
interceptAndJump(e, '犇犇查看更多');
return;
}
cur = cur.parentElement;
}
}, true);
})();