Skip to content

Instantly share code, notes, and snippets.

@0187773933
Last active June 27, 2021 07:33
Show Gist options
  • Save 0187773933/3822c5cc165dcdb31413460849390fc9 to your computer and use it in GitHub Desktop.
Save 0187773933/3822c5cc165dcdb31413460849390fc9 to your computer and use it in GitHub Desktop.
Chess Com POST PGN to Lichess UserScript
// ==UserScript==
// @name Chess Com POST PGN to Lichess
// @namespace http://userstyles.org
// @description Posts PGN to Lichess via Added Button
// @author 707433
// @include *://*chess.com/live/*
// @run-at document-start
// @version 0.1
// ==/UserScript==
// NOTES
// Currently not happening bcoz of lichess.
// IDK , maybe you can fix it.
// Everything is there , but it lichess silently blocks it.
// https://lichess.org/api
// For Auto Login to Lichess , install this chrome extension to dowload your 'logged in' lichess session cookie
// https://chrome.google.com/webstore/detail/cookiestxt/njabckikapfpffapmjgojcnbfjonfjfg/related
// Then replace the cookie in the POST data
function post_pgn_to_lichess( pgn_string ) {
// May Need to URL encode pgn_string ??
pgn_string = encodeURIComponent( pgn_string );
pgn_string = "pgn=" + pgn_string;
fetch( 'https://lichess.org/import' , {
mode: 'no-cors' ,
method: 'post' ,
headers: {
'authority': 'lichess.org',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'origin': 'https://lichess.org',
'upgrade-insecure-requests': '1',
'dnt': '1',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'sec-fetch-dest': 'document',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'referer': 'https://lichess.org/paste',
'accept-language': 'en-GB,en;q=0.9,en-US;q=0.8,es;q=0.7',
'cookie': 'lila2=$YOUR_CUSTOM_COOKIE'
} ,
//body: JSON.stringify({a: 7, str: 'Some string: &=&'})
body: pgn_string
})
.then( res => res.json() )
.then( res => console.log( res ) );
}
function post_pgn_button_on_click() {
console.log( "testing" );
var download_button = document.querySelector( 'span.download' );
download_button.click();
setTimeout(function(){
var pgn_string = document.querySelector( 'textarea[name="pgn"]' ).__vue__.value;
console.log( pgn_string );
post_pgn_to_lichess( pgn_string );
} , 1000 );
}
function add_post_pgn_button() {
try {
var post_pgn_button = document.body.querySelector( "#x1_load" );
if ( post_pgn_button ) { return true; }
var custom_button_parent = document.querySelector( 'div.secondary-controls-action-group' );
custom_button_parent.insertAdjacentHTML( 'beforeend' , '<button id="x1_load">POST PGN</button>' );
post_pgn_button = document.body.querySelector( "#x1_load" );
post_pgn_button.addEventListener( "click" , post_pgn_button_on_click );
return true;
}
catch( error ) { return false; }
}
var URL_STATE_IN_2020_INTERVAL = false;
var CURRENT_URL = false;
function observe_url_state() {
if ( CURRENT_URL !== window.location.href ) {
console.log( "URL State Changed" );
var result = add_post_pgn_button()
if ( result ) {
CURRENT_URL = window.location.href;
}
}
}
function init() {
setTimeout( function() {
observe_url_state();
URL_STATE_IN_2020_INTERVAL = setInterval( observe_url_state , 500 );
add_post_pgn_button();
} , 1000 );
}
(function() {
var READY_CHECK_INTERVAL = setInterval(function(){
var x1 = document.querySelectorAll( 'div#board-layout-chessboard' );
if ( x1 ) { if ( x1[ 0 ] ) { clearInterval( READY_CHECK_INTERVAL ); init(); } }
} , 2 );
setTimeout( function() {
clearInterval( READY_CHECK_INTERVAL );
} , 10000 );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment