Lichess Chesscom sound pack

chesscom sound pack for lichess.

当前为 2021-08-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Lichess Chesscom sound pack
  3. // @namespace jwapobie
  4. // @description chesscom sound pack for lichess.
  5. // @include https://*.lichess.org/*
  6. // @include https://lichess.org/*
  7. // @version 1.0
  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. // creates a sound source
  71. var source = context.createBufferSource();
  72. // tell the source which sound to play
  73. source.buffer = buffer;
  74. // connect the source to the context's destination (the speakers)
  75. volNode.gain.value = volume;
  76. source.connect(volNode);
  77. // play the source now
  78. // note: on older systems, may have to use deprecated noteOn(time);
  79. source.start(0);
  80. }
  81.  
  82. lichess.sound.origPlay = lichess.sound.play;
  83.  
  84. function customPlay(name, volume) {
  85. if (customSnds[name]) {
  86. if (!volume) volume = lichess.sound.getVolume();
  87. playSound(customSnds[name], volume);
  88. } else {
  89. lichess.sound.origPlay(name, volume);
  90. }
  91. }
  92.  
  93. lichess.sound.play = customPlay;