Lichess ChessComPack

Sound Replace

  1. // ==UserScript==
  2. // @name Lichess ChessComPack
  3. // @namespace http://example.com
  4. // @description Sound Replace
  5. // @include http://*.lichess.org/*
  6. // @version 2.2
  7. // @grant none
  8. // ==/UserScript==
  9. $.sound = (function() {
  10. var Move = new Audio();
  11. Move.src = 'http://images.chesscomfiles.com/chess-themes/sounds/_MP3_/default/move-self.mp3';
  12. Move.volume = 0.5;
  13. var Take = new Audio();
  14. Take.src = 'http://images.chesscomfiles.com/chess-themes/sounds/_MP3_/default/capture.mp3';
  15. Take.volume = 0.5;
  16. var Dong = new Audio();
  17. Dong.src = 'http://images.chesscomfiles.com/chess-themes/sounds/_MP3_/default/notify.mp3';
  18. Dong.volume = 0.5;
  19. var Lowtime = new Audio();
  20. Lowtime.src = 'http://ralphschuler.ch/ChessCubeSoundPack/Chesscube_gamestart.wav';
  21. Lowtime.volume = 0.5;
  22. var baseUrl = $('body').data('sound-dir') + '/';
  23. Howler.volume(lichess.storage.get('sound-volume') || 0.1);
  24. var audio = {
  25. dong: 'dong',
  26. moveW: 'move',
  27. moveB: 'move',
  28. take: 'take',
  29. lowtime: 'lowtime'
  30. };
  31. var volumes = {
  32. lowtime: 0.05
  33. };
  34. var get = new $.lazy(function(k) {
  35. return new Howl({
  36. src: ['ogg', 'mp3'].map(function(ext) {
  37. return baseUrl + audio[k] + '.' + ext;
  38. }),
  39. volume: volumes[k] || 0.1
  40. });
  41. });
  42. var $control = $('#sound_control');
  43. var $toggle = $('#sound_state');
  44. var enabled = function() {
  45. return !!lichess.storage.get('sound');
  46. };
  47. $control.add($toggle).toggleClass('sound_state_on', enabled());
  48. var play = {
  49. move: function(white) {
  50. if (enabled()) {
  51. if (white) Move.play();
  52. else Move.play();
  53. }
  54. },
  55. take: function() {
  56. if (enabled()) Take.play();
  57. },
  58. dong: function() {
  59. if (enabled()) Dong.play();
  60. },
  61. lowtime: function() {
  62. if (enabled()) Lowtime.play();
  63. }
  64. };
  65. var setVolume = function(v) {
  66. lichess.storage.set('sound-volume', v);
  67. Howler.volume(v);
  68. };
  69. var manuallySetVolume = $.fp.debounce(function(v) {
  70. setVolume(v);
  71. play.move(true);
  72. }, 50);
  73. $toggle.click(function() {
  74. var enab = !enabled();
  75. if (enab) lichess.storage.set('sound', 1);
  76. else lichess.storage.remove('sound');
  77. $control.add($toggle).toggleClass('sound_state_on', enab);
  78. play.dong();
  79. return false;
  80. });
  81. $toggle.one('mouseover', function() {
  82. $toggle.parent().find('.slider').slider({
  83. orientation: "vertical",
  84. min: 0,
  85. max: 1,
  86. range: 'min',
  87. step: 0.01,
  88. value: Howler.volume(),
  89. slide: function(e, ui) {
  90. manuallySetVolume(ui.value);
  91. }
  92. });
  93. });
  94. return play;
  95. })();