Gesture controls (swipe/double-tap) for FreshRSS: double-tap to close articles, edge swipe to jump to article end, and auto-scroll
当前为
// ==UserScript==
// @name RSS: FreshRSS Gestures and Auto-Scroll
// @namespace http://tampermonkey.net/
// @version 3.1
// @description Gesture controls (swipe/double-tap) for FreshRSS: double-tap to close articles, edge swipe to jump to article end, and auto-scroll
// @author Your Name
// @homepage https://greatest.deepsurf.us/en/scripts/525912
// @match http://192.168.1.2:1030/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Debug mode
const DEBUG = false;
// Swipe detection configuration
const EDGE_THRESHOLD = 50; // Distance from edge to start swipe
const SWIPE_THRESHOLD = 50; // Minimum distance for a swipe
let touchStartX = 0;
let touchStartY = 0;
function debugLog(message) {
if (DEBUG) {
console.log(`[FreshRSS Script]: ${message}`);
}
}
debugLog('Script loaded');
// Function to scroll to element
function scrollToElement(element) {
if (element) {
const header = document.querySelector('header');
const headerHeight = header ? header.offsetHeight : 0;
const elementPosition = element.getBoundingClientRect().top + window.pageYOffset;
const scrollTarget = elementPosition - headerHeight - 20; // 20px padding from header
window.scrollTo({
top: scrollTarget,
behavior: 'smooth'
});
debugLog('Scrolling element near top: ' + element.id);
}
}
// Function to scroll to next element with peek
function scrollToNextElement(element) {
if (element) {
const nextElement = element.nextElementSibling;
if (nextElement) {
const header = document.querySelector('header');
const headerHeight = header ? header.offsetHeight : 0;
const nextElementPosition = nextElement.getBoundingClientRect().top + window.pageYOffset;
const scrollTarget = nextElementPosition - headerHeight - 25; // px padding from header
window.scrollTo({
top: scrollTarget,
behavior: 'smooth'
});
debugLog('Scrolled to show next element near top');
}
}
}
// Function to close active article
function closeActiveArticle(element) {
if (element) {
element.classList.remove('active');
debugLog('Closed article');
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
// Handle double-tap to close
document.addEventListener('dblclick', function(event) {
const interactiveElements = ['A', 'BUTTON', 'INPUT', 'TEXTAREA', 'SELECT', 'LABEL'];
if (interactiveElements.includes(event.target.tagName)) {
debugLog('Ignored double-tap on interactive element');
return;
}
const activeElement = event.target.closest('.flux.active');
if (activeElement) {
closeActiveArticle(activeElement);
}
});
// Touch event handlers for swipe detection
document.addEventListener('touchstart', function(event) {
touchStartX = event.touches[0].clientX;
touchStartY = event.touches[0].clientY;
// If touch starts from near the left edge, always prevent default
if (touchStartX <= EDGE_THRESHOLD) {
event.preventDefault();
debugLog('Touch started near left edge');
}
}, { passive: false });
document.addEventListener('touchmove', function(event) {
const currentX = event.touches[0].clientX;
const deltaX = currentX - touchStartX;
// If started from left edge, prevent default during the swipe
if (touchStartX <= EDGE_THRESHOLD && deltaX > 0) {
event.preventDefault();
debugLog('Preventing default during edge swipe');
}
}, { passive: false });
document.addEventListener('touchend', function(event) {
if (!touchStartX) return;
const touchEndX = event.changedTouches[0].clientX;
const deltaX = touchEndX - touchStartX;
// If it was a left-to-right swipe starting from the edge
if (touchStartX <= EDGE_THRESHOLD && deltaX >= SWIPE_THRESHOLD) {
const activeElement = document.querySelector('.flux.active');
if (activeElement) {
event.preventDefault();
scrollToNextElement(activeElement);
}
}
// Reset touch tracking
touchStartX = 0;
touchStartY = 0;
}, { passive: false });
// Mutation observer to catch programmatic changes
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.target.classList && mutation.target.classList.contains('flux')) {
if (mutation.target.classList.contains('active')) {
debugLog('Article became active via mutation');
scrollToElement(mutation.target);
}
}
});
});
// Start observing the document with the configured parameters
observer.observe(document.body, {
attributes: true,
attributeFilter: ['class'],
subtree: true
});
})();