projang / 리체스 Embed 공유 버튼 개선

// ==UserScript==
// @name         리체스 Embed 공유 버튼 개선
// @version      1.2
// @description  리체스의 Embed 공유를 편하게하는 버튼과 익명 공유 버튼을 추가합니다.
// @match        https://lichess.org/*
// @author       projang
// @license MIT
// @copyright 2021, projang (https://openuserjs.org/users/projang)
// @updateURL https://openuserjs.org/meta/projang/리체스_Embed_공유_버튼_개선.meta.js
// ==/UserScript==

const url = window.location.href;
const pgnMenuElement = document.querySelector("div.pgn-options > div");
if (!pgnMenuElement) throw new Error("pgn 정보가 없습니다!");

pgnMenuElement.insertAdjacentHTML(
    "beforeend",
    `<a data-icon="" class="text" id="copyembed">Embed 복사하기</a>`
);
//pgnMenuElement.insertAdjacentHTML(
//    "beforeend",
//    `<a data-icon="" class="text" id="copyanonembed">익명으로 Embed 복사하기</a>`
//);
pgnMenuElement.insertAdjacentHTML(
    "beforeend",
    `<a data-icon="" class="text" id="anongif">익명으로 Gif 열기</a>`
);

setTimeout(() => {
    document.querySelector("#copyembed").onclick = () => {
        const text = `<iframe src="${url.replace(
            "org/",
            "org/embed/"
        )}?theme=auto&bg=auto" width=600 height=397 frameborder=0></iframe>`;
        copyToClipboard(text);
    };
    //document.querySelector("#copyanonembed").onclick = copyAnon;
    document.querySelector("#anongif").onclick = openAnonGif;
}, 100);

async function openAnonGif(){
    const flipped = url.includes("black");
    const id= await getAnonGameId()
    const result=`https://lichess1.org/game/export/gif/${flipped?'black':'white'}/${id}.gif`
    window.open(result)
}
async function copyAnon() {
    const flipped = url.includes("black");
    const id=await getAnonGameId()
    const text = `<iframe src="https://lichess.org/embed/${id}${
      flipped ? "/black" : ""
    }?theme=auto&bg=auto" width=600 height=397 frameborder=0></iframe>`;
    console.log(text)
    copyToClipboard(text);
}

async function getAnonGameId(id){
    let pgn = document.querySelector(".pgn")?.textContent;
    if (!document.querySelector(".ceval") || !pgn) return;
    pgn = editPgnData(pgn, "White", "Anon");
    pgn = editPgnData(pgn, "Black", "Anon");
    pgn = editPgnData(pgn, "WhiteElo", "");
    pgn = editPgnData(pgn, "BlackElo", "");
    pgn = editPgnData(pgn, "Site", "");
    return await getGameId(pgn)
}

function editPgnData(pgn, key, newValue) {
    const i = pgn.indexOf(key) + key.length + 2;
    return pgn.slice(0, i) + newValue + pgn.slice(pgn.indexOf('"', i));
}

async function getGameId(pgn) {
    pgn = encodeURIComponent(pgn);
    return await fetch("https://lichess.org/import", {
        "headers": {
            "content-type": "application/x-www-form-urlencoded",
        },
        "body": `pgn=${pgn}`,
        "method": "POST",
    })
        .then((res) => res.url)
        .then((location) => location.split("/").pop());
}

function copyToClipboard(text) {
    var area = document.createElement("textarea");
    area.value = text;
    document.body.appendChild(area);

    area.select();
    document.execCommand("copy");
    document.body.removeChild(area);
}