Faircamp Playback Toggle

Use spacebar to toggle music playback in Faircamp based websites

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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         Faircamp Playback Toggle
// @namespace    https://openuserjs.org/users/burn
// @version      2025-07-22
// @description  Use spacebar to toggle music playback in Faircamp based websites
// @author       Burn
// @include      *://*
// @grant        none
// @license      MIT
// @copyright    2025, burn (https://openuserjs.org/users/burn)
// ==/UserScript==

(function() {
    'use strict';
    const DBG = false;

    let storeName = GM_info.script.name,
        myLog = (msg) => {
            DBG && console.log(storeName + " | " + msg);
        },
        qS = (el, scope) => {
            scope = (scope instanceof HTMLElement) ? scope : document;
            return scope.querySelector(el) || false;
        },
        qSAll = (els, scope) => {
            scope = (scope instanceof HTMLElement) ? scope : document;
            return scope.querySelectorAll(els) || false;
        },
        hidden, visibilityChange, state,
        tabFocused = (evt) => {
            myLog("tab has focus!");
            if (document !== evt.target) {
                myLog("warning, document not equal to document. it is ");
                myLog(evt.target); // should be document
            }
            if (evt.target.body !== document.activeElement) {
                myLog("warning, document.activeElement not equal to document.body. it is ");
                myLog(evt.target.body); // should be document.body
            }
        },
        elmTarget = qS("#content > div.docked_player > div.elements > button.playback");
    if (!elmTarget) {
        myLog('track play button not found, exiting');
        return;
    }

    if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
        hidden = "hidden";
        visibilityChange = "visibilitychange";
        state = "visibilityState";
    } else if (typeof document.mozHidden !== "undefined") {
        hidden = "mozHidden";
        visibilityChange = "mozvisibilitychange";
        state = "mozVisibilityState";
    } else if (typeof document.msHidden !== "undefined") {
        hidden = "msHidden";
        visibilityChange = "msvisibilitychange";
        state = "msVisibilityState";
    } else if (typeof document.webkitHidden !== "undefined") {
        hidden = "webkitHidden";
        visibilityChange = "webkitvisibilitychange";
        state = "webkitVisibilityState";
    }

    if ('undefined' === typeof hidden) {
        myLog('document.hidden not found, exiting');
        return;
    }

    document.addEventListener(visibilityChange, (e) => {
        return (false === document[hidden]) && tabFocused(e);
    });
    window.addEventListener('keydown', (e) => {
        myLog("in keydown");
        if(e.key === " " && e.target === document.body) {
            myLog("keydown ok");
            e.preventDefault();
        }
    });
    qS('body').addEventListener("keyup", (e) => {
        myLog("in keyup");
        if (e.key === " " && e.target === document.body) {
            e.preventDefault();
            elmTarget.focus();
            elmTarget.click();
            elmTarget.blur();
            myLog("keyup ok");
        }
    });
})();