Better Webtoons

Eye protection, WASD scrolling, Tab navigation, Horizontal Position Memory, and Quick Like.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name        Better Webtoons
// @namespace   https://miku.us.kg/
// @version     1.3
// @description Eye protection, WASD scrolling, Tab navigation, Horizontal Position Memory, and Quick Like.
// @author      @redsus.vn on Discord
// @match       *://*.webtoons.com/*
// @match       *://*.naver.com/*
// @grant       none
// @run-at      document-start
// @license     CC BY-NC 4.0
// ==/UserScript==

(function() {
    'use strict';

    const STORAGE_KEY = 'wt_eye_protection_yellow_mode';
    const OFFSET_STORAGE_KEY = 'wt_viewer_horizontal_offset';

    const MODES = [
        'transparent',
        'rgba(255, 215, 0, 0.2)',
        'rgba(255, 140, 0, 0.45)'
    ];

    let currentMode = 0;
    let currentOffset = 0;

    try {
        const saved = localStorage.getItem(STORAGE_KEY);
        if (saved !== null) {
            currentMode = parseInt(saved);
        }
        const savedOffset = localStorage.getItem(OFFSET_STORAGE_KEY);
        if (savedOffset !== null) {
            currentOffset = parseInt(savedOffset);
        }
    } catch (e) {
        console.log("Storage access blocked");
    }

    if (currentMode >= MODES.length || isNaN(currentMode)) currentMode = 0;
    if (isNaN(currentOffset)) currentOffset = 0;

    function applyOverlay() {
        let overlay = document.getElementById('yellow-filter-overlay');

        if (!overlay) {
            const parent = document.documentElement || document.body;
            if (!parent) return;

            overlay = document.createElement('div');
            overlay.id = 'yellow-filter-overlay';
            overlay.style.cssText = `
                position: fixed;
                top: 0; left: 0;
                width: 100vw; height: 100vh;
                pointer-events: none;
                z-index: 2147483647;
                mix-blend-mode: multiply;
                transition: background-color 0.2s ease;
            `;
            parent.appendChild(overlay);
        }
        overlay.style.backgroundColor = MODES[currentMode];
    }

    function applyOffset() {
        const target = document.getElementById('wrap') || document.body;
        if (!target) return;

        // Automatically disable offset if window is wide (normal tab view)
        // Checks if window width is > 90% of available screen width
        const isWide = window.innerWidth > (window.screen.availWidth || window.innerWidth) * 0.9;
        const effectiveOffset = isWide ? 0 : currentOffset;

        if (target === document.body) {
            if (effectiveOffset !== 0) {
                target.style.marginLeft = effectiveOffset + 'px';
                target.style.overflowX = 'hidden';
            } else {
                target.style.marginLeft = '';
                target.style.overflowX = '';
            }
        } else {
            target.style.transform = effectiveOffset !== 0 ? `translateX(${effectiveOffset}px)` : '';
        }
    }

    function createOffsetControl() {
        if (document.getElementById('wt-offset-slider')) return;
        if (!document.body) return;

        const container = document.createElement('div');
        container.style.cssText = `
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 20px;
            z-index: 2147483648;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(0,0,0,0.05);
            opacity: 0;
            transition: opacity 0.3s;
        `;

        container.onmouseenter = () => container.style.opacity = '1';
        container.onmouseleave = () => container.style.opacity = '0';

        const slider = document.createElement('input');
        slider.type = 'range';
        slider.id = 'wt-offset-slider';
        slider.min = -2000;
        slider.max = 2000;
        slider.value = currentOffset;
        slider.style.cssText = `
            width: 90%;
            height: 6px;
            cursor: pointer;
            border-radius: 3px;
        `;

        slider.oninput = function() {
            currentOffset = parseInt(this.value);
            applyOffset();
        };

        slider.onchange = function() {
            localStorage.setItem(OFFSET_STORAGE_KEY, currentOffset);
            this.blur();
        };

        container.appendChild(slider);
        document.body.appendChild(container);
    }

    function createButton() {
        if (document.getElementById('yellow-mode-btn')) return;
        if (!document.body) return;

        // Inject CSS for the smooth hover animation
        const style = document.createElement('style');
        style.innerHTML = `
            #yellow-mode-btn {
                position: fixed;
                bottom: 120px; /* Positioned slightly above the scroll-to-top button */
                right: 0;
                width: 8px;
                height: 60px;
                background: #222;
                border-radius: 4px 0 0 4px;
                cursor: pointer;
                z-index: 2147483648;
                display: flex;
                align-items: center;
                justify-content: center;
                transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
                box-shadow: -2px 2px 5px rgba(0,0,0,0.2);
                overflow: hidden;
            }
            #yellow-mode-btn:hover {
                width: 60px;
                background: #fff;
                border: 3px solid #000;
                border-radius: 12px 0 0 12px;
            }
            #yellow-mode-btn span {
                opacity: 0;
                font-size: 28px;
                transform: scale(0.5);
                transition: all 0.3s ease;
            }
            #yellow-mode-btn:hover span {
                opacity: 1;
                transform: scale(1);
            }
        `;
        document.head.appendChild(style);

        const btn = document.createElement('div');
        btn.id = 'yellow-mode-btn';
        btn.innerHTML = '<span>👁️</span>';
        btn.title = "Toggle Yellow Mode";

        btn.onclick = function() {
            currentMode++;
            if (currentMode >= MODES.length) currentMode = 0;
            localStorage.setItem(STORAGE_KEY, currentMode);
            applyOverlay();
        };

        document.body.appendChild(btn);
    }

    // Custom notification function for "Liked" and "Disliked"
    function showNotification(message) {
        let notif = document.getElementById('wt-like-notification');
        
        if (!notif) {
            notif = document.createElement('div');
            notif.id = 'wt-like-notification';
            notif.style.cssText = `
                position: fixed;
                bottom: 30px;
                left: 50%;
                transform: translateX(-50%);
                background: rgba(0, 0, 0, 0.85);
                color: white;
                padding: 12px 24px;
                border-radius: 30px;
                font-size: 16px;
                font-family: sans-serif;
                font-weight: bold;
                z-index: 2147483649;
                pointer-events: none;
                transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out;
                opacity: 0;
                box-shadow: 0 4px 6px rgba(0,0,0,0.3);
            `;
            document.body.appendChild(notif);
        }

        notif.textContent = message;
        notif.style.opacity = '1';
        notif.style.transform = 'translateX(-50%) translateY(-10px)';

        if (notif.timeoutId) clearTimeout(notif.timeoutId);

        notif.timeoutId = setTimeout(() => {
            notif.style.opacity = '0';
            notif.style.transform = 'translateX(-50%) translateY(0)';
        }, 2000);
    }

    function setupKeyboardControls() {
        window.addEventListener('keydown', (e) => {
            const tag = document.activeElement.tagName;
            if (tag === 'INPUT' || tag === 'TEXTAREA' || document.activeElement.isContentEditable) {
                return;
            }

            const key = e.key.toLowerCase();
            const scrollAmount = window.innerHeight * 0.85;

            // Handle Like Toggle
            if (key === '`' || key === '~') {
                e.preventDefault();
                const likeBtn = document.getElementById('likeItButton');
                if (likeBtn) {
                    const em = likeBtn.querySelector('em._btnLike');
                    // Check if it's currently liked BEFORE clicking
                    const isCurrentlyLiked = em && em.classList.contains('on');
                    
                    // Trigger the click natively
                    likeBtn.click();
                    
                    if (isCurrentlyLiked) {
                        showNotification('💔 Disliked');
                    } else {
                        showNotification('❤️ Liked!');
                    }
                }
                return;
            }

            if (['arrowdown', 'arrowright', 's', 'd'].includes(key)) {
                e.preventDefault();
                window.scrollBy({ top: scrollAmount, behavior: 'smooth' });
            }

            if (['arrowup', 'arrowleft', 'w', 'a'].includes(key)) {
                e.preventDefault();
                window.scrollBy({ top: -scrollAmount, behavior: 'smooth' });
            }

            if (e.key === 'Tab') {
                e.preventDefault();

                if (e.shiftKey) {
                    const prevBtn = document.querySelector('.pg_prev._prevEpisode');
                    if (prevBtn) {
                        prevBtn.click();
                    } else {
                        console.log("No previous chapter found.");
                    }
                } else {
                    const nextBtn = document.querySelector('.pg_next._nextEpisode');
                    if (nextBtn) {
                        nextBtn.click();
                    } else {
                        console.log("No next chapter found.");
                    }
                }
            }
        });
    }

    // Add listener to reset offset when window is resized (e.g. going to full screen)
    window.addEventListener('resize', applyOffset);

    let controlsSetup = false;
    const startLoop = setInterval(() => {
        if (document.documentElement) applyOverlay();
        applyOffset();

        if (!controlsSetup) {
            setupKeyboardControls();
            controlsSetup = true;
        }

        if (document.body) {
            createButton();
            createOffsetControl();
            if (document.getElementById('yellow-filter-overlay') &&
                document.getElementById('yellow-mode-btn') &&
                document.getElementById('wt-offset-slider')) {
                clearInterval(startLoop);
            }
        }
    }, 50);

})();