Lichess Auto Add Time

Automatically Adds Time to Opponenents Clock To Keep Game Going

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          Lichess Auto Add Time
// @namespace     http://userstyles.org
// @description   Automatically Adds Time to Opponenents Clock To Keep Game Going
// @author        636597
// @include       https://lichess.org/*
// @run-at        document-start
// @version       0.2
// ==/UserScript==


// Doesn't Check yet if board is "Flipped" , so thats going to be a problem

var locked = false;
var opponent_clock_element = null;
var more_time_element = null;
var opponent_clock_observer = null;
var observerConfig = {
    childList: true,
    attributes: true,
    characterData: true,
    subtree: true,
    attributeOldValue: true,
    characterDataOldValue: true
};

(function(){

	function add_3_time() {
		if ( locked ) { return; }
		locked = true;
		console.log( "Trying to Add Time");
		more_time_element.click();
		setTimeout( function() {
			more_time_element.click();
		} , 300 );
		setTimeout( function() {
			more_time_element.click();
			locked = false;
		} , 300 );
	}

	function observe_clock() {
		opponent_clock_observer = new MutationObserver( function( mutations ) {
			mutations.forEach( function( mutation , index ) {
				if ( mutation.target.className === "time" ) {
					if ( mutation.addedNodes.length === 4 ) {
						var minutes = parseInt( mutation.addedNodes[ 0 ].data );
						var seconds = parseInt( mutation.addedNodes[ 2 ].data );
						if ( minutes < 1 ) {
							if ( seconds < 10 ) {
								//console.log( "Opponent's Time Low === " + minutes.toString() + " :: " + seconds.toString() );
								add_3_time();
								return;
							}
						}
					}
				}
			});
		});
		opponent_clock_observer.observe( opponent_clock_element , observerConfig );
	}

	function wait_for_clock() {
		var ready = setInterval(function(){
			var x1 = document.getElementsByClassName( "rclock-top" )
			if ( x1 ) {
				if ( x1[ 0 ] ) {
					opponent_clock_element = x1[ 0 ];
					more_time_element = document.getElementsByClassName( "moretime" );
					if ( more_time_element ) {
						if ( more_time_element[ 0 ] ) {
							more_time_element = more_time_element[ 0 ];
							clearInterval( ready );
							observe_clock();
						}
					}
				}
			}
		} , 2 );
		setTimeout( function() { clearInterval( ready ); } , 20000 );
	}

	window.addEventListener ( "load" , wait_for_clock );
})();