Greasy Fork is available in English.

Instagram Auto Unmute Videos and Stories

Automatically unmutes Instagram videos and stories once per session or event.

  1. // ==UserScript==
  2. // @name Instagram Auto Unmute Videos and Stories
  3. // @namespace your_namespace
  4. // @match https://www.instagram.com/*
  5. // @grant none
  6. // @version 1.0
  7. // @author UniverDev
  8. // @license GPL-3.0-or-later
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=instagram.com
  10. // @description Automatically unmutes Instagram videos and stories once per session or event.
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. let videoUnmuted = false;
  17. let storyUnmuted = false;
  18.  
  19. function clickUnmuteButton(selector, flagSetter) {
  20. console.log(`Looking for button with selector: ${selector}`);
  21. const button = document.querySelector(selector);
  22. if (button) {
  23. console.log(`Found button. Clicking to unmute.`);
  24. button.click();
  25. flagSetter(true);
  26. } else {
  27. console.log(`Button not found for selector: ${selector}`);
  28. }
  29. }
  30.  
  31. const handleVideoPlay = (event) => {
  32. if (event.target.tagName === 'VIDEO' && !videoUnmuted) {
  33. console.log('Video play event detected. Checking unmute status.');
  34. clickUnmuteButton('button[aria-label="Toggle audio"]', (value) => {
  35. videoUnmuted = value;
  36. console.log(`Video unmuted: ${videoUnmuted}`);
  37. });
  38. monitorMuteStateOnce(event.target, 'button[aria-label="Toggle audio"]', () => {
  39. videoUnmuted = true;
  40. console.log('Video unmuted after monitoring mute state.');
  41. });
  42. }
  43. };
  44.  
  45. const handleStoryPlay = (event) => {
  46. if (event.target.tagName === 'VIDEO' && !storyUnmuted) {
  47. console.log('Story play event detected. Checking unmute status.');
  48. clickUnmuteButton('div[aria-label="Toggle audio"][role="button"]', (value) => {
  49. storyUnmuted = value;
  50. console.log(`Story unmuted: ${storyUnmuted}`);
  51. });
  52. monitorMuteStateOnce(event.target, 'div[aria-label="Toggle audio"][role="button"]', () => {
  53. storyUnmuted = true;
  54. console.log('Story unmuted after monitoring mute state.');
  55. });
  56. }
  57. };
  58.  
  59. const monitorMuteStateOnce = (videoElement, selector, callback) => {
  60. const onVolumeChange = () => {
  61. if (videoElement.muted) {
  62. console.log('Video is muted. Attempting to unmute.');
  63. clickUnmuteButton(selector, () => {
  64. callback();
  65. });
  66. } else {
  67. console.log('Video is not muted. Removing mute state listener.');
  68. videoElement.removeEventListener('volumechange', onVolumeChange);
  69. }
  70. };
  71. videoElement.addEventListener('volumechange', onVolumeChange);
  72. };
  73.  
  74. const resetUnmuteFlags = () => {
  75. console.log('Resetting unmute flags.');
  76. videoUnmuted = false;
  77. storyUnmuted = false;
  78. };
  79.  
  80. const monitorSplashScreen = () => {
  81. console.log('Monitoring splash screen for visibility changes.');
  82. const splashScreen = document.querySelector('[data-testid="splashScreen"]');
  83. if (splashScreen) {
  84. splashScreen.addEventListener('transitionend', () => {
  85. if (window.getComputedStyle(splashScreen).display !== 'none') {
  86. console.log('Splash screen visible. Resetting unmute flags.');
  87. resetUnmuteFlags();
  88. }
  89. });
  90. } else {
  91. console.log('Splash screen not found.');
  92. }
  93. };
  94.  
  95. const initialize = () => {
  96. console.log('Initializing script.');
  97. document.addEventListener('play', (event) => {
  98. if (event.target.tagName === 'VIDEO') {
  99. handleVideoPlay(event);
  100. handleStoryPlay(event);
  101. }
  102. }, true);
  103.  
  104. monitorSplashScreen();
  105. };
  106.  
  107. window.addEventListener('load', initialize);
  108. })();