Skip to content

Instantly share code, notes, and snippets.

@ecornell
Last active January 9, 2025 21:21
Show Gist options
  • Save ecornell/7b59c98373a598dae2f509f83e202fb3 to your computer and use it in GitHub Desktop.
Save ecornell/7b59c98373a598dae2f509f83e202fb3 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Sidebar Enhancements
// @description Sidebar Enhancements
// @summary Enhances the sidebar with auto-expand on mouse enter and auto-collapse on mouse leave
// @notes Firefox 135+ required - vertical tabs must be enabled
// @author ecornell
// @include main
// @startup UC.sidebar.exec(win);
// @shutdown UC.sidebar.destroy();
// @onlyonce
// @source https://gist.github.com/ecornell/7b59c98373a598dae2f509f83e202fb3
// ==/UserScript==
UC.sidebar = {
timeoutIds: [], // Change timeoutId to an array to store multiple IDs
exec: function (win) {
win.addEventListener("keydown", this.sidebar.bind(this), false);
win.document.addEventListener('DOMContentLoaded', () => {
const sidebar = win.document.getElementById('sidebar-main');
let timeoutId;
if (sidebar) {
sidebar.addEventListener('mouseenter', () => {
timeoutId = this.addEventListenerWithTimeout(win, () => { // Store timeoutID for clearing later
if (sidebar.clientWidth < 200) {
win.SidebarController._state.launcherExpanded = true;
}
}, 300); // ms delay
});
sidebar.addEventListener('mouseleave', () => {
//console.log('Mouse left sidebar' + timeoutId);
win.clearTimeout(timeoutId); // Clear the specific timeoutId
this.timeoutIds = this.timeoutIds.filter(id => id !== timeoutId); // Remove the cleared timeoutId from the array
win.SidebarController._state.launcherExpanded = false;
});
} else {
console.error("Element with ID 'sidebar-main' not found.");
}
});
},
addEventListenerWithTimeout(win, listener, timeout) {
const timeoutId = win.setTimeout(() => {
//console.log('Timeout triggered');
listener();
// Remove the timeoutId from the array once the listener is executed
this.timeoutIds = this.timeoutIds.filter(id => id !== timeoutId);
}, timeout);
//console.log('Timeout ID:', timeoutId);
this.timeoutIds.push(timeoutId); // Store the timeoutId in the array
return timeoutId; // Return the timeoutId for reference
},
// Toggle sidebar with F1 key
sidebar(ev) {
if (ev.key === 'F1') { // event.ctrlKey && event.shiftKey && event.key === '***'
ev.currentTarget.SidebarController.handleToolbarButtonClick()
ev.preventDefault(); //Optional - Prevent default action if needed
return;
}
},
destroy: function () {
window.removeEventListener("keydown", this.sidebar, false);
delete UC.sidebar;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment