// ==UserScript== // @name 偶然中文快捷翻页脚本 // @namespace http://tampermonkey.net/ // @version 0.5 // @description 单击键盘左右键或ad键进行上一页下一页切换,双击进行上下章节切换;单击键盘上下键或ws键进行页面内滚动。 // @author coccvo // @match *://www.or77.net/* // @icon https://www.or77.net/favicon.ico // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 模拟点击函数 function simulateClickByText(textContent) { // 查找所有标签 const links = document.querySelectorAll('a'); // 遍历每个标签 for (let i = 0; i < links.length; i++) { // 如果文本内容匹配,则模拟点击 if (links[i].textContent.trim() === textContent) { links[i].click(); break; } } } const SCROLL_FRACTION = 0.8; // 设置滚动距离为视口高度的百分比 document.addEventListener('keydown', function(e) { // 定义一个数组包含需要处理的按键 const keysToHandle = ['ArrowUp', 'ArrowDown', 'w', 's']; // 检查按键是否在需要处理的列表中 if (keysToHandle.includes(e.key)) { // 计算滚动距离 let distance = e.key === 'ArrowDown' || e.key === 's' ? window.innerHeight * SCROLL_FRACTION : -window.innerHeight * SCROLL_FRACTION; // 执行平滑滚动 if ('scrollBehavior' in document.documentElement.style) { window.scrollBy({ top: distance, left: 0, behavior: 'smooth' }); } else { window.scrollBy(0, distance); } // 阻止按键的默认行为,以允许脚本完全控制滚动(这将改变上下箭头的默认滚动行为) e.preventDefault(); } }, false); // 双击检测变量 let clickTimer = null; // 监听键盘事件 document.addEventListener('keydown', function(e) { // 左键或 'a' 键(上一页) if (e.key === 'ArrowLeft' || e.key === 'a') { if (clickTimer == null) { clickTimer = setTimeout(function() { simulateClickByText('上一页'); clickTimer = null; }, 300); } else { clearTimeout(clickTimer); clickTimer = null; simulateClickByText('上一章'); } } // 右键或 'd' 键(下一页) else if (e.key === 'ArrowRight' || e.key === 'd') { if (clickTimer == null) { clickTimer = setTimeout(function() { simulateClickByText('下一页'); clickTimer = null; }, 300); } else { clearTimeout(clickTimer); clickTimer = null; simulateClickByText('下一章'); } } }); })();