-
Notifications
You must be signed in to change notification settings - Fork 15
Implement menu and functionality to discard tabs, fixes #11. #33
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
Open
sfleiter
wants to merge
1
commit into
autonome:master
Choose a base branch
from
sfleiter:add-menu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,22 +45,23 @@ | |
| return config.excludedWebsites.value.some(w => w.length && url.startsWith(w)); | ||
| } | ||
|
|
||
| // Check for tabs that have hit dormanticizable age and dormanticize them. | ||
| const tabQueryOptions = { | ||
| // keep tabs that play sound awake | ||
| audible: false, | ||
| // discarded tabs are already asleep, ignore them | ||
| discarded: false, | ||
| // keep pinned tabs awake for their special role | ||
| pinned: false, | ||
| } | ||
|
|
||
| // Check for tabs that have hit sleeping age and put them to sleep. | ||
| async function periodicTabCheck() { | ||
| // Query for the active tab | ||
| let activeWindow = await browser.windows.getCurrent(); | ||
| let activeWindowId = activeWindow.id; | ||
|
|
||
| // Query for all tabs that are not the active tab | ||
| let tabs = await browser.tabs.query({ | ||
| pinned: false, | ||
| // only sleep if isn't the active tab | ||
| active: false, | ||
| // only sleep if not already asleep | ||
| discarded: false, | ||
| // do not sleep tabs that play sound | ||
| audible: false | ||
| }); | ||
| let tabs = await browser.tabs.query({ ...tabQueryOptions, active: false }); | ||
|
|
||
| for (let i in tabs) { | ||
| let tab = tabs[i]; | ||
|
|
@@ -87,12 +88,88 @@ | |
| } | ||
| }); | ||
|
|
||
| function addMenu() { | ||
|
|
||
| async function discardTabs(tabs) { | ||
| tabs.forEach(tab => { | ||
| if (!tab.active) { | ||
| browser.tabs.discard(tab.id); | ||
| } | ||
| }); | ||
| } | ||
| async function discardSelected() { | ||
| let tabs = await browser.tabs.query({ ...tabQueryOptions, currentWindow: true, highlighted: true }); | ||
| discardTabs(tabs); | ||
| }; | ||
| async function getCurrentWindowsTabs() { | ||
| return await browser.tabs.query({ ...tabQueryOptions, currentWindow: true }); | ||
| } | ||
| async function discardPositional(position) { | ||
| let tabs = await getCurrentWindowsTabs(); | ||
| const toDiscard = [] | ||
| if (position != 'left') { | ||
| tabs = tabs.reverse(); | ||
| } | ||
| for (let i = 0; !tabs[i].active && i < tabs.length; i++) { | ||
| toDiscard.push(tabs[i]) | ||
| } | ||
| discardTabs(toDiscard); | ||
| }; | ||
| async function discardLeft() { | ||
| discardPositional('left'); | ||
| } | ||
| async function discardRight() { | ||
| discardPositional('right'); | ||
| } | ||
| async function discardOther() { | ||
| let tabs = await getCurrentWindowsTabs(); | ||
| discardTabs(tabs); | ||
| } | ||
|
|
||
| browser.menus.remove('dormancy-menu'); | ||
| const topMenuId = browser.menus.create({ | ||
| id: 'dormancy-menu', | ||
| title: browser.i18n.getMessage('menuGroup'), | ||
| icons: { | ||
| '96': 'icon-96.png' | ||
| }, | ||
| contexts: ['tab'] | ||
| }); | ||
| browser.menus.create({ | ||
| id: 'dormancy-menu-discard-selected', | ||
| parentId: topMenuId, | ||
| title: browser.i18n.getMessage('menuDiscardSelected'), | ||
| onclick: discardSelected | ||
| }); | ||
| browser.menus.create({ | ||
| id: 'dormancy-menu-discard-left', | ||
| parentId: topMenuId, | ||
| title: browser.i18n.getMessage('menuDiscardLeft'), | ||
| onclick: discardLeft | ||
| }); | ||
| browser.menus.create({ | ||
| id: 'dormancy-menu-discard-right', | ||
| parentId: topMenuId, | ||
| title: browser.i18n.getMessage('menuDiscardRight'), | ||
| onclick: discardRight | ||
| }); | ||
| browser.menus.create({ | ||
| id: 'dormancy-menu-discard-other', | ||
| parentId: topMenuId, | ||
| title: browser.i18n.getMessage('menuDiscardOther'), | ||
| onclick: discardOther | ||
| }); | ||
| }; | ||
|
|
||
| // Start everything. Or cancel what's going on and restart. | ||
| async function init() { | ||
| // Load (or reload) config from storage | ||
| let oldConfig = config; | ||
| config = await loadConfig(); | ||
|
|
||
| // add a context menu to tab-strip to put tabs to sleep manually | ||
| addMenu(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor nit: add a space between this and the config loading bit, with a comment explaining what's happening
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do |
||
|
|
||
| // Reset timer if timeout value changed | ||
| if (!timerId || (oldConfig.timeout.value && (config.timeout.value != oldConfig.timeout.value))) { | ||
| if (timerId) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.