Export from chessgames.com to lichess

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

当前为 2022-01-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Export from chessgames.com to lichess
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  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 GM_addStyle
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. /* jshint esversion: 6 */
  13.  
  14. // Note: this script tweaks the original by bfishbaum, found here:
  15. // https://greasyfork.org/en/scripts/389928-lichess-analysis-link-on-chessbase
  16.  
  17. GM_addStyle (`
  18. #exportDiv {
  19. display: inline-block;
  20. width: 600px;
  21. margin-bottom: 4px; /* This adds a white buffer below the div */
  22. padding: 4px; /* This makes the background grey a little taller. */
  23. background: #9E9E9E;
  24. font-family: Arial, Helvetica, sans-serif;
  25. font-weight: bold;
  26. font-size: 12px;
  27. }
  28.  
  29. #exportButton {
  30. background-color: #4D4D4D; /* lichess grey */
  31. border: none;
  32. margin: 0px 4px; /* distance to element on the right */
  33. color: white;
  34. padding: 3px 5px; /* padding around the text */
  35. text-align: center; /* alignment within the button */
  36. font-weight: bold; /* for some reason this doesn't get picked up from the div */
  37. border-radius: 5px;
  38. display: inline-block;
  39. }
  40.  
  41. #urlLabel {
  42. display: inline-block;
  43. margin: 0px 10px;
  44. color: #4D4D4D;
  45. text-align: left;
  46. }
  47.  
  48. #lichessURL {
  49. display: inline-block;
  50. color: #110011;
  51. text-align: left;
  52. }
  53.  
  54. #error {
  55. margin: 0px 10px;
  56. color: #dd1111;
  57. }
  58.  
  59. `);
  60.  
  61. (function() {
  62. 'use strict';
  63. const LICHESS_IMPORT = 'https://lichess.org/api/import';
  64.  
  65. function buildPGNLink() {
  66. var url = new URL(window.location.href);
  67. var gid = url.searchParams.get("gid");
  68. var link = "https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid;
  69. return link;
  70. }
  71. function tidyPGN(inputPGN) {
  72. var newPGN = inputPGN.split("\n");
  73. newPGN = newPGN.join(" ");
  74. return newPGN;
  75. }
  76. function getElementByXpath(path) {
  77. return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  78. }
  79. function placeElement(new_elem, path) {
  80. var path_element = getElementByXpath(path);
  81. path_element.parentNode.insertBefore(new_elem, path_element.nextSibling);
  82. }
  83. function getCleanPgn(callback) {
  84. var url = new URL(window.location.href);
  85. var gid = url.searchParams.get("gid");
  86. var pgnLink = "https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid;
  87. fetch(pgnLink).then(function(response) {
  88. response.text().then(function(body){
  89. var cleanPgn;
  90. cleanPgn = tidyPGN(body);
  91. callback(cleanPgn);
  92. })
  93. })
  94. }
  95. async function submitToLichess(_pgn) {
  96. const params = new URLSearchParams();
  97. params.append("pgn", _pgn);
  98. const options = {
  99. method: 'POST',
  100. headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
  101. body: params.toString()
  102. };
  103. // now post to lichess...
  104. let response = await fetch(LICHESS_IMPORT, options);
  105. if (response.ok) {
  106. let result = await response.json();
  107. var displayLink = document.getElementById("lichessURL");
  108. displayLink.textContent = 'http://lichess.org/' + result.id;
  109. displayLink.hidden = false;
  110. displayLink.href = 'http://lichess.org/' + result.id;
  111. var urlLabel = document.getElementById("urlLabel");
  112. urlLabel.hidden = false;
  113. console.info(result);
  114. } else {
  115. var errorText = document.createElement('a');
  116. errorText.id = 'error';
  117. errorText.href = 'https://greasyfork.org/en/scripts/435720-export-from-chessgames-com-to-lichess/feedback';
  118. errorText.innerHTML = "Error when uploading to lichess. Click here and report this at greasyfork, thanks!";
  119. placeElement(errorText, "//html/body/center[2]/div");
  120. console.info(response.status);
  121. }
  122. }
  123. function exportAction() {
  124. getCleanPgn(submitToLichess);
  125. }
  126. function addExportButton() {
  127. // create div
  128. var div_block = document.createElement('div');
  129. div_block.setAttribute("id", "exportDiv");
  130. div_block.setAttribute("align", "left"); // I'd like all elements in the div to be aligned to the left
  131. var button = '<button id="exportButton" type="button">export to lichess</button>';
  132. var label = '<p id="urlLabel" hidden=true>exported game URL:</p>';
  133. var url = '<a id="lichessURL" hidden=true href="https://lichess.org">hidden</a>';
  134. div_block.innerHTML = button+label+url;
  135. placeElement(div_block, "//html/body/center[2]/div");
  136.  
  137. // set up listener
  138. document.getElementById ("exportButton").addEventListener('click', exportAction);
  139. }
  140. addExportButton();
  141.  
  142. })();