Skip to content

Commit

Permalink
Rework entire extension
Browse files Browse the repository at this point in the history
- Add a feature to remember last explicit container and open new tabs in
  that when the current tab is in the default "no" container.
- Change default shortcut to use two modifier keys to make it more
  reliable.

Signed-off-by: Roy-Orbison <Roy-Orbison@users.noreply.github.com>
  • Loading branch information
Roy-Orbison committed Aug 24, 2021
1 parent f100d6b commit 967bafa
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 86 deletions.
35 changes: 0 additions & 35 deletions CONTRIBUTING.md

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Mozilla Public License Version 2.0
means any form of the work other than Source Code Form.

1.7. "Larger Work"
means a work that combines Covered Software with other material, in
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.

1.8. "License"
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# New container tab
# <img width="24" height="24" src="icon.svg"> Retainer

Creates a new container tab based upon the current one when you use Alt+C.
Provides a keyboard shortcut (<kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>C</kbd> by default) to create a new tab that retains the current/last-used container.

You can edit the default shortcut under _Add-ons and Themes > Extensions > :gear: > Manage Extension Shortcuts > Retainer_.
New tabs can be restricted to the last-used container in the extension's preferences panel under *Add-ons and Themes > Extensions > Retainer > ⋯ > Preferences*.
116 changes: 101 additions & 15 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,103 @@
browser.commands.onCommand.addListener(function(command) {
if (command == "new-container-tab") {
createContainerTab();
}
});
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/
*
* © 2017 Jonathan Kingston
* © 2021 Roy Orbison
* */

async function createContainerTab() {
const tabs = await browser.tabs.query({
currentWindow: true,
active: true
});
const tab = tabs[0];
browser.tabs.create({
index: tab.index + 1,
cookieStoreId: tab.cookieStoreId
});
class WindowContainers {
set(tab) {
if (
tab.cookieStoreId
&& tab.windowId
&& tab.cookieStoreId !== containerNone
) {
this[tab.windowId] = tab.cookieStoreId;
}
}
}

const containers = new WindowContainers;
const containerNone = 'firefox-default';


browser.windows.onRemoved.addListener(windowId => delete containers[windowId]);

(async function() {
try {
const actives = await browser.tabs.query({
active: true
});
if (actives?.length) {
actives.forEach(async function(tab) {
if (tab.windowId && tab.cookieStoreId == containerNone) {
const others = await browser.tabs.query({
windowId: tab.windowId
});
if (others?.length) {
let counts = {},
stable = [];
others.forEach(tabOther => {
if (tabOther.cookieStoreId && tabOther.cookieStoreId != containerNone) {
stable[tabOther.index] = tabOther.cookieStoreId
if (counts[tabOther.cookieStoreId]) {
counts[tabOther.cookieStoreId].count++;
}
else {
counts[tabOther.cookieStoreId] = {
count: 1,
oneOf: tabOther
};
}
}
});
if (stable.length) {
stable = stable.reduce((containerLast, containerCurrent) =>
containerLast
&& (
!containerCurrent
|| counts[containerLast].count > counts[containerCurrent].count
) ?
containerLast :
containerCurrent);
tab = counts[stable].oneOf;
}
}
}
containers.set(tab);
});
}
}
catch (error) {
}
})();

browser.tabs.onActivated.addListener(async function(activeInfo) {
try {
const tab = await browser.tabs.get(activeInfo.tabId);
containers.set(tab);
}
catch (error) {
}
});

browser.commands.onCommand.addListener(async function(command) {
if (command === 'retainer-tab') {
try {
const tabs = await browser.tabs.query({
currentWindow: true,
active: true
});
if (tabs?.length) {
const exclKey = 'excludeDefault',
res = await browser.storage.sync.get(exclKey);
browser.tabs.create({
cookieStoreId: res && res[exclKey] && containers[tabs[0].windowId] || tabs[0].cookieStoreId
});
}
}
catch (error) {
}
}
});
11 changes: 11 additions & 0 deletions icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 0 additions & 13 deletions img/icon.svg

This file was deleted.

63 changes: 43 additions & 20 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
{
"name": "New container tab",
"version": "0.2.0",
"description": "Press Alt+C to create a new container tab.",
"author": "Jonathan Kingston",
"icons": {
"128": "img/icon.svg"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"cookies"
],
"commands": {
"new-container-tab": {
"suggested_key": { "default": "Alt+C" },
"description": "Create a new container tab"
}
},
"manifest_version": 2
"licence": "This Source Code Form is subject to the terms of the Mozilla Public License, v2.0. If a copy of the MPL was not distributed with this file, you can obtain one at https://mozilla.org/MPL/2.0/",
"copyright": "© 2017 Jonathan Kingston, © 2021 Roy Orbison",
"manifest_version": 2,
"name": "Retainer",
"author": "Roy Orbison",
"version": "0.1.0",
"description": "Provides a keyboard shortcut (Shift + Alt + C by default) to create a new tab that retains current/last-used container.",
"icons": {
"16": "icon.svg",
"32": "icon.svg",
"48": "icon.svg",
"96": "icon.svg",
"128": "icon.svg",
"512": "icon.svg"
},
"homepage_url": "https://github.com/Roy-Orbison/Retainer",
"browser_specific_settings": {
"gecko": {
"id": "retainer@roy-orbison.test",
"strict_min_version": "58.0a1"
}
},
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"tabs",
"storage",
"cookies"
],
"commands": {
"retainer-tab": {
"suggested_key": {
"default": "Shift+Alt+C"
},
"description": "New retained tab"
}
},
"options_ui": {
"page": "options.html"
}
}
40 changes: 40 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang=en>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v2.0. If a copy of the MPL was not distributed with this
- file, you can obtain one at https://mozilla.org/MPL/2.0/
-
- © 2021 Roy Orbison
-->
<head>
<meta charset=utf-8>
<title>Retainer options page</title>
<style type=text/css>
html {
height: 100%;
}
body {
min-height: 100%;
background-color: Menu;
color: MenuText;
}
label {
display: block;
margin: 1em;
}
@media (prefers-color-scheme: dark) {
body {
background-color: MenuText;
color: Menu;
}
}
</style>
</head>
<body>
<form>
<label><input type=checkbox id=exclude-default disabled>
Try to create tabs in the most recently used container if the current tab isn't in one.</label>
</form>
<script src=options.js></script>
</body>
</html>
27 changes: 27 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/
*
* © 2021 Roy Orbison
* */

const excludeCheckbox = document.querySelector('#exclude-default'),
exclKey = 'excludeDefault',
store = browser.storage.sync;
excludeCheckbox.indeterminate = true;

store.get(exclKey).then(res => {
excludeCheckbox.disabled = false;
excludeCheckbox.indeterminate = false;
excludeCheckbox.checked = !!(res && res[exclKey]);

excludeCheckbox.addEventListener('change', function() {
excludeCheckbox.indeterminate = true;
const upd = {
[exclKey]: excludeCheckbox.checked
};
store.set(upd)
.finally(() => excludeCheckbox.indeterminate = false)
.catch(() => excludeCheckbox.checked = !upd[exclKey]);
});
});

0 comments on commit 967bafa

Please sign in to comment.