Export from chessgames.com to lichess

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

当前为 2021-12-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Export from chessgames.com to lichess
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Allows a user to quickly export a PGN to lichess for analysis
  6. // @author UncleVinny
  7. // @include https://www.chessgames.com/perl/chessgame?gid=*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. // Note: this script tweaks the original by bfishbaum, found here:
  13. // https://greasyfork.org/en/scripts/389928-lichess-analysis-link-on-chessbase
  14.  
  15. (function() {
  16. 'use strict';
  17. const LICHESS_IMPORT = 'https://lichess.org/api/import';
  18.  
  19. function buildPGNLink() {
  20. var url = new URL(window.location.href);
  21. var gid = url.searchParams.get("gid");
  22. var link = "https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid;
  23. return link;
  24. }
  25. function tidyPGN(inputPGN) {
  26. var newPGN = inputPGN.split("\n");
  27. newPGN = newPGN.join(" ");
  28. return newPGN;
  29. }
  30. function getElementByXpath(path) {
  31. return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  32. }
  33. function placeElement(new_elem, path) {
  34. var path_element = getElementByXpath(path);
  35. path_element.parentNode.insertBefore(new_elem, path_element.nextSibling);
  36. }
  37. function getCleanPgn(callback) {
  38. var url = new URL(window.location.href);
  39. var gid = url.searchParams.get("gid");
  40. var pgnLink = "https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid;
  41. fetch(pgnLink).then(function(response) {
  42. response.text().then(function(body){
  43. var cleanPgn;
  44. cleanPgn = tidyPGN(body);
  45. callback(cleanPgn);
  46. })
  47. })
  48. }
  49. async function submitToLichess(_pgn) {
  50. const params = new URLSearchParams();
  51. params.append("pgn", _pgn);
  52. const options = {
  53. method: 'POST',
  54. headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
  55. body: params.toString()
  56. };
  57. // now post to lichess...
  58. let response = await fetch(LICHESS_IMPORT, options);
  59. if (response.ok) {
  60. let result = await response.json();
  61. var displayLink = document.createElement("a");
  62. displayLink.href = 'https://lichess.org/' + result.id
  63. displayLink.text = "https://lichess.org/" + result.id;
  64. displayLink.style.font = 'bold 12px verdana,arial,helvetica';
  65. displayLink.style.color = '#4D4D4D'; // lichess grey!
  66. placeElement(displayLink, "//html/body/center[2]/input");
  67. console.info(result);
  68. } else {
  69. var errorText = document.createElement('p');
  70. errorText.innerHTML = "Error when uploading to lichess. Please report this at greasyfork, thanks!";
  71. errorText.style.color = '#4D4D4D';
  72. placeElement(errorText, "//html/body/center[2]/input");
  73. console.info(response.status);
  74. }
  75. }
  76. function exportAction() {
  77. getCleanPgn(submitToLichess);
  78. }
  79. function addExportButton() {
  80. var exportButton = document.createElement('input');
  81. exportButton.type = 'button';
  82. exportButton.style = 'display: inline-flex;align-items: center;color: #fff; text-shadow: 1px 1px 1px #000;background-color: #4D4D4D;border-radius: 5px;'
  83. exportButton.value = 'export to lichess';
  84. exportButton.addEventListener('click', exportAction);
  85. placeElement(exportButton, "//html/body/center[2]/div");
  86. }
  87. addExportButton();
  88.  
  89. })();