luetage / vivaldi_modding

Custom modifications for Vivaldi web browser.
MIT License
80 stars 5 forks source link

Keep in the current tab stack after Close tab #10

Open Ashark opened 1 year ago

Ashark commented 1 year ago

Hi. Is it possible to manually keep the activated tab from the list of current tab stack when closing a tab? The problem is reported in this forum thread. Maybe this can be done with some mod? Is there a way to list/switch tabs in javascript based on a stack it belongs?

luetage commented 1 year ago

I’ve never been a fan of stacks. And they are a an outlier from a coding standpoint, because they aren’t proper tabs anymore. Had to deal with them in the “activate tab on hover” mod in the past and had to give up on supporting them. From the top of my head I don’t know a solution to this.

Ashark commented 1 year ago

Thanks for comment. It's sad. This is one of major vivaldi frustrations.

Ashark commented 10 months ago

I have an idea on how to implement this.

Getting the current tab (its id and its stack id):

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
  var activeTab = tabs[0];
  var activeTabvivExtData = activeTab.vivExtData;
  var jr = JSON.parse(activeTabvivExtData);
  var activeTabStackId = jr["group"];
  console.log(activeTabStackId);
});

Getting the dictionary of stack ids with list of their tabs:

chrome.tabs.query({ currentWindow: true }, function (tabs) {
    var groups = {};
    for (const tab of tabs) {
        tab_group = JSON.parse(tab.vivExtData)["group"];
        if (!groups[tab_group]) {
            groups[tab_group] = [];
        }
        groups[tab_group].push(tab);
    }
    console.log(groups);
});

Switching to another tab:

chrome.tabs.update(tabId_here, {active: true});

Now I want my mod.js to catch the tab close event. I tried this two functions in console, and they worked:

    chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
    myFunction(tabId);
    });

    function myFunction(tabId) {
    console.log("Tab closed: " + tabId);
    }

But how to run that in a mod.js? I tried surrounding that with

 (function () {
 ...
 })();

as I saw in your other mods. But it does not logs tab closing in console. What I am doing wrong?

luetage commented 10 months ago

Could be this only works from an extension. Put a console log after the mod has loaded, then you know whether your mod loads, surrounding with a function is not enough, you have to initiate somehow.

Ashark commented 10 months ago

You were right that the mod was not activated. I tried your mod “activate tab on hover” on Arch Linux with the vivaldi-autoinject-custom-js-ui, and it is not currently updated to modify window.html instead of browser.html. So I moved the <script src="activate-tab-on-hover.js"></script> to window.html and it worked. Also, I placed my <script src="keep-in-stack-at-tab-close.js"></script> there. It is loaded now.

Put a console log after the mod has loaded, then you know whether your mod loads

I put the following to the keep-in-stack-at-tab-close.js file:

(function () {
    console.log("I am loaded");

    chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
        myFunction(tabId);
    });

    function myFunction(tabId) {
        console.log("Tab closed: " + tabId);
    }
})();

And now I can see that "I am loaded" in log messages. But I cannot see the logs at tab closings.

luetage commented 10 months ago

Then it probably doesn’t work.

Ashark commented 10 months ago

Is it possible to access chrome.tabs from ViolentMonkey script? Looks like no. Looks like the only solution is the browser extension.

budRich commented 10 months ago

just wanted to say that vivaldi-autoinject-custom-js-ui has been updated.