Chess.com Game Preview Styling

Applies custom styling to Chess.com home page.

As of 2022-10-31. See the latest version.

  1. // ==UserScript==
  2. // @name Chess.com Game Preview Styling
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Applies custom styling to Chess.com home page.
  6. // @author SaberSpeed77
  7. // @match https://www.chess.com/home
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=chess.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. const imgs = new Map([
  14. /* White Pieces */
  15. ["wp", ""],
  16. ["wn", ""],
  17. ["wb", ""],
  18. ["wr", ""],
  19. ["wq", ""],
  20. ["wk", ""],
  21.  
  22. /* Black Pieces */
  23. ["bp", ""],
  24. ["bn", ""],
  25. ["bb", ""],
  26. ["br", ""],
  27. ["bq", ""],
  28. ["bk", ""],
  29. ]);
  30.  
  31. /* Background */
  32. const background = "";
  33.  
  34. function waitForElement(querySelector, timeout){
  35. return new Promise((resolve, reject)=>{
  36. var timer = false;
  37. if(document.querySelectorAll(querySelector).length) return resolve();
  38. const observer = new MutationObserver(()=>{
  39. if(document.querySelectorAll(querySelector).length){
  40. observer.disconnect();
  41. if(timer !== false) clearTimeout(timer);
  42. return resolve();
  43. }
  44. });
  45. observer.observe(document.body, {
  46. childList: true,
  47. subtree: true
  48. });
  49. if(timeout) timer = setTimeout(()=>{
  50. observer.disconnect();
  51. reject();
  52. }, timeout);
  53. });
  54. }
  55.  
  56. waitForElement("div.game-preview-component.promo-preview", 300).then(function(){
  57. let components = document.querySelectorAll("div.game-preview-component.promo-preview");
  58. if (components) {
  59. for (let i = 0; i < components.length; i++) {
  60. if (background != "")
  61. components[i].style.backgroundImage = "url(" + background + ")";
  62.  
  63. let pieces = components[i].children;
  64. for (let j = 0; j < pieces.length; j++) {
  65. let curPiece = pieces[j];
  66. let key = curPiece.src[curPiece.src.indexOf(".png") - 2] + curPiece.src[curPiece.src.indexOf(".png") - 1];
  67. if (imgs.get(key) != "")
  68. curPiece.src = imgs.get(key);
  69. }
  70. }
  71. }
  72. }).catch(()=>{
  73. console.log("failed");
  74. });