Files
sharechess/scripts/getPieces.js
Maciej Caderek f3069d6dd1 WIP
2022-01-13 06:00:00 +01:00

41 lines
853 B
JavaScript

const getBackgroundUrl = (element, prop) => {
const style = window.getComputedStyle(element);
return style.getPropertyValue(prop).replace('url("', "").replace('")', "");
};
const items = {
kw: "",
qw: "",
rw: "",
bw: "",
nw: "",
pw: "",
kb: "",
qb: "",
rb: "",
bb: "",
nb: "",
pb: "",
};
document.querySelectorAll("cg-board > piece").forEach((item) => {
const color = item.classList.contains("black") ? "b" : "w";
const type = item.classList.contains("pawn")
? "p"
: item.classList.contains("king")
? "k"
: item.classList.contains("queen")
? "q"
: item.classList.contains("bishop")
? "b"
: item.classList.contains("rook")
? "r"
: "n";
const url = getBackgroundUrl(item, "background-image");
items[`${type}${color}`] = url;
});
console.log(JSON.stringify(items, null, 2));