• Browser
  • Mail
  • News
  • Community
  • About
Register Login
HomeBlogsForumThemesContributeSocial

Vivaldi

  • Browser
  • Mail
  • News
  • Community
  • About

Navigation

    • Home
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    1. Home
    2. Desktop
    3. Archive
    4. Context Menu Option to Go to "Parent Tab"

    Context Menu Option to Go to "Parent Tab"

    Archive
    context menu tabs mod
    5
    16
    1.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nomadic
      N
      nomadic Soprano
      last edited by nomadic

      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 an in Vivaldi shortcut for it to work.

      Ā 
      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
        switchTab.gif

      • Creating a new tab with the original parent tab url if the parent tab is closed or has navigted to a new url
        newTab-smaller.gif
        Ā 

      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.
      1 Reply Last reply Reply Quote 10
      • barbudo2005
        B
        barbudo2005
        last edited by

        @nomadic Thank you. I add an & (title: "&Go to parent tab") to can use it with a shortcut : gg

        nomadic
        N
        1 Reply Last reply
        Reply Quote 1
        • LonM
          L
          LonM Soprano Patron Moderator
          last edited by LonM

          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.

          šŸ’» Windows 10 64-bit Sopranos Builds • en-GB • šŸ—³ vote for features • šŸ•µļøā€ā™€ļø Code of Conduct • šŸž Report bugs

          nomadic
          N
          1 Reply Last reply
          Reply Quote 1
          • nomadic
            N
            nomadic Soprano @LonM
            last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • nomadic
              N
              nomadic Soprano
              last edited by nomadic

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • nomadic
                N
                nomadic Soprano @barbudo2005
                last edited by

                @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.

                1 Reply Last reply Reply Quote 0
                • nomadic
                  N
                  nomadic Soprano
                  last edited by

                  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.

                  Zalex108
                  Z
                  1 Reply Last reply
                  Reply Quote 0
                  • Zalex108
                    Z
                    Zalex108 Moderator @nomadic
                    last edited by

                    @nomadic

                    Thanks!

                    "You cannot know the meaning of your life until you are connected to the power that created you" Ā· Shri Mataji Nirmala Devi

                    1 Reply Last reply Reply Quote 1
                    • barbudo2005
                      B
                      barbudo2005
                      last edited by

                      @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.

                      1 Reply Last reply Reply Quote 1
                      • barbudo2005
                        B
                        barbudo2005
                        last edited by

                        I use the method "all links open in new tab", so your extension is more helpful to me than the Rewind button.

                        1 Reply Last reply Reply Quote 0
                        • barbudo2005
                          B
                          barbudo2005
                          last edited by

                          @nomadic It seems there is a bug in the extension. I have the script and the extension installed:

                          d8516910-08c6-42a7-bbce-041a4eaf510e-image.png

                          nomadic
                          N
                          1 Reply Last reply
                          Reply Quote 0
                          • nomadic
                            N
                            nomadic Soprano @barbudo2005
                            last edited by nomadic

                            @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 that Go 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?

                            1 Reply Last reply Reply Quote 0
                            • barbudo2005
                              B
                              barbudo2005
                              last edited by

                              @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

                              57152892-b00f-47de-998a-bb65ae87ad36-image.png

                              But the context menu of the extension display:

                              ad88f142-2434-4f14-9964-aab2650cd68b-image.png

                              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 :

                              ac882e58-f1b3-4e36-b0ee-6f0ee780931c-image.png

                              And both the script and the extension go to the parent tab.

                              nomadic
                              N
                              1 Reply Last reply
                              Reply Quote 0
                              • nomadic
                                N
                                nomadic Soprano @barbudo2005
                                last edited by

                                @barbudo2005 Ok, I updated the extension and the script. Can you try using them now?

                                1 Reply Last reply Reply Quote 0
                                • barbudo2005
                                  B
                                  barbudo2005
                                  last edited by

                                  @nomadic : Now both performs well. Thank you.

                                  1 Reply Last reply Reply Quote 1
                                  • D
                                    debiedowner
                                    last edited by

                                    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"], to contexts: ["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.

                                    1 Reply Last reply Reply Quote 1
                                    • pafflick
                                      P
                                      pafflick moved this topic from Modifications on
                                    Loading More Posts
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • 1 / 1
                                    • First post
                                      Last post

                                    Looks like your connection to Vivaldi Forum was lost, please wait while we try to reconnect.

                                    Copyright © Vivaldi Technologies™ — All rights reserved. Privacy Policy | Code of conduct | Terms of use | Vivaldi Status