Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions _locales/de/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,25 @@
"optionActiveWindow": {
"message": "Halte das aktive Fenster am Leben",
"description": "Label for option for whether to put tabs to sleep in the active window."
},
"menuGroup": {
"message": "Dormancy - 'Einschläfern' von Tabs",
"description": "Menu group for the extension at tab context."
},
"menuDiscardSelected": {
"message": "Au&sgewählte Tabs 'einschläfern'",
"description": "Menu item to put all selected / highlighted tabs to sleep."
},
"menuDiscardLeft": {
"message": "Tabs &links 'einschläfern'",
"description": "Menu item to put tabs to the left in the same window to sleep."
},
"menuDiscardRight": {
"message": "Tabs &rechts 'einschläfern'",
"description": "Menu item to put tabs to the right in the same window to sleep."
},
"menuDiscardOther": {
"message": "&Andere Tabs im Fenster 'einschläfern'",
"description": "Menu item to put all other tabs in the same window to sleep."
}
}
20 changes: 20 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,25 @@
"optionExcludedWebsites": {
"message": "Keep these URLs alive (separated by commas)",
"description": "Comma separated list of website URLs."
},
"menuGroup": {
"message": "Dormancy - Put tabs to sleep",
"description": "Menu group for the extension at tab context."
},
"menuDiscardSelected": {
"message": "Put &selected tabs to sleep",
"description": "Menu item to put all selected / highlighted tabs to sleep."
},
"menuDiscardLeft": {
"message": "Put tabs to the &left to sleep",
"description": "Menu item to put tabs to the left in the same window to sleep."
},
"menuDiscardRight": {
"message": "Put tabs to the &right to sleep",
"description": "Menu item to put tabs to the right in the same window to sleep."
},
"menuDiscardOther": {
"message": "Put &other tabs of window to sleep",
"description": "Menu item to put all other tabs in the same window to sleep."
}
}
97 changes: 87 additions & 10 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Comment thread
sfleiter marked this conversation as resolved.
// 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];
Expand All @@ -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();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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) {
Expand Down
6 changes: 5 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "3.4.3",
"icons": {
"96": "icon-96.png"
},
"version": "3.5.0",
"manifest_version": 2,
"default_locale": "en",

Expand All @@ -25,6 +28,7 @@
},

"permissions": [
"menus",
"sessions",
"storage",
"tabs"
Expand Down