Skip to content
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

add options for closing the least recently used tab and ignoring pinned tabs #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
31 changes: 27 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
const reactToNewTab = async tab => {
const tabs = await browser.tabs.query({})
const res = await browser.storage.local.get('tab-limit')
const limit = res['tab-limit'] || browser.runtime.getManifest().DEFAULT_TAB_LIMIT
if (tabs.length > limit) await browser.tabs.remove(tab.id)
const res = await browser.storage.local.get()
const limit = res['tab-limit'] || defaultOptions['tab-limit']
const which = res.which || defaultOptions.which
const ignorePinned = res.ignorePinned || defaultOptions.ignorePinned

const tabs = await browser.tabs.query({
currentWindow: true,
pinned: ignorePinned ? false : undefined
})

if (tabs.length > limit) {
switch(which) {
case 'newest': {
return browser.tabs.remove(tab.id)
}

case 'least-recently-used': {
const leastRecent = tabs.reduce(
(least, tab) => tab.lastAccessed < least.lastAccessed ? tab : least,
{lastAccessed: Infinity}
)

return browser.tabs.remove(leastRecent.id)
}
}

}
}

browser.tabs.onCreated.addListener(reactToNewTab)
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"background": {
"scripts": [
"options/default-options.js",
"background.js"
]
},
Expand All @@ -26,6 +27,5 @@
"tabs",
"storage"
],
"version": "0.1",
"DEFAULT_TAB_LIMIT": 6
"version": "0.1"
}
5 changes: 5 additions & 0 deletions options/default-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defaultOptions = {
'tab-limit': 6,
which: 'newest',
ignorePinned: false
}
28 changes: 23 additions & 5 deletions options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,30 @@
</head>

<body>
<form>
<label>Max Tab Limit</label>
<input type="number" id="tab-limit" class="browser-style">
<button id="save-button" class="browser-style">Save</button>
<button id="default-button" class="browser-style">Back to Default</button>
<form name="options">
<div class="panel-section panel-section-formElements">
<div class="panel-formElements-item">
<label for="tab-limit">Max Tab Limit</label>
<input type="number" min="1" name="tab-limit" id="tab-limit" class="browser-style">
</div>
<div class="panel-formElements-item">
<label for="which">Tab to close</label>
<select name="which" id="which" class="browser-style">
<option value="newest">Newest</option>
<option value="least-recently-used">Least-recently used</option>
</select>
</div>
<div class="panel-formElements-item">
<label for="ignorePinned">Ignore pinned</label>
<input type="checkbox" name="ignorePinned" id="ignorePinned" class="browser-style">
</div>
<div class="panel-formElements-item">
<button id="save-button" class="browser-style">Save</button>
<button id="default-button" class="browser-style">Back to Default</button>
</div>
</div>
</form>
<script src="default-options.js"></script>
<script src="options.js"></script>
</body>

Expand Down
22 changes: 15 additions & 7 deletions options/options.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
async function restoreOptions () {
const res = await browser.storage.local.get('tab-limit')
document.querySelector("#tab-limit").value = res['tab-limit'] || browser.runtime.getManifest().DEFAULT_TAB_LIMIT
async function restoreOptions (event) {
event.preventDefault()
const form = document.forms.options
const res = await browser.storage.local.get()

form.elements['tab-limit'].value = res['tab-limit'] || defaultOptions['tab-limit']
form.elements.which.value = res.which || defaultOptions.which
form.elements.ignorePinned.checked = res.ignorePinned || defaultOptions.ignorePinned
}

async function saveOptions () {
const newValue = document.querySelector("#tab-limit").value
async function saveOptions (event) {
event.preventDefault()
const form = document.forms.options
await browser.storage.local.set({
'tab-limit': (!isNaN(newValue) && newValue > 1) ? newValue : browser.runtime.getManifest().DEFAULT_TAB_LIMIT
'tab-limit': form.elements['tab-limit'].valueAsNumber || defaultOptions['tab-limit'],
which: form.elements.which.value || defaultOptions.which,
ignorePinned: form.elements.ignorePinned.checked || defaultOptions.ignorePinned
})
}

async function backToDefault () {
await browser.storage.local.remove('tab-limit')
await browser.storage.local.clear()
}

document.addEventListener('DOMContentLoaded', restoreOptions)
Expand Down