// ==UserScript==
// @name 偶然中文快捷翻页脚本
// @namespace http://tampermonkey.net/
// @version 0.4
// @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) {
// 如果按键发生在输入框或文本域内,不执行滚动操作
if (e.target.tagName.toLowerCase() === 'input' ||
e.target.tagName.toLowerCase() === 'textarea') {
return;
}
let distance;// 初始化滚动距离
switch(e.key) {
case 'ArrowDown':
case 's':
// 向下滚动视口高度的部分距离
distance = window.innerHeight * SCROLL_FRACTION;
smoothScroll(distance, 'down');
break;
case 'ArrowUp':
case 'w':
// 向上滚动视口高度的部分距离
distance = -window.innerHeight * SCROLL_FRACTION;
smoothScroll(distance, 'up');
break;
default:
}
}, false);
// 定义平滑滚动
function smoothScroll(distance, direction) {
if ('scrollBehavior' in document.documentElement.style) {
window.scrollBy({
top: distance,
left: 0,
behavior: 'smooth'
});
} else {
window.scrollBy(0, distance);
}
}
// 双击检测变量
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('下一章');
}
}
});
})();