Skip to content

Commit

Permalink
Add an extra restriction preference
Browse files Browse the repository at this point in the history
Enables one to avoid creating uncontained tabs.

Signed-off-by: Roy-Orbison <[email protected]>
  • Loading branch information
Roy-Orbison committed Nov 1, 2021
1 parent 967bafa commit 406db9e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
14 changes: 9 additions & 5 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ browser.commands.onCommand.addListener(async function(command) {
currentWindow: true,
active: true
});
if (tabs?.length) {
if (tabs?.length && tabs[0].id != browser.tabs.TAB_ID_NONE) {
const exclKey = 'excludeDefault',
res = await browser.storage.sync.get(exclKey);
browser.tabs.create({
cookieStoreId: res && res[exclKey] && containers[tabs[0].windowId] || tabs[0].cookieStoreId
});
res = await browser.storage.sync.get(exclKey),
excl = +(res && res[exclKey] || 0),
mruCookieStoreId = containers[tabs[0].windowId];
if (excl < 2 || mruCookieStoreId) {
browser.tabs.create({
cookieStoreId: excl && mruCookieStoreId || tabs[0].cookieStoreId
});
}
}
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"manifest_version": 2,
"name": "Retainer",
"author": "Roy Orbison",
"version": "0.1.0",
"version": "0.2.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",
Expand Down
10 changes: 7 additions & 3 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
</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 name=prefs>
<label><input type=radio name=restriction value=0 disabled>
Create in the same container (or not) as the current tab.</label>
<label><input type=radio name=restriction value=1 disabled>
Try to create tabs in the most recently used container, or fall back to no container.</label>
<label><input type=radio name=restriction value=2 disabled>
Never create uncontained tabs.</label>
</form>
<script src=options.js></script>
</body>
Expand Down
26 changes: 13 additions & 13 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
* © 2021 Roy Orbison
* */

const excludeCheckbox = document.querySelector('#exclude-default'),
const excludeRadio = document.forms.prefs.elements.restriction,
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]);
excludeRadio.forEach(option => {
option.disabled = false;
option.checked = option.value == +(res && res[exclKey] || 0);

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

0 comments on commit 406db9e

Please sign in to comment.