Context Menu Option to Go to "Parent Tab"
-
What?
-
This is based off of a feature request made by @oskark here: https://forum.vivaldi.net/topic/45626/go-to-the-site-from-which-you-came-to-a-tab
-
The basic idea of this mod is that it keeps track of the "parent tab" of each newly created tab. Using Chrome.tabs, this is referred to as
openerTabId
. This "parent tab" can be accessed by the right click context menu on webpages. -
This mod is essentially a micro-extension rather than a UI mod,
so I might wrap it up into a full extension if there is any desire from potential users. (I did include one bug-fix that relies on Vivaldi's UI, but this bug is only present because the code runs on every window of Vivaldi and wouldn't be an issue as an extension.)This mod can now be installed as an extension. You can find an unpacked and packed version of it on GitHub here.- The extension version also includes a keyboard shortcut option that you can set on the extensions page. You might need to set it to a
Global
shortcut rather than anin Vivaldi
shortcut for it to work.
- The extension version also includes a keyboard shortcut option that you can set on the extensions page. You might need to set it to a
Installation- Look at the Pinned Posts in the Modding Forum here to see how to install a
JavaScript
mod for "Adding Functionality
." - Alternatively, you can also install it as an extension from here.
Demo
-
Switching to parent tab if the parent tab is still open and on the same url
-
Creating a new tab with the original parent tab url if the parent tab is closed or has navigted to a new url
Javascript
(function () { // ============================================================================================================ // Go to the tab that opened the current tab with context menu option. // - made by nomadic on the Vivaldi Forums // ============================================================================================================ function addParentTabContextMenu() { // add an option to the conext menu chrome.contextMenus.create({ id: "parent-tab", title: "&Go to parent tab", contexts: ["all"], enabled: false, }); // Determines if the parent tab is still valid and switches to it or creates a new tab with the valid url function goToParent() { // get the active tab chrome.tabs.query({ currentWindow: true, active: true }, (tab) => { tab = tab[0]; let parentTabId = tab.openerTabId; let tabId = tab.id; // see what the tab's parent tab url is supposed to be chrome.storage.local.get(tabId.toString(), (storageItems) => { let savedUrl = storageItems[tabId.toString()]; // if the parent tab still exists ... if (parentTabId) { // ... check the current url of the parent tab chrome.tabs.get(parentTabId, (parentTab) => { let parentTabUrl = parentTab.url ? parentTab.url : parentTab.pendingUrl ? parentTab.pendingUrl : null; if (parentTabUrl === savedUrl) { // if the parent tab has the same url as it did in the past, switch to the parent tab chrome.tabs.update(parentTabId, { highlighted: true }); } else if (savedUrl && savedUrl !== "") { // create a new tab since the parent tab no longer has the correct url chrome.tabs.create({ url: savedUrl }); } }); } else if (savedUrl && savedUrl !== "") { // the parent tab no longer exists, so create a new tab with the saved url chrome.tabs.create({ url: savedUrl }); } }); }); } // save a given tab's parent url to local storage function saveParentUrl(tab) { let tabId = tab.id; let storageOBJ = {}; if (tab.openerTabId) { chrome.tabs.get(tab.openerTabId, (parentTab) => { let url = parentTab.url ? parentTab.url : parentTab.pendingUrl ? parentTab.pendingUrl : ""; storageOBJ[tabId.toString()] = url; chrome.storage.local.set(storageOBJ, function () { enableDisableContextMenu(tabId); }); }); } else { storageOBJ[tabId.toString()] = ""; chrome.storage.local.set(storageOBJ, function () { enableDisableContextMenu(tabId); }); } } function enableDisableContextMenu(tabId) { chrome.storage.local.get(tabId.toString(), (storageItems) => { let savedUrl = storageItems[tabId.toString()]; if (!savedUrl || savedUrl === "") { chrome.contextMenus.update("parent-tab", { enabled: false }); } else { chrome.contextMenus.update("parent-tab", { enabled: true }); } }); } // listener for clicks on context menu item chrome.contextMenus.onClicked.addListener((info) => { //* BUG-FIX: multiple windows led to creation of duplicate tabs equal to number of windows let doesntHaveFocus = document.querySelector("#browser.isblurred"); if (doesntHaveFocus) return; // Match id used in context menu item creation if (info.menuItemId === "parent-tab") { goToParent(); } }); // whenever a new tab is created, add its parent tab's url to storage chrome.tabs.onCreated.addListener(saveParentUrl); // remove storage values when a tab is closed chrome.tabs.onRemoved.addListener((tabId) => { chrome.storage.local.remove(tabId.toString()); }); // enable/disable the context menu item as applicable chrome.tabs.onActivated.addListener((activeInfo) => { let tabId = activeInfo.tabId; enableDisableContextMenu(tabId); }); } // Loop waiting for the browser to load the UI let intervalID = setInterval(function () { const browser = document.getElementById("browser"); if (browser) { clearInterval(intervalID); addParentTabContextMenu(); } }, 300); })();
Notes:
- Opening links in new windows doesn't appear to set the value of
openerTabId
, so the context menu item won't work on these tabs. - Reopened sessions will not work properly with the current code.
- For any page the context menu won't work on, the option should be grayed out and disabled.
Edits:- (June 9, 2020) - Added link to Extension Version, context menu title suggestion of @barbudo2005, and better handling of tabs opened from a session.
- (June 11, 2020) - "Open in New Tab" caused checking for if the extension should be enabled on the page before values were written to storage. This lead to the context menu option to be falsely disabled.
-
-
@nomadic Thank you. I add an & (title: "&Go to parent tab") to can use it with a shortcut : gg
-
Could this be implemented as an extension instead? That would be easier for people to install and you would not need to re install after updates.
It looks like it only needed the chrome extension api.
-
@LonM Yeah, I can do that.
I probably won't publish it on the Chrome Web Store unless I get around to setting up an account.
-
This post is deleted! -
@barbudo2005 Thanks for the tip. Updated the code to include it!
Didn't even know that was possible. Wish the documentation had something about that functionality.
-
Added a link to an extension version in the original post. Another benefit of the extension version is a keyboard shortcut.
I just created a basic icon, so feel free to make a pull request if you can come up with a better one.
-
Thanks!
-
@nomadic When you use & you can access the command with the keyboard. So I make a script to simulate context menu and click the letter "g". Then I assign a mnemonic shortcut to the script like "gp" or "gg" (easier and faster). Thank again for the extension.
-
I use the method "all links open in new tab", so your extension is more helpful to me than the Rewind button.
-
@nomadic It seems there is a bug in the extension. I have the script and the extension installed:
-
@barbudo2005 Did you update the script code when I made the edit? I probably should have made a post saying I updated the script as well.
There was an issue with the original code in regard to tabs in reopened sessions. Their saved URLs were
undefined
, so if you click thatGo to parent tab
from the script, it brings you to a new tab with the default Chrome new tab page.I fixed that when I made the extension by disabling the context menu option for tabs from reopened sessions, so I added the change to the script as well. Just changed a few
if
statements.Can you confirm that clicking the script version's
Go to parent tab
doesn't actually bring you to a useful page? -
@nomadic :
I update the script.
I open Vivaldi browser, next open Vivaldi forum. I click in a thread. In this thread the context menu display both disabled. Not OK
But the context menu of the extension display:
If you click "&Go to parent tab" it goes to the parent tab : Vivaldi forum. OK
If you come back to the thread the context menu display :
And both the script and the extension go to the parent tab.
-
@barbudo2005 Ok, I updated the extension and the script. Can you try using them now?
-
@nomadic : Now both performs well. Thank you.
-
This mod has been an excellent substitute for my "Preserve tab history when a link is opened in new tab" feature request as well. It works great, and the multiple tabs bug-fix has been useful in two of my other mods as well. The only change I made was changing
contexts: ["all"],
tocontexts: ["page"],
as I don't think it needs to clutter text selection menu or other context menus, just the page menu is sufficient. Other than that, it's been excellent in every way. -
Ppafflick moved this topic from Modifications on