Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active December 30, 2024 13:20
Show Gist options
  • Save PlugFox/7315cad8ef028e2751f4a971ca9d59e9 to your computer and use it in GitHub Desktop.
Save PlugFox/7315cad8ef028e2751f4a971ca9d59e9 to your computer and use it in GitHub Desktop.
Tampermonkey Auto Scroll Toggle for comick.io
// ==UserScript==
// @name Auto Scroll Toggle for comick.io
// @namespace plugfox
// @version 1.3
// @description Toggle auto scroll on and off with a hotkey, and stop on page blur
// @author @plugfox
// @run-at document-idle
// @homepage https://gist.github.com/PlugFox/7315cad8ef028e2751f4a971ca9d59e9
// @homepageURL https://gist.github.com/PlugFox/7315cad8ef028e2751f4a971ca9d59e9
// @match *://comick.io/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const scrollSpeed = 5; // Adjust this value to change the scroll speed
let scrolling = false;
let scrollInterval;
let wakeLock = null;
async function requestWakeLock() {
try {
wakeLock = await navigator.wakeLock.request('screen');
wakeLock.addEventListener('release', () => {
console.log('Screen Wake Lock was released');
});
console.log('Screen Wake Lock is active');
} catch (err) {
console.error(`${err.name}, ${err.message}`);
}
}
async function releaseWakeLock() {
if (wakeLock !== null) {
await wakeLock.release();
wakeLock = null;
console.log('Screen Wake Lock was released manually');
}
}
function startScrolling() {
if (scrolling) return;
scrolling = true;
scrollInterval = setInterval(() => {
window.scrollBy(0, scrollSpeed);
}, 20);
}
function stopScrolling() {
if (!scrolling) return;
scrolling = false;
clearInterval(scrollInterval);
}
function toggle() {
if (scrolling) {
stopScrolling();
releaseWakeLock();
} else {
requestWakeLock();
startScrolling(); // alert('!!!');
}
}
function addScrollButton() {
const icon = document.createElement('div');
icon.title = 'Scroll it';
icon.classList.add('scroll-button');
icon.style.position = 'fixed';
icon.style.bottom = '72px'; // 16 px
icon.style.right = '8px'; // 16 px
icon.style.zIndex = '1000';
icon.style.cursor = 'pointer';
icon.style.width = '48px';
icon.style.height = '48px';
icon.style.color = '#e5e7eb';
icon.style.backgroundColor = '#374151';
icon.style.borderRadius = '50%';
icon.style.display = 'flex';
icon.style.alignItems = 'center';
icon.style.justifyContent = 'center';
icon.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)';
icon.style.fontSize = '24px';
icon.textContent = '↓';
// Handle click
icon.addEventListener('click', function () {
toggle();
});
// Append icon to body
document.body.appendChild(icon);
}
let isKeyDown = false; // Флаг, чтобы предотвратить множественные нажатия подряд
function simulateKeyPress(key) {
if (isKeyDown) return; // Если флаг установлен, игнорируем последующие вызовы
isKeyDown = true;
const event = new KeyboardEvent("keydown", {
key: key,
code: key,
bubbles: true,
cancelable: true
});
document.dispatchEvent(event);
setTimeout(() => {
const upEvent = new KeyboardEvent("keyup", {
key: key,
code: key,
bubbles: true,
cancelable: true
});
document.dispatchEvent(upEvent);
// Очищаем флаг после задержки для следующего вызова
setTimeout(() => {
isKeyDown = false;
}, 200); // Интервал между нажатиями, можно увеличить для уменьшения частоты
}, 100); // Задержка для быстрого отпускания клавиши
}
function checkGamepadState() {
const gamepads = navigator.getGamepads();
for (let i = 0; i < gamepads.length; i++) {
const gp = gamepads[i];
if (!gp) continue;
// Проверка кнопок геймпада
gp.buttons.forEach((button, index) => {
if (!button.pressed) return;
switch (index) {
case 4:
// Left bumper pressed
window.scrollBy(0, -10 * scrollSpeed);
break;
case 5:
// Right bumper pressed
window.scrollBy(0, 10 * scrollSpeed);
break;
case 6:
// Left trigger pressed
window.scrollBy(0, -2 * scrollSpeed * button.value);
break;
case 7:
// Right trigger pressed
window.scrollBy(0, 2 * scrollSpeed * button.value);
break;
case 12:
// D-Pad Up
simulateKeyPress("ArrowUp");
break;
case 13:
// D-Pad Down
simulateKeyPress("ArrowDown");
break;
case 14:
// D-Pad Left
simulateKeyPress("ArrowLeft");
break;
case 15:
// D-Pad Right
simulateKeyPress("ArrowRight");
break;
default:
// If button pressed - scroll it
window.scrollBy(0, 5 * scrollSpeed);
break;
}
});
gp.axes.forEach((axis, index) => {
const min = 0.3, max = 1.0;
const left = true, right = false;
var offset = Math.abs(axis);
if (offset < min) {
return;
} else if (left && index === 1 || right && index === 3) {
// If axis are up or down - scoll it
offset = max * offset + min * (1 - offset);
window.scrollBy(0, Math.sign(axis) * 2 * scrollSpeed * offset);
}
});
}
requestAnimationFrame(checkGamepadState);
}
window.addEventListener('load', function () {
addScrollButton();
window.addEventListener('keydown', (e) => {
if (e.repeat) { return } // Prevent repeated actions
if (scrolling) {
e.preventDefault(); // Prevent default handler
toggle(); // Disable scroll
} else if ((e.key == " " || e.code == "Space" || e.keyCode == 32)
&& (e.ctrlKey || e.metaKey)) {
e.preventDefault(); // Prevent default handler
toggle(); // Enable scroll
} else if (e.code == "Space") {
event.preventDefault();
event.stopPropagation();
window.scrollBy({
top: 1000,
left: 0,
behavior: "smooth",
});
}
});
window.addEventListener('blur', () => {
releaseWakeLock();
stopScrolling();
});
if (('getGamepads' in navigator)) {
window.addEventListener('gamepadconnected', (event) => {
//console.log('Gamepad connected:', event.gamepad);
requestAnimationFrame(checkGamepadState);
});
window.addEventListener('gamepaddisconnected', (event) => {
//console.log('Gamepad disconnected:', event.gamepad);
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment