Faircamp Playback Toggle

Use spacebar to toggle music playback in Faircamp based websites

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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");
        }
    });
})();