Lichess Chesscom sound pack

chesscom sound pack for lichess.

  1. // ==UserScript==
  2. // @name Lichess Chesscom sound pack
  3. // @namespace mickael_r
  4. // @description chesscom sound pack for lichess.
  5. // @include https://*.lichess.org/*
  6. // @include https://lichess.org/*
  7. // @version 1.1
  8. // @grant GM_xmlhttpRequest
  9. // @connect cdn.discordapp.com
  10. // ==/UserScript==
  11.  
  12. // this function makes the request and puts it in an decoded audio buffer
  13. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  14. var context = new AudioContext();
  15. function loadSound(url) {
  16. return new Promise(function(resolve, reject) {
  17. // This will get around the CORS issue
  18. // http://wiki.greasespot.net/GM_xmlhttpRequest
  19. var req = GM_xmlhttpRequest({
  20. method: "GET",
  21. url: url,
  22. responseType: 'arraybuffer',
  23. onload: function(response) {
  24. try {
  25. context.decodeAudioData(response.response, function(buffer) {
  26. resolve(buffer)
  27. }, function(e) {
  28. reject(e);
  29. });
  30. }
  31. catch(e) {
  32. reject(e);
  33. }
  34. }
  35. });
  36. })
  37. }
  38.  
  39. // adjust volume
  40. var volNode;
  41. if( context.createGain instanceof Function ) {
  42. volNode = context.createGain();
  43. } else if( context.createGainNode instanceof Function ) {
  44. volNode = context.createGainNode();
  45. }
  46.  
  47. // Connect the volume control the the speaker
  48. volNode.connect( context.destination );
  49.  
  50.  
  51. // allocate buffers for sounds
  52. var customSndList = new Map([
  53. ['move','https://cdn.discordapp.com/attachments/877926182715289634/878309282653810718/move-self_3.mp3'],
  54. ['capture','https://cdn.discordapp.com/attachments/877926182715289634/878309669259608064/capture.mp3'],
  55. ['check','https://cdn.discordapp.com/attachments/877926182715289634/878310089109438536/move-check.mp3'],
  56. ['victory','https://cdn.discordapp.com/attachments/877926182715289634/878317417560952862/game-win.mp3'],
  57. ['defeat','https://cdn.discordapp.com/attachments/877926182715289634/878317292138688522/game-end.mp3'],
  58. ['draw','https://cdn.discordapp.com/attachments/877926182715289634/878317326661992538/game-draw.mp3'],
  59. ['genericNotify','https://cdn.discordapp.com/attachments/877926182715289634/878311365884907520/dong.mp3'],
  60. ['lowTime','https://cdn.discordapp.com/attachments/877926182715289634/878311630465806366/lowtime.mp3'],
  61. ['castle','https://cdn.discordapp.com/attachments/877926182715289634/878312026559098960/castle.mp3'],
  62. ])
  63. var customSnds = {};
  64. customSndList.forEach(function(element, index) {
  65. loadSound(element).then(function(buffer) {customSnds[index] = buffer;}, function(e) {console.log(e);})
  66. });
  67.  
  68. // use this later in the script
  69. function playSound(buffer, volume) {
  70. console.log('PS1');
  71. // creates a sound source
  72. var source = context.createBufferSource();
  73. // tell the source which sound to play
  74. source.buffer = buffer;
  75. // connect the source to the context's destination (the speakers)
  76. volNode.gain.value = volume;
  77. source.connect(volNode);
  78. // play the source now
  79. // note: on older systems, may have to use deprecated noteOn(time);
  80. source.start(0);
  81. }
  82.  
  83. lichess.sound.origPlay = lichess.sound.play;
  84.  
  85. function customPlay(name, volume) {
  86. console.log(name);
  87. if (customSnds[name]) {
  88. if (!volume) volume = lichess.sound.getVolume();
  89. if (name != 'check'){playSound(customSnds[name], volume);}
  90. if (name == 'check'){setTimeout(function(){playSound(customSnds[name], volume)}, 80); console.log('called');}
  91. //playSound(customSnds[name], volume);
  92. } else {
  93. lichess.sound.origPlay(name, volume);
  94. }
  95. }
  96.  
  97. lichess.sound.play = customPlay;