forked from jonathanKingston/new-container-tab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
f100d6b
commit 967bafa
Showing
9 changed files
with
228 additions
and
86 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -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*. |
This file contains 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 |
---|---|---|
@@ -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) { | ||
} | ||
} | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -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> |
This file contains 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 |
---|---|---|
@@ -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]); | ||
}); | ||
}); |