Skip to content

Instantly share code, notes, and snippets.

@NoxArt
Last active March 18, 2025 09:08
AdminerRestoreMenuScroll plugin for Adminer
<?php
/**
* Remembers and restores scrollbar position of side menu
*
* Changes (2025-03-18):
* - Added nonce()
* - Changed load event binding
*
* @author Jiří @NoxArt Petruželka
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
class AdminerRestoreMenuScroll {
protected $script;
/**
* @param string text to append before first calendar usage
*/
public function __construct() {
$this->script = "
<script type='text/javascript' " . Adminer\nonce() .">
(function(){
var executed = false;
var saveAndRestore = function() {
if( executed ) {
return;
}
executed = true;
var menu = document.getElementById('menu');
var scrolled = localStorage.getItem('_adminerScrolled');
if( scrolled && scrolled >= 0 ) {
menu.scrollTop = scrolled;
}
window.addEventListener('unload', function(){
localStorage.setItem('_adminerScrolled', menu.scrollTop);
});
};
window.addEventListener('load', saveAndRestore);
})();
</script>";
}
public function head() {
echo $this->script;
}
}
@D-ominik
Copy link

This plugin is one of the most valuable for Adminer, unfortunately it doesn't work anymore. Here is an updated version:

<?php
/** Remembers and restores scollbar position of side menu
* @author Jiří @NoxArt Petruželka, www.noxart.cz
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
class AdminerRestoreMenuScroll {

	public function __construct() {
	}

	public function head(){
		?>
		<script <?php echo nonce(); ?>>
		(function(){
			var executed = false;
			var saveAndRestore = function() {
				if (executed) {
					return;
				}

				executed = true;
				var menu = document.getElementById('menu');
				var scrolled = localStorage.getItem('_adminerScrolled');
				if (scrolled && scrolled >= 0) {
					setTimeout(function() {
						menu.scrollTop = scrolled;
					}, 50);
				}

				window.addEventListener('unload', function(){
					localStorage.setItem('_adminerScrolled', menu.scrollTop);
				});
			};
			document.addEventListener && document.addEventListener('DOMContentLoaded', saveAndRestore);
			document.attachEvent && document.attachEvent('onreadystatechange', saveAndRestore);
		})();
		</script>
		<?php
	}

}

@NoxArt
Copy link
Author

NoxArt commented Sep 30, 2021

Glad you find it helpful. Thank you for fixing it and also reformatting, much more readable

@vrana
Copy link

vrana commented Mar 16, 2025

Adminer 5 wrapped itself into a namespace and plugins now need to call Adminer's functions via this namespace.

Please update this Gist to this: https://gist.github.com/vrana/8d9e12e260731a516f973a284760aa03

@NoxArt
Copy link
Author

NoxArt commented Mar 18, 2025

Done, thank you

I also done some changes very recently to it since it seemed to had some issues, can you please re-check it? It should still be hopefully ok. I kept the nonce you've added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment