Freesound Shortcuts

adds shortcuts to freesound

  1. // ==UserScript==
  2. // @name Freesound Shortcuts
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.11
  5. // @description adds shortcuts to freesound
  6. // @author Sebastien Andary
  7. // @match https://freesound.org/*
  8. // @grant none
  9. // ==/UserScript==
  10. //debugger;
  11. (function() {
  12. 'use strict';
  13. const ids = soundManager.soundIDs;
  14. const controls = $("a.play");
  15. const nSamples = ids.length;
  16. const SKIP_MILLIS = 3000;
  17. console.log("found " + nSamples + " samples");
  18.  
  19. let current = -1;
  20.  
  21. function toggleCurrent(){
  22. if (current < 0 && current >= nSamples) {
  23. console.log("cannot toggle sample state");
  24. return;
  25. }
  26. var sound = getSound(current);
  27. if (sound.playState === 0) {
  28. sound.play();
  29. } else if (sound.paused) {
  30. sound.resume();
  31. } else {
  32. sound.pause();
  33. }
  34. }
  35. function getSound(i){
  36. return soundManager.getSoundById(ids[i]);
  37. }
  38. function scrollTo(i){
  39. $("html, body").scrollTop($(controls[i]).offset().top - 80);
  40. }
  41. function playNext(count) {
  42. if (current >= 0 && current < nSamples) {
  43. stopSound(current);
  44. }
  45. current += count;
  46. if (current >= nSamples) {
  47. current -= nSamples;
  48. }
  49. if (current < 0) {
  50. current += nSamples;
  51. }
  52. playSound(current);
  53. }
  54. function skipSamples(direction) {
  55. if (current < 0 && current >= nSamples) {
  56. console.log("cannot skip samples");
  57. return;
  58. }
  59. var sound = soundManager.getSoundById(ids[current]);
  60. if (sound.playState === 0){
  61. sound.play();
  62. }
  63. if (sound.paused) {
  64. sound.resume();
  65. }
  66. var pos = sound.position + direction * SKIP_MILLIS;
  67. if (pos < 0) pos = 0;
  68. if (pos > sound.duration) pos = sound.duration - 1;
  69. sound.setPosition(pos);
  70. }
  71. function playSound(i) {
  72. if (i >= nSamples || i < 0) {
  73. console.log("cannot play " + i);
  74. return;
  75. }
  76. console.log("play "+ i);
  77.  
  78. switchOn($(controls[i]));
  79. scrollTo(i);
  80. var sound = soundManager.getSoundById(ids[i]);
  81. sound.stop();
  82. sound.setPosition(0);
  83. sound.play();
  84. }
  85. function stopSound(i) {
  86. if (i >= nSamples || i < 0) {
  87. console.log("cannot stop " + i);
  88. return;
  89. }
  90. console.log("stop "+ i);
  91.  
  92. switchOff($(controls[i]));
  93. var sound = soundManager.getSoundById(ids[i]);
  94. sound.stop();
  95. }
  96. document.addEventListener('keydown', (event) => {
  97. if (/[input|textarea|select]/i.test(event.target.tagName)) {
  98. return false;
  99. }
  100. const keyName = event.key;
  101. const ctrl = event.getModifierState("Control");
  102. //console.log(keyName + " pressed");
  103. if (keyName === "ArrowDown") {
  104. event.preventDefault();
  105. if (ctrl) {
  106. // next page if possible
  107. let link = $('.next-page > a')[0];
  108. if (link) link.click();
  109. } else {
  110. playNext(1);
  111. }
  112. } else if (keyName === "ArrowUp") {
  113. event.preventDefault();
  114. if (ctrl) {
  115. // previous page if possible
  116. let link = $('.previous-page > a')[0];
  117. if (link) link.click();
  118. } else {
  119. playNext(-1);
  120. }
  121. } else if (keyName === ' ') {
  122. event.preventDefault();
  123. toggleCurrent();
  124. } else if (keyName === 'ArrowRight') {
  125. event.preventDefault();
  126. skipSamples(1 * (ctrl ? 10 : 1));
  127. } else if (keyName === 'ArrowLeft') {
  128. event.preventDefault();
  129. skipSamples(-1 * (ctrl ? 10 : 1));
  130. } else if (keyName === "r") {
  131. event.preventDefault();
  132. playNext(0);
  133. }
  134. });
  135. $('.play').live('toggle', function(event) {
  136. current = controls.index(this);
  137. });
  138. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址