Admute

Mute/unmute based on ad presence.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        Admute
// @namespace   https://greasyfork.org/en/users/1441726-d155
// @match       *://*spotify.com/*
// @grant       none
// @version     2.0
// @author      d155
// @description Mute/unmute based on ad presence.
// @license GNU GPLv3
// ==/UserScript==

(function () {
    'use strict';

    let muteButton = null;
    let adDetected = false;

    function findMuteButton() {
        const button = document.querySelector('[data-testid="volume-bar-toggle-mute-button"]');
        if (button) {
            muteButton = button;
        }
    }

    function isAd() {
        return document.querySelector('[aria-label="Advertisement"]') !== null;
    }

    function update() {
        if (!muteButton || !document.body.contains(muteButton)) {
            findMuteButton();
        }

        const adIsPlaying = isAd();

        if (adIsPlaying && !adDetected) {
            adDetected = true;
            if (muteButton) muteButton.click();
        }

        if (!adIsPlaying && adDetected) {
            adDetected = false;
            if (muteButton) muteButton.click();
        }
    }

    setInterval(update, 500);
})();