Chess.com Game Preview Styling

Applies custom styling to Chess.com home page.

Verze ze dne 02. 11. 2022. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         Chess.com Game Preview Styling
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Applies custom styling to Chess.com home page.
// @author       SaberSpeed77
// @match        https://www.chess.com/home
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chess.com
// @grant        none
// @license      MIT
// ==/UserScript==

const imgs = new Map([
  /* White Pieces */
  ["wp", "https://im3.ezgif.com/tmp/ezgif-3-bf69976b7f.png"],
  ["wn", "https://im3.ezgif.com/tmp/ezgif-3-366197dbd0.png"],
  ["wb", "https://im3.ezgif.com/tmp/ezgif-3-2d9aec71f3.png"],
  ["wr", "https://im3.ezgif.com/tmp/ezgif-3-7ef27a11c1.png"],
  ["wq", "https://im3.ezgif.com/tmp/ezgif-3-3f3b16b74c.png"],
  ["wk", "https://im3.ezgif.com/tmp/ezgif-3-7a3048dd0b.png"],

  /* Black Pieces */
  ["bp", "https://im3.ezgif.com/tmp/ezgif-3-ae6cd300a4.png"],
  ["bn", "https://im3.ezgif.com/tmp/ezgif-3-1b252a3850.png"],
  ["bb", "https://im3.ezgif.com/tmp/ezgif-3-46f763639b.png"],
  ["br", "https://im3.ezgif.com/tmp/ezgif-3-fa604afa13.png"],
  ["bq", "https://im3.ezgif.com/tmp/ezgif-3-8462cd6e12.png"],
  ["bk", "https://im3.ezgif.com/tmp/ezgif-3-4b6347fcc7.png"],
]);

/* Background */
const background = "https://i.imgur.com/jKU9y9W.png";

function waitForElement(querySelector, timeout){
  return new Promise((resolve, reject)=>{
    var timer = false;
    if(document.querySelectorAll(querySelector).length) return resolve();
    const observer = new MutationObserver(()=>{
      if(document.querySelectorAll(querySelector).length){
        observer.disconnect();
        if(timer !== false) clearTimeout(timer);
        return resolve();
      }
    });
    observer.observe(document.body, {
      childList: true,
      subtree: true
    });
    if (timeout) timer = setTimeout(()=>{
      observer.disconnect();
      reject();
    }, timeout);
  });
}

waitForElement("div.game-preview-component.promo-preview", 300).then(function(){
    let components = document.querySelectorAll("div.game-preview-component.promo-preview");
    if (components) {
        var t = setInterval(() => {
                for (let i = 0; i < components.length; i++) {
                    if (background != "")
                        components[i].style.backgroundImage = "url(" + background + ")";

                    let pieces = components[i].children;
                    for (let j = 0; j < pieces.length; j++) {
                        let curPiece = pieces[j];
                        let key = curPiece.src[curPiece.src.indexOf(".png") - 2] + curPiece.src[curPiece.src.indexOf(".png") - 1];
                        if (imgs.get(key) != "")
                            curPiece.src = imgs.get(key);
                    }
                }
            clearInterval(t);
        }, 300);

    }
}).catch(()=>{
   console.log("failed");
});