Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freeze multiple tabs at once via Context menu #4

Open
rayman89 opened this issue Nov 10, 2017 · 5 comments · May be fixed by #15
Open

Freeze multiple tabs at once via Context menu #4

rayman89 opened this issue Nov 10, 2017 · 5 comments · May be fixed by #15
Labels

Comments

@rayman89
Copy link

Could you consider adding to the context menu:
freeze other tabs (aka freeze all the tabs but current)
freeze tabs to the left
freeze tabs to the right
freeze tabs from this domain

@freddyb freddyb changed the title Context menu Freeze multiple tabs at once via Context menu Nov 10, 2017
@freddyb
Copy link
Owner

freddyb commented Nov 10, 2017

Thank you for this suggestion!

I suspect that freezing so many tabs might not have the desired outcome. The bulk discarding and then reloading might even have adverse facts. Especially given how Firefox already throttles background tasks in inactive tabs.

Limiting a specific site makes sense to me, so I'll consider the last suggestion (all for domain/origin) first.

I will also accept pull requests that implement this behavior!

Reminder to self: Figure out what happens to ServiceWorker client handles and opener relationships when a tab is discarded.

@freddyb
Copy link
Owner

freddyb commented Nov 10, 2017

I will also accept pull requests that implement this behavior!

This sounds straightforward, but someone picking this up should consult me on an implementation plan in this issue before submitting a pull request.

@freddyb
Copy link
Owner

freddyb commented May 6, 2020

I have revisited my decision from 2017 and will no longer expect this to be implemented. I want to keep the extension simple and minimize the permissions required to use this add-on. Working for multiple tabs requires extra permissions, so I'm not going to allow it. Sorry if this is inconvenient for you. I totally understand. Please try using another "freeze tabs" add-on from addons.mozilla.org but you're also welcome to fork under a different name as long as you're in compliance with the license :)

@kevincox
Copy link

When selecting multiple tabs it is surprising that only one of them is discarded. Do you think this case could be supported? It requires no more UI (I don't know about more permissions).

@Nothing4You
Copy link

Nothing4You commented Dec 25, 2023

When selecting multiple tabs it is surprising that only one of them is discarded. Do you think this case could be supported?

This was also striking me as off, but it seems the API used for this, browser.menus.onClicked, just doesn't expose this information to an extension.
The callback for the menu item click only contains the tab that the menu was selected on.

It is however fairly simple to determine selected tabs or even all tabs in the active window using browser.tabs.query():

// get discardable tabs in active window
await browser.tabs.query({windowId: tab.windowId, active: false, discarded: false})

// get discardable selected tabs in active window
await browser.tabs.query({windowId: tab.windowId, active: false, discarded: false, highlighted: true})

This doesn't seem to require extra permissions either, at least when loading the unpacked extension as temporary addon.
According to the documentation, this only requires the tabs permission, which this extension already has.

The primary downside of this is that once you have more than one menu item it will move them all into a submenu, grouped by extension.

It's still possible to at least fix the case of discarding all selected tabs without introducing additional menu options:

diff --git a/background.js b/background.js
index f4866fc..1aa41d8 100644
--- a/background.js
+++ b/background.js
@@ -22,7 +22,7 @@ browser.menus.create({
   contexts: ["tab"]
 });
 
-browser.menus.onClicked.addListener(async (info, tab) => {
+const discardTab = async (tab) => {
   if (tab.discarded) {
     return;
   }
@@ -39,6 +39,18 @@ browser.menus.onClicked.addListener(async (info, tab) => {
     }
   }
   browser.tabs.discard(tab.id);
+}
+
+browser.menus.onClicked.addListener(async (info, tab) => {
+  // A highlighted tab is a selected tab.
+  // If the tab is not highlighted, the user clicked on a tab that is neither selected nor the active tab.
+  // If the tab is highlighted it might be the active tab or one of multiple selected tabs.
+  if (tab.highlighted) {
+    const tabs = await browser.tabs.query({windowId: tab.windowId, active: false, discarded: false, highlighted: true});
+    tabs.forEach(async (it) => await discardTab(it));
+  } else {
+    discardTab(tab);
+  }
 });
 
 browser.storage.onChanged.addListener((changes) => {

Created #15 for this.

Nothing4You added a commit to Nothing4You/webext-discard-tabs that referenced this issue Dec 25, 2023
@Nothing4You Nothing4You linked a pull request Dec 25, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants