Greasy Fork is available in English.

Lichess Auto Rematch

Automatically Presses Rematch Button When Available

// ==UserScript==
// @name          Lichess Auto Rematch
// @namespace     http://userstyles.org
// @description   Automatically Presses Rematch Button When Available
// @author        636597
// @include       https://lichess.org/*
// @run-at        document-start
// @version       0.1
// ==/UserScript==


function request_rematch() {
	lichess.socket.send( "rematch-yes" );
}

var rematch_button_element = false;
var rematch_button_observer = false;

var document_observer = false;
var parent_element = false;
var observerConfig = {
	childList: true,
	attributes: true,
	characterData: true,
	subtree: true,
	attributeOldValue: true,
	characterDataOldValue: true
};

function observe_rematch_button() {
	rematch_button_element = document.querySelector( "button.rematch" );
	if ( !rematch_button_element ) {
		console( "Could Not Find Rematch Button To Observe" );
		return;
	}
	rematch_button_observer = new MutationObserver( function( mutations ) {
		mutations.forEach( function( mutation , index ) {
			if ( mutation.type === "childList" ) {
				if ( mutation.removedNodes ) {
					if ( mutation.removedNodes[ 0 ] ) {
						if ( mutation.removedNodes[ 0 ].className ) {
							if ( mutation.removedNodes[ 0 ].className === "spinner" ) {
								console.log( "Someone Cancelled The Rematch" );
								console.log( "Re-Requesting Rematch" );
								request_rematch();
							}
						}
					}
				}
			}
			console.log( mutation );
		});
	});
	rematch_button_observer.observe( rematch_button_element , observerConfig );
	console.log( "Observing Rematch Button" );
}

function observe_document() {
	parent_element = document.querySelector( "main.round" );
	console.log( parent_element );
	if ( !parent_element ) {
		console.log( "Could Not Find Parent Element to Observe" );
		return;
	}
	document_observer = new MutationObserver( function( mutations ) {
		mutations.forEach( function( mutation , index ) {
			if ( mutation.target.firstChild ) {
				if ( mutation.target.firstChild.className ) {
					if ( mutation.target.firstChild.className === "follow-up" ) {
						console.log( "Rematch Button Available" );
						request_rematch();
						observe_rematch_button();
					}
				}
			}
		});
	});
	document_observer.observe( parent_element , observerConfig );
	console.log( "Lichess Auto-Rematch Loaded" );
}

( function() {
	window.addEventListener ( "load" , observe_document );
})();