CCCPaste

Auto Unmute and Reset Instagram Reels

// ==UserScript==
// @name Auto Unmute and Reset Instagram Reels 1.0
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Automatically unmutes Instagram Reels on desktop and resets playback to start
// @author Grok
// @match https://www.instagram.com/reel/*
// @grant none
// ==/UserScript==
(function() {
    'use strict';
    // Function to unmute video and reset playback to start
    function unmuteVideo(retries = 5) {
        const unmuteButton = document.querySelector('div[aria-label="A hang némítva van."][role="button"]');
        if (unmuteButton) {
            unmuteButton.click();
            console.log('Unmute button clicked successfully');
            // Find the closest video element
            const video = unmuteButton.closest('div').querySelector('video') || document.querySelector('video');
            if (video) {
                video.currentTime = 0;
                console.log('Video playback reset to start');
            } else {
                console.log('Video element not found for reset');
            }
            return;
        }
        // Fallback: directly unmute any muted video elements and reset
        const videos = document.querySelectorAll('video');
        videos.forEach(video => {
            if (video.muted) {
                video.muted = false;
                video.currentTime = 0;
                console.log('Video directly unmuted and reset to start');
            }
        });
        // Retry if button not found and retries remain
        if (retries > 0) {
            console.log(`Unmute button not found, retrying (${retries} attempts left)`);
            setTimeout(() => unmuteVideo(retries - 1), 500);
        } else {
            console.log('Unmute button not found after all retries');
        }
    }
    // MutationObserver to detect when the unmute button is added
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === 1 && node.matches('div[aria-label="A hang némítva van."][role="button"]')) {
                        console.log('Unmute button detected in DOM');
                        setTimeout(() => {
                            node.click();
                            console.log('Unmute button clicked via observer');
                            const video = node.closest('div').querySelector('video') || document.querySelector('video');
                            if (video) {
                                video.currentTime = 0;
                                console.log('Video playback reset to start via observer');
                            } else {
                                console.log('Video element not found for reset via observer');
                            }
                        }, 100);
                    }
                });
            }
        });
    });
    // Start observing the document for changes
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
    // Try unmuting initially
    setTimeout(() => unmuteVideo(), 1000);
    // Listen for clicks on Reel links
    document.addEventListener('click', (event) => {
        if (event.target.tagName === 'A' && event.target.href.includes('/reel/')) {
            console.log('Reel link clicked, attempting to unmute and reset');
            setTimeout(() => unmuteVideo(), 1000);
        }
    });
})();