Hibernate Background Tabs Keyboard Shortcut Hotkey
-
What?
- Based off of a request by @stardepp here: Keyboard shortcut for "Hibernate Background Tabs"
- This adds a Keyboard Shortcut / Hotkey that will hibernate all tabs other than the currently active tab.
- The default combo used is
Alt
+H
, but it can be configured.
Installation
- Look at the Pinned Posts in the Modding Forum here to see how to install a JavaScript [functionality] mod.
Configuration
- This mod has 3 configuration variables near the top that you can use to change the behavior of the mod:
HIBERNATE_KEY_COMBO
- Set this to the Keyboard Shortcut / Hotkey that you want to use to activate the hibernate command
- Enter a new combo in the form of:
"Ctrl+Shift+Alt+KEY"
- e.g.
"Alt+H"
- e.g.
- Avoid combos that are already assigned in Vivaldi's settings# JavaScript
SKIP_SELECTED_TABS
- Set this to
true
to not hibernate any tabs that are selected (blue dashed border) at the time of running the hibernation command - Set to
false
to hibernate background tabs regardless of their selected state
- Set this to
SKIP_ACTIVE_TAB_STACK_TABS
- Set this to
true
to not hibernate any tabs that are in the same tab stack as the currently active tab at the time of running the hibernation command - Set to
false
to hibernate background tabs even if they are in the same tab stack as the active tab
- Set this to
JavaScript
(function () { // ============================================================================================================ // Hibernate Background Tabs Keyboard Shortcut Mod // URL: https://forum.vivaldi.net/topic/60172/hibernate-background-tabs-keyboard-shortcut-hotkey // Description: Adds a user defined keyboard shortcut that will hibernate all tabs except the active one // Author(s): @nomadic [ based on a mod by @LonM "Custom Keyboard Shortcuts Mod" ] // CopyRight: No Copyright Reserved // ============================================================================================================ function hibernateBackgroundTabsHotkey() { // Config ------------ // Keyboard shortcut combo to hibernate all background tabs // written in the form "Ctrl+Shift+Alt+KEY" const HIBERNATE_KEY_COMBO = "Alt+H"; const SKIP_SELECTED_TABS = true; const SKIP_ACTIVE_TAB_STACK_TABS = false; // ------------------- // for finding tab stack id in tab data const regex = /(?<="group":")[a-z0-9]+(?=")/i; function hibernateBackgroundTabs() { vivaldi.windowPrivate.getCurrentId((currentWindowId) => { chrome.tabs.query({ active: true, windowId: currentWindowId }, (activeTab) => { activeTab = activeTab[0]; let activeTabStackID = (regex.exec(activeTab.extData) || [false])[0]; chrome.tabs.query({ windowType: "normal" }, (tabs) => { tabs.forEach((tab) => { if (tab.discarded) return; // already discarded, so can skip it if (tab.windowId === currentWindowId && tab.active) return; // skip active tab if (SKIP_SELECTED_TABS && tab.highlighted) return; // skip selected tabs if configured to do so // skip tabs in the same tab stack as the active tab if configured to do so if (SKIP_ACTIVE_TAB_STACK_TABS && activeTabStackID) { let stackID = (regex.exec(tab.extData) || [""])[0]; if (stackID === activeTabStackID) return; } chrome.tabs.discard(tab.id); }); }); }); }); } function areStringsEqualIgnoringCase(string1, string2) { return string1.localeCompare(string2, undefined, { sensitivity: "base" }) === 0; } // UPDATE: event now returns window ID before giving the combo function handleKeyboardShortcut(_, combo) { if (areStringsEqualIgnoringCase(HIBERNATE_KEY_COMBO, combo)) { hibernateBackgroundTabs(); } } vivaldi.tabsPrivate.onKeyboardShortcut.addListener(handleKeyboardShortcut); } let intervalID = setInterval(() => { const browser = document.getElementById("browser"); if (browser) { clearInterval(intervalID); hibernateBackgroundTabsHotkey(); } }, 300); })();
Edits:
- (April 27th, 2021 - 1) - As per the suggestion of @chonkyPhteven in the status bar button version of this mod, I added additional configuration options to not hibernate tabs in the active tab stack or not hibernate selected tabs.
- (March 29th, 2022) - The keyboard shortcut event now gives the
windowID
before the key combo, so updated the handler to properly check the combo.
-
Added two new configuration options to avoid hibernating certain tabs when the keyboard shortcut is used.
One lets you hibernate all background tabs other than those that are selected, and the other lets you hibernate all background tabs other than those in the same tab stack as the active tab. These two options can of course be used at the same time to be combined as well.
This change was based off a comment by @chonkyPhteven on the status bar version of this mod.
-
@nomadic I am once again very excited about your programming skills, thanks for your effort, this mod works reliably.
Here are my keyboard shortcuts:
-
@nomadic Yo, can you combine these enhancements with the Tabbar button version when you have the free time? Then we all can use both buttons & shortcuts with just 1 js mod.
Thanks for all your great work! Vivaldi should hire you as remote freelancer to fulfill some of their feature requests. LOL
-
@dude99 said in Hibernate Background Tabs Keyboard Shortcut Hotkey:
Vivaldi should hire you as remote freelancer to fulfill some of their feature requests.
Yes!!!!!
-
@dude99 Here they are combined.
Thanks for all your great work! Vivaldi should hire you as remote freelancer to fulfill some of their feature requests. LOL
Thanks for the flattery (will try not to let it go to my head), but I am having fun making fast and loose mods.
They should also probably go for someone that actually knows ReactJS and didn't start using JavaScript about a year ago.
From https://vivaldi.com/jobs/front-end-developer/
Requirements:
- ...
- Deep understanding of JavaScript.
- Experience with React, or similar frameworks.
- Good knowledge of web standards.
-
Ok guys, go HERE for Tabbar button version.
-
@nomadic This is a great mod, I came across it while looking for a way to hibernate all tabs on all windows. While this doesn't do that it does make it quicker when going to each window to do so.
But it got me thinking, looking at the code (and I'm no coder), it looks like it could be modified to loop through all the windowId's rather than just the currently active one. But I have no idea how to actually do it. Any chance you could make a version with that ability?
-
@explorerthegeneral I just tested it, and other than needing to update an event handler to bring it up to date with
5.1.2567.73
(see original post for updated code), it should hibernate background tabs in all open windows.GIF showing it working
One thing that the mod won't do is hibernate the active tab of any given window (even if it isn't focused). This is more a limitation of Vivaldi than a conscious choice I made while writing the mod. However, this behavior does make sense. Any active tab will need to be loaded again instantly, so it doesn't make much sense to allow hibernation for active tabs.
But it got me thinking, looking at the code (and I'm no coder), it looks like it could be modified to loop through all the windowId's rather than just the currently active one.
It is looping over all tabs rather than windows. The beginning section with the mention of the current windowId is for one of the config parameters. Here is a more thoroughly commented section of the relevant code to better explain it's function:
function hibernateBackgroundTabs() { // This section is needed for the SKIP_ACTIVE_TAB_STACK_TABS config parameter // ================ START ================ // figure out the ID of the active window to allow fetching the active tab vivaldi.windowPrivate.getCurrentId((currentWindowId) => { // get the active tab chrome.tabs.query({ active: true, windowId: currentWindowId }, (activeTab) => { activeTab = activeTab[0]; // see if the active tab is in a tab stack, and if so, get the stack ID let activeTabStackID = (regex.exec(activeTab.extData) || [false])[0]; // ================= END ================= // get tabs from all normal (not devTool or popup) windows chrome.tabs.query({ windowType: "normal" }, (tabs) => { // loop through all tabs and check if it should be hibernated or not based off of parameters of the tab and mod config tabs.forEach((tab) => { if (tab.discarded) return; // already discarded, so can skip it if (tab.windowId === currentWindowId && tab.active) return; // skip active tab; apparently not needed; Vivaldi already handles if (SKIP_SELECTED_TABS && tab.highlighted) return; // skip selected tabs if configured to do so // skip tabs in the same tab stack as the active tab if configured to do so if (SKIP_ACTIVE_TAB_STACK_TABS && activeTabStackID) { let stackID = (regex.exec(tab.extData) || [""])[0]; if (stackID === activeTabStackID) return; } chrome.tabs.discard(tab.id); }); }); }); }); }