Freesound Shortcuts

adds shortcuts to freesound

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Freesound Shortcuts
// @namespace    http://tampermonkey.net/
// @version      0.11
// @description  adds shortcuts to freesound
// @author       Sebastien Andary
// @match        https://freesound.org/*
// @grant        none
// ==/UserScript==
//debugger;
(function() {
    'use strict';
    const ids = soundManager.soundIDs;
    const controls = $("a.play");
    const nSamples = ids.length;
    const SKIP_MILLIS = 3000;
    console.log("found " + nSamples + " samples");

    let current = -1;

    function toggleCurrent(){
        if (current < 0 && current >= nSamples) {
            console.log("cannot toggle sample state");
            return;
        }
        var sound = getSound(current);
        if (sound.playState === 0) {
            sound.play();
        } else if (sound.paused) {
            sound.resume();
        } else {
            sound.pause();
        }
    }
    function getSound(i){
        return soundManager.getSoundById(ids[i]);
    }
    function scrollTo(i){
        $("html, body").scrollTop($(controls[i]).offset().top - 80);
    }
    function playNext(count) {
        if (current >= 0 && current < nSamples) {
            stopSound(current);
        }
        current += count;
        if (current >= nSamples) {
            current -= nSamples;
        }
        if (current < 0) {
            current += nSamples;
        }
        playSound(current);
    }
    function skipSamples(direction) {
        if (current < 0 && current >= nSamples) {
            console.log("cannot skip samples");
            return;
        }
        var sound = soundManager.getSoundById(ids[current]);
        if (sound.playState === 0){
            sound.play();
        }
        if (sound.paused) {
            sound.resume();
        }
        var pos = sound.position + direction * SKIP_MILLIS;
        if (pos < 0) pos = 0;
        if (pos > sound.duration) pos = sound.duration - 1;
        sound.setPosition(pos);
    }
    function playSound(i) {
        if (i >= nSamples || i < 0) {
            console.log("cannot play " + i);
            return;
        }
        console.log("play "+ i);

        switchOn($(controls[i]));
        scrollTo(i);
        var sound = soundManager.getSoundById(ids[i]);
        sound.stop();
        sound.setPosition(0);
        sound.play();
    }
    function stopSound(i) {
        if (i >= nSamples || i < 0) {
            console.log("cannot stop " + i);
            return;
        }
        console.log("stop "+ i);

        switchOff($(controls[i]));
        var sound = soundManager.getSoundById(ids[i]);
        sound.stop();
    }
    document.addEventListener('keydown', (event) => {
        if (/[input|textarea|select]/i.test(event.target.tagName)) {
            return false;
        }
        const keyName = event.key;
        const ctrl = event.getModifierState("Control");
        //console.log(keyName + " pressed");
        if (keyName === "ArrowDown") {
            event.preventDefault();
            if (ctrl) {
                // next page if possible
                let link = $('.next-page > a')[0];
                if (link) link.click();
            } else {
                playNext(1);
            }
        } else if (keyName === "ArrowUp") {
            event.preventDefault();
            if (ctrl) {
                // previous page if possible
                let link = $('.previous-page > a')[0];
                if (link) link.click();
            } else {
                playNext(-1);
            }
        } else if (keyName === ' ') {
            event.preventDefault();
            toggleCurrent();
        } else if (keyName === 'ArrowRight') {
            event.preventDefault();
            skipSamples(1 * (ctrl ? 10 : 1));
        } else if (keyName === 'ArrowLeft') {
            event.preventDefault();
            skipSamples(-1 * (ctrl ? 10 : 1));
        } else if (keyName === "r") {
            event.preventDefault();
            playNext(0);
        }
    });
    $('.play').live('toggle', function(event) {
        current = controls.index(this);
    });
})();