Add 'export to lichess' link to chessgames.com pages

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

当前为 2021-11-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Add 'export to lichess' link to chessgames.com pages
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  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_ANALYSIS = "https://lichess.org/paste";
  18. function getElementByXpath(path) {
  19. return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  20. }
  21.  
  22. function buildPGNLink() {
  23. var url = new URL(window.location.href);
  24. var gid = url.searchParams.get("gid");
  25. var link = "https://www.chessgames.com/perl/nph-chesspgn?text=1&gid=" + gid;
  26. return link;
  27. }
  28.  
  29. function buildLichessLink(callback) {
  30. var pgnLink = buildPGNLink();
  31. fetch(pgnLink).then(function(response) {
  32. response.text().then(function(body){
  33. var lichessLink = document.createElement("a");
  34. lichessLink.href = LICHESS_ANALYSIS + "?pgn="+cleanPGN(body);
  35. lichessLink.target = "_blank";
  36. lichessLink.text = "export to lichess.org";
  37. lichessLink.style.font = 'bold 12px verdana,arial,helvetica';
  38. lichessLink.style.color = '#4D4D4D'; // lichess grey!
  39. callback(lichessLink);
  40. });
  41. });
  42. }
  43.  
  44. function cleanPGN(pgn) {
  45. var newPGN = pgn.split("\n");
  46. newPGN = newPGN.join(" ");
  47. return newPGN;
  48. }
  49.  
  50. function addLichessLink(link) {
  51. // link is added below the "Get this game explained" advertisement
  52. var block = getElementByXpath("//html/body/center[2]/div");
  53. block.parentNode.insertBefore(link, block.nextSibling);
  54. }
  55.  
  56. buildLichessLink(addLichessLink);
  57.  
  58. })();