Lichess Analysis Link on Chessbase

Adds a link to a lichess analysis of the game on chessbase games

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Lichess Analysis Link on Chessbase
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a link to a lichess analysis of the game on chessbase games
// @author       You
// @include      http://www.chessgames.com/perl/chessgame?gid=*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const LICHESS_ANALYSIS = "https://lichess.org/paste"

    function buildPGNLink() {
        var url = new URL(window.location.href)
        var gid = url.searchParams.get("gid")
        var link = "http://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid
        return link
    }

    function buildLichessLink(callback) {
        var pgnLink = buildPGNLink()
        fetch(pgnLink).then(function(response) {
            response.text().then(function(body){
                var lichessLink = document.createElement("a")
                lichessLink.href = LICHESS_ANALYSIS + "?pgn="+cleanPGN(body)
                lichessLink.target = "_blank"
                lichessLink.text = "LI"
                callback(lichessLink)
            });
        });
    }

    function cleanPGN(pgn) {
        console.log(pgn)
        var newPGN = pgn.split("\n")
        newPGN = newPGN.join(" ")
        console.log(newPGN)
        return newPGN
    }

    function addLichessLink(link) {
        var tds = document.getElementsByTagName("td")
        var pgnTD = tds[15]
        pgnTD.firstChild.appendChild(document.createTextNode(" | "))
        pgnTD.firstChild.appendChild(link)
    }

    buildLichessLink(addLichessLink)

})();