跳到正文

科技·工程

防止颓废插件

da_ke约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月27日
篇幅
约 9 分钟

版本

  1. v1当前

背景:珂愛の da_ke 非常愛颓废,为了帮助她改掉这个毛病,珂愛の我用 Deepseek 写了一个插件。

省略和 Deepseek 的对话 114514 字。

更新后的脚本代码

JavaScript
// ==UserScript==
// @name         防止颓废 - 限制洛谷访问并重定向
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  禁止访问洛谷网站根域名及通知页面,允许访问题目页面,访问主站时重定向到指定页面
// @author       你的名字
// @match        *://www.luogu.com.cn/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 目标URL
    const targetRootUrl = 'https://www.luogu.com.cn/';
    const targetNotificationUrl = 'https://www.luogu.com.cn/user/notification';
    const redirectUrl = 'https://vjudge.net/article/8018'; // 重定向目标URL

    // 白名单:允许访问的路径
    const allowedPaths = [
        '/problem', // 允许访问题目页面
        '/problem/', // 允许访问题目页面
        '/problem/P', // 允许访问具体题目(如 /problem/P1001)
    ];

    // 检查当前URL是否在白名单中
    function isUrlAllowed(url) {
        return allowedPaths.some(path => url.startsWith(targetRootUrl + path));
    }

    // 禁止访问根域名并重定向
    if (window.location.href === targetRootUrl && !isUrlAllowed(window.location.href)) {
        window.stop();
        window.location.href = redirectUrl; // 重定向到指定页面
        return; // 直接退出,避免执行后续逻辑
    }

    // 禁止访问通知页面
    if (window.location.href === targetNotificationUrl) {
        window.stop();
        window.location.href = 'about:blank'; // 重定向到空白页
        alert('禁止访问通知页面!');
        return; // 直接退出,避免执行后续逻辑
    }

    // 监听鼠标悬停事件
    document.addEventListener('mouseover', function(event) {
        const target = event.target;

        // 检查目标是否是链接且包含目标URL
        if (target.tagName === 'A' && (target.href === targetRootUrl || target.href === targetNotificationUrl)) {
            // 阻止默认点击行为
            target.addEventListener('click', function(e) {
                e.preventDefault();
                alert('禁止访问该页面!');
            }, { once: true }); // 只监听一次,避免重复绑定
        }
    });

    // 监听所有链接点击事件
    document.addEventListener('click', function(event) {
        const target = event.target;

        // 检查目标是否是链接
        if (target.tagName === 'A') {
            // 如果链接是根域名,则重定向
            if (target.href === targetRootUrl) {
                event.preventDefault();
                window.location.href = redirectUrl;
            }
            // 如果链接是通知页面,则阻止访问
            else if (target.href === targetNotificationUrl) {
                event.preventDefault();
                alert('禁止访问通知页面!');
            }
            // 如果链接不在白名单中,则阻止访问
            else if (!isUrlAllowed(target.href)) {
                event.preventDefault();
                alert('禁止访问该页面!');
            }
        }
    });
})();

脚本功能说明

  1. 重定向主站访问:

    • 当用户访问 https://www.luogu.com.cn/ 时,脚本会立即重定向到 https://vjudge.net/article/8018。
  2. 白名单机制:

    • 允许访问 https://www.luogu.com.cn/problem 及其子页面(如 https://www.luogu.com.cn/problem/P1001)。
    • 其他页面(如通知页面)仍然会被禁止访问。
  3. 禁止访问通知页面:

    • 如果用户尝试访问 https://www.luogu.com.cn/user/notification,脚本会立即停止页面加载并重定向到空白页(about:blank)。
    • 弹出提示信息:“禁止访问通知页面!”。
  4. 阻止点击相关链接:

    • 当用户将鼠标悬停在指向 https://www.luogu.com.cn/ 或 https://www.luogu.com.cn/user/notification 的链接上时,脚本会监听点击事件。
    • 如果用户尝试点击这些链接,脚本会阻止默认行为并弹出提示信息。
  5. 全局链接点击拦截:

    • 监听所有链接的点击事件,如果链接是根域名,则重定向到指定页面;如果链接是通知页面或不在白名单中,则阻止访问并弹出提示。

使用方法

  1. 将上述代码复制到油猴脚本编辑器中。
  2. 保存并启用脚本。
  3. 尝试访问以下页面进行测试:
    • https://www.luogu.com.cn/:重定向到 https://vjudge.net/article/8018。
    • https://www.luogu.com.cn/user/notification:禁止访问,重定向到空白页。
    • https://www.luogu.com.cn/problem:允许访问。
    • https://www.luogu.com.cn/problem/P1001:允许访问。

测试场景

  1. 访问根域名:

    • 打开 https://www.luogu.com.cn/,页面会立即重定向到 https://vjudge.net/article/8018。
  2. 访问通知页面:

    • 打开 https://www.luogu.com.cn/user/notification,页面会被阻止加载并重定向到空白页,同时弹出提示信息。
  3. 访问题目页面:

    • 打开 https://www.luogu.com.cn/problem 或 https://www.luogu.com.cn/problem/P1001,页面可以正常加载。
  4. 点击相关链接:

    • 鼠标悬停在指向 https://www.luogu.com.cn/ 的链接上并点击,页面会重定向到 https://vjudge.net/article/8018。
    • 鼠标悬停在指向 https://www.luogu.com.cn/user/notification 的链接上并点击,链接点击会被阻止,并弹出提示信息。

注意事项

  1. 重定向目标:

    • 你可以根据需要修改 redirectUrl 变量,将其替换为其他目标 URL。
  2. 动态加载内容:

    • 如果页面内容是通过 JavaScript 动态加载的(例如 AJAX 或前端框架),可能需要使用 MutationObserver 监听 DOM 变化以确保脚本生效。
  3. 浏览器兼容性:

    • 脚本在现代浏览器(如 Chrome、Firefox)中测试通过,确保你的浏览器支持油猴插件。

通过以上脚本,你可以在防止颓废的同时,将洛谷主站的访问重定向到指定页面,并保留对题目页面的访问权限!

原文