Redd.Tube Respect Mute

Remember if videos should be muted on Redd.Tube

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Redd.Tube Respect Mute
// @description Remember if videos should be muted on Redd.Tube
// @namespace   https://sleazyfork.org/en/users/963844-hibye
// @homepageURL https://sleazyfork.org/en/scripts/452422-redd-tube-respect-mute
// @version     0.7
// @match       https://www.redd.tube/video/*
// @match       https://redd.tube/video/*
// ==/UserScript==

(() => {
    const muteSetting = 'muteVideo';
    const mutedVal = 'mute';
    const unmutedVal = 'unMute';

    const unpauseVideo = function (video) {
        if (video.paused) {
            video.play()
                .catch(err => console.error('Unable to play video', err));
        }
    }

    document.addEventListener('canplay', (evnt) => {
        const video = evnt.target;
        const vidContainsAudio = video.webkitAudioDecodedByteCount > 0 || video.mozHasAudio;

        if (localStorage.getItem(muteSetting) === null) {
            localStorage.setItem(muteSetting, mutedVal);
        }
        const currMuteVal = localStorage.getItem(muteSetting);

        if (vidContainsAudio) {
            if (currMuteVal === unmutedVal && video.muted) {
                video.muted = false;
                unpauseVideo(video);
            }
        }

        video.addEventListener('volumechange', () => {
            const settingVal = video.muted || video.volume === 0 ? mutedVal : unmutedVal;
            localStorage.setItem(muteSetting, settingVal);
        });

    }, { capture: true, once: true });

    document.addEventListener('click', () => {
        const video = document.querySelector('video');

        if (video.paused) {
            unpauseVideo(video);
        }
    }, { once: true });
})();