The provided code is a **userscript** intended to optimize video...

September 3, 2025 at 03:13 PM

// ==UserScript== // @name Anime Buffering Speed Controller - Based on Video Tutorials // @namespace http://tampermonkey.net/ // @version 1.0 // @description Tiered buffering speed control (6x, 8x, 9x) using concepts from meditation app tutorial // @author VideoTutorialBased // @match *://animekai.to/* // @match *://hianime.nz/* // @grant none // ==/UserScript== (function() { 'use strict'; // App function following the meditation app structure from the tutorial const app = () => { // Configuration using data attribute concept from meditation app const speedTiers = [6, 8, 9]; // Tier speeds as requested let currentTier = 0; let fakeDuration = 600; // Using fake duration concept from meditation app let lastBufferCheck = Date.now(); let bufferProgress = 0; // Video selection using querySelector concept from tutorial const detectVideos = () => { // Multiple selector approach like in the meditation app const videoSelectors = [ 'video', 'video[src]', '.video-player video', '#video-player video', '[id*="video"] video', '[class*="video"] video', '[id*="player"] video', '[class*="player"] video' ]; for (const selector of videoSelectors) { const videos = document.querySelectorAll(selector); if (videos.length > 0) { return Array.from(videos); } } return []; }; // Get buffer percentage using video properties (like currentTime in meditation app) const getBufferPercentage = (video) => { if (!video || !video.buffered || video.buffered.length === 0) { return 0; } try { const bufferedEnd = video.buffered.end(video.buffered.length - 1); const duration = video.duration; return duration > 0 ? (bufferedEnd / duration) : 0; } catch (e) { return 0; } }; // Check playing state like in meditation app tutorial const checkBuffering = (video) => { if (!video) return false; const currentTime = Date.now(); const currentBuffer = getBufferPercentage(video); // Using timing concepts from meditation app const timeDiff = currentTime - lastBufferCheck; const bufferDiff = currentBuffer - bufferProgress; // If buffer hasn't increased in 3 seconds (like timer logic in meditation app) if (bufferDiff <= 0.01 && timeDiff > 3000 && !video.paused) { return true; // Slow buffering detected } bufferProgress = currentBuffer; lastBufferCheck = currentTime; return false; }; // Apply speed tier using playbackRate (similar to how meditation app controls audio) const applySpeedTier = (video, tier) => { if (!video || tier < 0 || tier >= speedTiers.length) return; const speed = speedTiers[tier]; const originalRate = video.playbackRate; // Apply speed boost temporarily (like how meditation app handles play/pause) video.playbackRate = speed; console.log("Applied tier " + (tier + 1) + ": " + speed + "x speed"); // Reset after 2 seconds (using setTimeout like meditation app timing) setTimeout(() => { if (video.playbackRate === speed) { video.playbackRate = originalRate; } }, 2000); }; // Monitor buffering using ontimeupdate concept from meditation app const monitorVideo = (video) => { // Using ontimeupdate approach from meditation app tutorial video.ontimeupdate = () => { const bufferPercentage = getBufferPercentage(video); // Low buffer detection (like the progress calculations in meditation app) if (!video.paused && bufferPercentage < 0.15) { console.log("Low buffer detected: " + Math.floor(bufferPercentage * 100) + "%"); if (checkBuffering(video)) { console.log("Slow buffering - increasing tier"); // Move to next tier if available (like tier switching logic) if (currentTier < speedTiers.length - 1) { currentTier++; applySpeedTier(video, currentTier); } } } else if (bufferPercentage > 0.5) { // Good buffer - reset to tier 0 (like meditation app resets) if (currentTier > 0) { currentTier = 0; console.log("Buffer recovered - reset to tier 1"); } } }; // Add event listeners like in meditation app video.addEventListener('waiting', () => { console.log('Video waiting for data - applying speed boost'); applySpeedTier(video, currentTier); }); video.addEventListener('progress', () => { // Monitor progress like the meditation app monitors audio progress const buffer = getBufferPercentage(video); if (buffer < 0.1 && !video.paused) { applySpeedTier(video, currentTier); } }); console.log('Video monitoring initialized using meditation app concepts'); }; // Initialize video detection and monitoring const initializeApp = () => { console.log('Anime Buffering Speed Controller initialized'); // Find videos using querySelector approach from tutorial const videos = detectVideos(); if (videos.length > 0) { // Monitor first video found (like meditation app selects first song) monitorVideo(videos); } // Use setInterval for periodic checks (like meditation app timer logic) setInterval(() => { const newVideos = detectVideos(); if (newVideos.length > 0) { // Check if we need to monitor new videos newVideos.forEach(video => { if (!video.ontimeupdate || video.ontimeupdate.toString().indexOf('buffer') === -1) { monitorVideo(video); } }); } }, 5000); }; // Start the app (like calling app() in meditation tutorial) initializeApp(); }; // Wait for DOM ready and then start app (following meditation app pattern) if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', app); } else { app(); } })();

The provided code is a userscript intended to optimize video playback buffering on anime streaming sites, specifically targeting animekai.to and hianime.nz. It enhances the video's buffering behavior based on detected conditions, borrowing concepts from a "meditation app tutorial" for handling timings, monitoring, and controls. Here's an explanation of its functionality:

Key Features and Behavior

  1. Purpose:

    • The script adjusts the playback speed dynamically when poor buffering conditions are detected, with the goal of improving buffering and preventing playback interruptions on the targeted websites.
  2. Targeted Websites:

    • The script applies only to animekai.to and hianime.nz (@match fields indicate the sites where this script will be injected).
  3. Buffer Monitoring:

    • Videos are identified using a variety of selectors such as <video> and other related elements.
    • The buffering progress of the detected videos is continuously monitored using video properties like:
      • buffered: Tracks the loaded parts of the video.
      • duration: The length of the video.
      • playbackRate: The current playback speed.
  4. Slow Buffering Detection:

    • The script measures how often the buffering percentage increases.
    • If buffering progress is too slow (e.g., no improvement in 3 seconds) and the video is playing, it considers buffering to be slow and invokes corrective measures.
  5. Playback Speed Tiers:

    • It uses three tiered playback speeds: 6x, 8x, and 9x.
    • Under poor buffering conditions, it switches to the next tier (higher playback speed).
    • When buffering improves, it resets playback speed to the original (normal speed).
  6. Corrective Playback Speed Changes:

    • When slow buffering is detected or the video is "waiting" for data:
      • The playback speed is temporarily increased according to the current tier, which serves to quickly load more video data during slow buffering.
      • After 2 seconds, the playback rate is restored to its original value.
  7. Event Listeners:

    • ontimeupdate: Monitors the video's buffering percentage and adjusts tiers accordingly.
    • waiting: Handles cases when the video pauses to buffer more data.
    • progress: Listens for video loading progress events and tweaks tiers if buffer levels are inadequate.
  8. Continuous Video Monitoring:

    • The script regularly scans the page every 5 seconds (setInterval) for newly added videos and initializes monitoring for them, ensuring that the buffering optimizations apply even if videos are dynamically loaded on the page.
  9. Initialization:

    • The script waits for the page's DOM content to load completely before executing the core logic.

Summary

The userscript improves video playback on the target anime streaming sites by:

  • Monitoring the buffer state of videos.
  • Dynamically increasing playback speed (to load data quicker) during poor buffering.
  • Resetting playback speed to normal once buffering improves.
  • Handling dynamic page content by scanning for new videos periodically.

It's particularly useful for users experiencing buffering issues on these specific sites.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node