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.
// @ts-check
// Delay untill the toolbar loads
mw.hook( 'ext.pageTriage.toolbar.ready' ).add( function () {
	mw.loader.using( [ 'mediawiki.api' ], function () {
		if ( mw.storage.get( 'user-js-fastreview-temp-tk' ) ) {
			mw.notify( 'Reviewed page!' );
			mw.storage.remove( 'user-js-fastreview-temp-tk' );
		}

		document.addEventListener( 'keydown', function ( e ) {
			if ( e.target !== document.body ) {
				return;
			}

			if ( ( e.key === 'r' || e.key === 't' ) && mw.config.get( 'wgIsRedirect' ) ) {
				new mw.Api( {
					ajax: {
						headers: {
							'Api-User-Agent': 'User:Sohom_Datta/fastreview.js'
						}
					}
				} ).postWithEditToken( {
					action: 'pagetriageaction',
					reviewed: 1,
					// @ts-ignore
					pageid: mw.config.get( 'wgRelevantArticleId' )
				} ).then( function () {
					mw.storage.set( 'user-js-fastreview-temp-tk', '1' );
					if ( e.key === 'r' ) {
						setTimeout( function () {
							document.location.reload();
						}, 1 );
					} else {
						// eslint-disable-next-line no-jquery/no-global-selector
						$( '#mwe-pt-next .mwe-pt-tool-icon' ).trigger( 'click' );
					}
				} ).catch( function () {
					mw.notify( 'Failed to review page' );
				} );
			}

			if ( e.key === 'b' ) {
				history.back();
			}

			if ( e.key === 'n' ) {
				// We need a better API for this, but this hack works for now!
				// eslint-disable-next-line no-jquery/no-global-selector
				$( '#mwe-pt-next .mwe-pt-tool-icon' ).trigger( 'click' );
			}
		} );
	} );
} );