Advertisement
doxorn

Tampermonkey - Auto Unmute and Reset Facebook Reels and Videos

Jun 3rd, 2025
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.62 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name         Auto Unmute and Reset Facebook Reels and Videos
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.4
  5. // @description  Automatically unmutes Facebook Reels and videos on desktop and resets playback to start
  6. // @author       Grok
  7. // @match        https://www.facebook.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     // Function to unmute video and reset playback to start
  15.     function unmuteVideo(retries = 5) {
  16.         const unmuteButton = document.querySelector('div[aria-label="Unmute"][role="button"]');
  17.         if (unmuteButton) {
  18.             unmuteButton.click();
  19.             console.log('Unmute button clicked successfully');
  20.  
  21.             // Find the closest video element
  22.             const video = unmuteButton.closest('div').querySelector('video') || document.querySelector('video');
  23.             if (video) {
  24.                 video.currentTime = 0;
  25.                 console.log('Video playback reset to start');
  26.             } else {
  27.                 console.log('Video element not found for reset');
  28.             }
  29.             return;
  30.         }
  31.  
  32.         // Fallback: directly unmute any muted video elements and reset
  33.         const videos = document.querySelectorAll('video');
  34.         videos.forEach(video => {
  35.             if (video.muted) {W
  36.                 video.muted = false;
  37.                 video.currentTime = 0;
  38.                 console.log('Video directly unmuted and reset to start');
  39.             }
  40.         });
  41.  
  42.         // Retry if button not found and retries remain
  43.         if (retries > 0) {
  44.             console.log(`Unmute button not found, retrying (${retries} attempts left)`);
  45.             setTimeout(() => unmuteVideo(retries - 1), 500);
  46.         } else {
  47.             console.log('Unmute button not found after all retries');
  48.         }
  49.     }
  50.  
  51.     // MutationObserver to detect when the unmute button is added
  52.     const observer = new MutationObserver((mutations) => {
  53.         mutations.forEach((mutation) => {
  54.             if (mutation.type === 'childList') {
  55.                 mutation.addedNodes.forEach((node) => {
  56.                     if (node.nodeType === 1 && node.matches('div[aria-label="Unmute"][role="button"]')) {
  57.                         console.log('Unmute button detected in DOM');
  58.                         setTimeout(() => {
  59.                             node.click();
  60.                             console.log('Unmute button clicked via observer');
  61.                             const video = node.closest('div').querySelector('video') || document.querySelector('video');
  62.                             if (video) {
  63.                                 video.currentTime = 0;
  64.                                 console.log('Video playback reset to start via observer');
  65.                             } else {
  66.                                 console.log('Video element not found for reset via observer');
  67.                             }
  68.                         }, 100);
  69.                     }
  70.                 });
  71.             }
  72.         });
  73.     });
  74.  
  75.     // Start observing the document for changes
  76.     observer.observe(document.body, {
  77.         childList: true,
  78.         subtree: true
  79.     });
  80.  
  81.     // Try unmuting initially (no delay, as per your working version)
  82.     setTimeout(() => unmuteVideo(), 0);
  83.  
  84.     // Listen for clicks on Reel links
  85.     document.addEventListener('click', (event) => {
  86.         if (event.target.tagName === 'A' && event.target.href.includes('/reel/')) {
  87.             console.log('Reel link clicked, attempting to unmute and reset');
  88.             setTimeout(() => unmuteVideo(), 100);
  89.         }
  90.     });
  91. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement