Greasy Fork is available in English.

力扣上一题下一题快捷键

绑定力扣刷题页面上一题下一题快捷键

// ==UserScript==
// @name         力扣上一题下一题快捷键
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  绑定力扣刷题页面上一题下一题快捷键
// @author       tianyw0
// @match        https://leetcode.cn/problems/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let filteredLinks = [];

    function triggerEmptyHrefLinks() {
        const emptyHrefLinks = Array.from(document.querySelectorAll('a'))
            .filter(a => a.hasAttribute('href') && a.getAttribute('href') === '');

        emptyHrefLinks.forEach(link => {
            link.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, cancelable: true }));
        });
    }

    function findLinks() {
        const links = document.querySelectorAll('a[href^="/problems/"][href*="?"]');
        filteredLinks = Array.from(links).filter(link => {
            const url = new URL(link.href, window.location.origin);
            const path = url.pathname.split('/');
            const problemName = path[2];
            return problemName && url.searchParams.has('envType') && url.searchParams.has('envId') && url.searchParams.get('envType') !== 'daily-question';
        });

        if (filteredLinks.length >= 2) {
            console.log(`找到 ${filteredLinks.length} 个符合条件的链接:`);
            filteredLinks.forEach((link, index) => console.log(`链接 ${index + 1}: ${link.href}`));
            bindKeyEvents();
        } else {
            console.log('未找到足够符合条件的链接,重新查找...');
            triggerEmptyHrefLinks(); // 重试时触发hover事件
            setTimeout(findLinks, 1000); // 休息1秒后重新查找
        }
    }

    function bindKeyEvents() {
        document.addEventListener('keydown', (event) => {
            const key = event.key.toUpperCase();
            if ((key === ',' || key === 'P') && filteredLinks[0]) {
                console.log(`点击链接(Prev): ${filteredLinks[0].href}`);
                filteredLinks[0].click();
                simulateClickEffect(filteredLinks[0]);
            } else if ((key === '.' || key === 'N') && filteredLinks[1]) {
                console.log(`点击链接(Next): ${filteredLinks[1].href}`);
                filteredLinks[1].click();
                simulateClickEffect(filteredLinks[1]);
            }
        });
    }

    function simulateClickEffect(link) {
        link.style.backgroundColor = '#474747';
        setTimeout(() => {
            link.style.backgroundColor = '';
        }, 50);
    }

    // 初始触发空链接请求
    triggerEmptyHrefLinks();

    // 初始查找
    setTimeout(findLinks, 1000); // 初始休息1秒后开始查找
})();