Export from chessgames.com to lichess

Allows a user to quickly export a PGN to lichess for analysis

目前為 2021-12-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Export from chessgames.com to lichess
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Allows a user to quickly export a PGN to lichess for analysis
// @author       UncleVinny
// @include      https://www.chessgames.com/perl/chessgame?gid=*
// @grant        none
// @license      MIT
// ==/UserScript==

// Note: this script tweaks the original by bfishbaum, found here:
// https://greasyfork.org/en/scripts/389928-lichess-analysis-link-on-chessbase

(function() {
    'use strict';
    const LICHESS_IMPORT = 'https://lichess.org/api/import';

    function buildPGNLink() {
        var url = new URL(window.location.href);
        var gid = url.searchParams.get("gid");
        var link = "https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid;
        return link;
    }  
  
    function tidyPGN(inputPGN) {
      var newPGN = inputPGN.split("\n");
      newPGN = newPGN.join(" ");
      return newPGN;
    }
  
    function getElementByXpath(path) {
      return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
  
    function placeElement(new_elem, path) {
      var path_element = getElementByXpath(path);
      path_element.parentNode.insertBefore(new_elem, path_element.nextSibling);
    }
  
    function getCleanPgn(callback) {
      var url = new URL(window.location.href);
      var gid = url.searchParams.get("gid");
      var pgnLink = "https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid;
      fetch(pgnLink).then(function(response) {
        response.text().then(function(body){
          var cleanPgn;
          cleanPgn = tidyPGN(body);
          callback(cleanPgn);
        })
      }) 
    }
  
    async function submitToLichess(_pgn) {
      const params = new URLSearchParams();
      params.append("pgn", _pgn);
      const options = {
        method: 'POST',   
        headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
        body: params.toString()
      };
      
      // now post to lichess...
      let response = await fetch(LICHESS_IMPORT, options);
      
      if (response.ok) {
        let result = await response.json();
        var displayLink = document.createElement("a");
        displayLink.href = 'https://lichess.org/' + result.id
        displayLink.text = "https://lichess.org/" + result.id;
        displayLink.style.font = 'bold 12px verdana,arial,helvetica';
        displayLink.style.color = '#4D4D4D'; // lichess grey!
        placeElement(displayLink, "//html/body/center[2]/input");
        console.info(result);        
      } else {
        var errorText = document.createElement('p');
        errorText.innerHTML = "Error when uploading to lichess. Please report this at greasyfork, thanks!";
        errorText.style.color = '#4D4D4D';
        placeElement(errorText, "//html/body/center[2]/input");
        console.info(response.status);
      }
    }
                                                
    function exportAction() {
      getCleanPgn(submitToLichess);
    }
  
    function addExportButton() {
      var exportButton = document.createElement('input');
      exportButton.type = 'button';
      exportButton.style = 'display: inline-flex;align-items: center;color: #fff; text-shadow: 1px 1px 1px #000;background-color: #4D4D4D;border-radius: 5px;'
      exportButton.value = 'export to lichess';
      exportButton.addEventListener('click', exportAction);
      placeElement(exportButton, "//html/body/center[2]/div");
    }
  
    addExportButton();

})();