您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Allows a user to quickly export a PGN to lichess for analysis
当前为
- // ==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();
- })();