打印带有特定参数的力扣问题链接,并绑定按键事件
当前为
// ==UserScript==
// @name 力扣上一题下一题快捷键
// @namespace http://tampermonkey.net/
// @version 1.1
// @description 打印带有特定参数的力扣问题链接,并绑定按键事件
// @author tianyw0
// @match https://leetcode.cn/problems/*
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
let filteredLinks = [];
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('未找到足够符合条件的链接,重新查找...');
setTimeout(findLinks, 1000); // 休息1秒后重新查找
} else {
console.log(`找到 ${filteredLinks.length} 个符合条件的链接:`);
filteredLinks.forEach((link, index) => console.log(`链接 ${index + 1}: ${link.href}`));
bindKeyEvents();
}
}
function bindKeyEvents() {
document.addEventListener('keydown', (event) => {
const key = event.key;
if ((key === ',' || key === 'P') && filteredLinks[0]) {
console.log(`点击链接: ${filteredLinks[0].href}`);
filteredLinks[0].click();
} else if ((key === '.' || key === 'N') && filteredLinks[1]) {
console.log(`点击链接: ${filteredLinks[1].href}`);
filteredLinks[1].click();
}
});
}
// 初始查找
setTimeout(findLinks, 1000); // 初始休息1秒后开始查找
})();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址