Auto unmute Instagram stories and keyboard shortcut for fullscreen video

Automatically unmute stories / turn story audio on with fullscreen toggle for video

// ==UserScript==
// @name        Auto unmute Instagram stories and keyboard shortcut for fullscreen video
// @namespace   Violentmonkey Scripts
// @match       https://www.instagram.com/*
// @grant       none
// @version     1.2.1
// @author      chaoscreater
// @description Automatically unmute stories / turn story audio on with fullscreen toggle for video
// ==/UserScript==

var mainLoop = setInterval(() => {
  try {
    var videoElement = document.querySelector('video');
    var audioButton = document.querySelector('button[aria-label="Toggle audio"]');

    if (videoElement && audioButton) {
      if (videoElement.muted && !audioButton.getAttribute('jside_done')) {
        audioButton.click();
        audioButton.setAttribute('jside_done', 'true'); // only run once per muted story
      } else if (!videoElement.muted) {
        audioButton.removeAttribute('jside_done'); // to allow muting again
      }
    }
  } catch (err) {
    // Handle errors, or you can log them to the console for debugging
  }
}, 1000);

// Function to toggle fullscreen on video
function toggleVideoFullscreen() {
  var videoElement = document.querySelector('video');
  if (videoElement) {
    if (!document.fullscreenElement) {
      videoElement.requestFullscreen();
    } else {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      }
    }
  }
}

// Event listener for key press
document.addEventListener('keydown', (event) => {
  if (event.key === 'f') {
    toggleVideoFullscreen();
  }
});