User:Fred Gandt/responsiveHistoryCompare.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$( document ).ready( () => {
	"use strict";
	if ( mw.config.get( "wgAction" ) === "history" ) {
		const HC = document.getElementById( "mw-history-compare" );
        if ( HC ) {
            const BTNS = HC.querySelectorAll( 'input[value="Compare selected revisions"]' ),
				TOP = BTNS[ 0 ], BTM = BTNS[ 1 ],
				encapsulate = () => {
					const LSB = HC.querySelector( "li.selected.before" ),
						LSA = HC.querySelector( "li.selected.after" );
					if ( TOP && LSB ) {
						LSB.before( TOP );
					}
					if ( BTM && LSA ) {
						LSA.after( BTM );
					}
					/* move buttons added by User:Enterprisey/cv-revdel.js along with the "Compare selected revisions" buttons */
					HC.querySelectorAll( "button.cv-rd-add-range" ).forEach( ( b, i ) => BTNS[ i ]?.before( b ) );
				};
			encapsulate();
			HC.addEventListener( "change", encapsulate, { passive: true } );
		}
	}
} );