1
0
Fork 0

Adding upstream version 0.4.1.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-31 14:47:36 +02:00
parent 956119c6fd
commit 5988d8b9b6
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
47 changed files with 8483 additions and 0 deletions

222
src/background.js Normal file
View file

@ -0,0 +1,222 @@
/* src/background.js
* Originally created 3/10/2017 by DaAwesomeP
* This is the background task file of the extension
* https://github.com/DaAwesomeP/tab-counter
*
* Copyright 2017-present DaAwesomeP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { debounce } from 'underscore'
const updateIcon = async function updateIcon () {
// Get settings
let settings = await browser.storage.local.get()
// Get tab counter setting
let counterPreference = settings.counter || 0
// Stop tab badge update if badge disabled
if (counterPreference === 3) return
// Get current tab to update badge in
let currentTab = (await browser.tabs.query({ currentWindow: true, active: true }))[0]
// Get tabs in current window, tabs in all windows, and the number of windows
let currentWindow = (await browser.tabs.query({ currentWindow: true })).length.toString()
let allTabs = (await browser.tabs.query({})).length.toString()
let allWindows = (await browser.windows.getAll({ populate: false, windowTypes: ['normal'] })).length.toString()
if (typeof currentTab !== 'undefined') {
let text
if (counterPreference === 0) text = currentWindow // Badge shows current window
else if (counterPreference === 1) text = allTabs // Badge shows total of all windows
else if (counterPreference === 2) text = `${currentWindow}/${allTabs}` // Badge shows both (Firefox limits to about 4 characters based on width)
else if (counterPreference === 4) text = allWindows // Badge shows total of all windows
// Update the badge
browser.browserAction.setBadgeText({
text: text,
tabId: currentTab.id
})
// Update the tooltip
browser.browserAction.setTitle({
title: `Tab Counter\nTabs in this window: ${currentWindow}\nTabs in all windows: ${allTabs}\nNumber of windows: ${allWindows}`,
tabId: currentTab.id
})
}
}
// Prevent from firing too frequently or flooding at a window or restore
const lazyUpdateIcon = debounce(updateIcon, 250)
// Prioritize active leading edge of every 1 second on tab switch (fluid update for new tabs)
const lazyActivateUpdateIcon = debounce(updateIcon, 1000, { leading: true })
// Will be error if tab has been removed, so wait 150ms;
// onActivated fires slightly before onRemoved,
// but tab is gone during onActivated.
// Must be a function to avoid event parameter errors
const update = function update () { setTimeout(lazyUpdateIcon, 150) }
// Init badge for when addon starts and not yet loaded tabs
browser.browserAction.setBadgeText({ text: 'wait' })
browser.browserAction.setBadgeBackgroundColor({ color: '#000000' })
// Handler for when current tab changes
const tabOnActivatedHandler = function tabOnActivatedHandler () {
// Run normal update for most events
update()
// Prioritize active (fluid update for new tabs)
lazyActivateUpdateIcon()
}
// Load and apply icon and badge color settings
const checkSettings = async function checkSettings (settingsUpdate) {
// Get settings object
let settings = await browser.storage.local.get()
// Get the browser name and version
let browserInfo
if (browser.runtime.hasOwnProperty('getBrowserInfo')) browserInfo = await browser.runtime.getBrowserInfo()
else {
browserInfo = { // polyfill doesn't seem to support this method, but we're only concerned with FF at the moment
version: '0',
vendor: '',
name: ''
}
}
const browserVersionSplit = browserInfo.version.split('.').map((n) => parseInt(n))
// Set base defaults if new insall
if (!settings.hasOwnProperty('version')) {
settings = {
version: '0.0.0',
icon: 'tabcounter.plain.min.svg',
counter: 0,
badgeColor: '#999999'
}
}
// Perform settings upgrade
if (settings.version !== browser.runtime.getManifest().version) {
let versionSplit = settings.version.split('.').map((n) => parseInt(n))
// Upgrade
// since v0.3.0, icons now adapt to theme so reset icon setting
if (versionSplit[0] === 0 && versionSplit[1] < 3) settings.icon = 'tabcounter.plain.min.svg'
// disable the "both" counter option in version v0.3.0 due to the four-character badge limit (renders the feature uselss)
if (versionSplit[0] === 0 && versionSplit[1] < 3) {
if (settings.hasOwnProperty('counter')) {
if (settings.counter === 2) settings.counter = 0
}
}
// add badgeTextColor support if at least v0.4.0 and FF 63
if (versionSplit[0] === 0 && versionSplit[1] < 4 && browserInfo.vendor === 'Mozilla' && browserInfo.name === 'Firefox' && browserVersionSplit[0] >= 63) {
settings.badgeTextColorAuto = true
settings.badgeTextColor = '#000000'
}
}
browser.storage.local.set(Object.assign(settings, {
version: browser.runtime.getManifest().version
}))
// Apply badge color or use default
if (settings.hasOwnProperty('badgeColor')) browser.browserAction.setBadgeBackgroundColor({ color: settings.badgeColor })
else browser.browserAction.setBadgeBackgroundColor({ color: '#000000' })
// Apply badge text color or use default if not set or not supported
if (settings.hasOwnProperty('badgeTextColor')) {
if (settings.badgeTextColorAuto !== true) browser.browserAction.setBadgeTextColor({ color: settings.badgeTextColor })
else browser.browserAction.setBadgeTextColor({ color: null })
}
// Apply icon selection or use default
if (settings.hasOwnProperty('icon')) browser.browserAction.setIcon({ path: `icons/${settings.icon}` })
else browser.browserAction.setIcon({ path: 'icons/tabcounter.plain.min.svg' })
// Get counter preference
let counterPreference
if (!settings.hasOwnProperty('counter')) counterPreference = 0
else counterPreference = settings.counter
// Either add badge update events or don't if not set to
if (counterPreference !== 3) {
// Watch for tab and window events five seconds after browser startup
setTimeout(() => {
browser.tabs.onActivated.addListener(tabOnActivatedHandler)
browser.tabs.onAttached.addListener(update)
browser.tabs.onCreated.addListener(update)
browser.tabs.onDetached.addListener(update)
browser.tabs.onMoved.addListener(update)
browser.tabs.onReplaced.addListener(update)
browser.tabs.onRemoved.addListener(update)
browser.tabs.onUpdated.addListener(update)
browser.windows.onCreated.addListener(update)
browser.windows.onRemoved.addListener(update)
browser.windows.onFocusChanged.addListener(update)
}, settingsUpdate ? 1 : 5000) // add listeners immeadietly if not browser startup
} else {
// remove the listeners that were added
browser.tabs.onActivated.removeListener(tabOnActivatedHandler)
browser.tabs.onAttached.removeListener(update)
browser.tabs.onCreated.removeListener(update)
browser.tabs.onDetached.removeListener(update)
browser.tabs.onMoved.removeListener(update)
browser.tabs.onReplaced.removeListener(update)
browser.tabs.onRemoved.removeListener(update)
browser.tabs.onUpdated.removeListener(update)
browser.windows.onCreated.removeListener(update)
browser.windows.onRemoved.removeListener(update)
browser.windows.onFocusChanged.removeListener(update)
// hide the "wait" badge if set not to show a badge
browser.browserAction.setBadgeText({ text: '' })
browser.browserAction.setTitle({ title: 'Tab Counter' })
// check each tab that was overriden with a counter badge
let allTabs = await browser.tabs.query({})
allTabs.forEach((tab) => {
browser.browserAction.setBadgeText({
text: '',
tabId: tab.id
})
browser.browserAction.setTitle({
title: 'Tab Counter',
tabId: tab.id
})
})
}
}
// Load settings and update badge at app start
const applyAll = async function applyAll (settingsUpdate) {
await checkSettings(settingsUpdate) // Icon and badge color
await update() // Badge text options
}
applyAll()
// Listen for settings changes and update color, icon, and badge text instantly
// Bug: this listener run nonstop
// browser.storage.onChanged.addListener(applyAll)
// Listen for internal addon messages
const messageHandler = async function messageHandler (request, sender, sendResponse) {
// Check for a settings update
if (request.hasOwnProperty('updateSettings')) if (request.updateSettings) applyAll(true)
}
browser.runtime.onMessage.addListener(messageHandler)

88
src/options.html Normal file
View file

@ -0,0 +1,88 @@
<!--
* src/options.html
* Originally created 3/11/2017 by DaAwesomeP
* This is the options page markup file
* https://github.com/DaAwesomeP/tab-counter
*
* Copyright 2017-present DaAwesomeP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tab Counter Options</title>
<style type="text/css">
html {
font-family: sans-serif;
}
tr {
margin-bottom: 5px;
}
td {
min-width: 400px !important;
}
</style>
</head>
<body>
<form>
<table>
<tr style="display: none">
<td>Badge Color</td>
<td><input type="color" name="badgeColor" id="badgeColor" optionType="string" /></td>
<td><em>Unfortuantely, the browser predetermines thefont size of the badge. Setting the badge text color is available in Firefox 63 and above.</em></td>
</tr>
<tr style="display: none">
<td>Automatically set Badge Color</td>
<td><input type="checkbox" name="badgeTextColorAuto" id="badgeTextColorAuto" optionType="boolean" /></td>
<td><em>When checked, the text color is either white or black based on the highest constrast with the badge color. When unchecked, a manual color may be selected.</em></td>
</tr>
<tr style="display: none">
<td>Badge Text Color (if not automatic)</td>
<td><input type="color" name="badgeTextColor" id="badgeTextColor" optionType="string" /></td>
<td><em>This option is ignored/disabled unless "Automatically set Badge Color" is unchecked.</em></td>
</tr>
<tr style="display: none">
<td>Icon Color &amp; Style</td>
<td>
<select name="icon" id="icon" optionType="string" >
<option value="tabcounter.plain.min.svg">Tab Counter Default</option>
<option value="tabcounter-gray90.plain.min.svg">Tab Counter Gray 90 (Firefox Photon Design System Light Background Icon Color)</option>
<option value="tabcounter-gray10.plain.min.svg">Tab Counter Gray 10 (Firefox Photon Design System Dark Background Icon Color)</option>
<option value="tabcounter-black.plain.min.svg">Tab Counter Black</option>
<option value="tabcounter-white.plain.min.svg">Tab Counter White</option>
<option value="clear.plain.min.svg">Clear SVG for Firefox (shows badge only)</option>
<option value="clear-1.png">Clear 1px PNG for Opera (shows badge only)</option>
</select>
</td>
</tr>
<tr style="display: none">
<td>Counter</td>
<td>
<select name="counter" id="counter" optionType="number">
<option value="0">Number of Tabs in Current Window</option>
<option value="1">Total Number of Tabs of All Windows</option>
<option value="2">Both Number of Tabs in Current Window/Total Number of Tabs of All Windows</option>
<option value="4">Total Number of Windows</option>
<option value="3">None (disables the counter and hover text; click the icon to see the count)</option>
</select>
</td>
<td><em>A maximum of four characters can be displayed on the badge; this is a browser limitation.</em></td>
</tr>
<tr>
</table>
</form>
<script type="text/javascript" src="options.js"></script>
</body>
</html>

83
src/options.js Normal file
View file

@ -0,0 +1,83 @@
/* src/options.js
* Originally created 3/11/2017 by DaAwesomeP
* This is the options page script file
* https://github.com/DaAwesomeP/tab-counter
*
* Copyright 2017-present DaAwesomeP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var domReady = false
var browserReady = false
var restored = false
async function checkBadgeColorManualSetting () {
let autoSelect = document.querySelector('#badgeTextColorAuto').checked
document.querySelector('#badgeTextColor').disabled = autoSelect
}
async function saveOptions () {
checkBadgeColorManualSetting()
let settings = await browser.storage.local.get()
for (let setting in settings) {
if (setting !== 'version') {
let el = document.querySelector(`#${setting}`)
if (el.getAttribute('type') === 'checkbox') settings[setting] = el.checked
else settings[setting] = el.value
let optionType = el.getAttribute('optionType')
if (optionType === 'number' && typeof settings[setting] !== 'number') settings[setting] = parseInt(settings[setting])
else if (optionType === 'string' && typeof settings[setting] !== 'string') settings[setting] = settings[setting].toString()
else if (optionType === 'boolean' && typeof settings[setting] !== 'boolean') settings[setting] = (settings[setting].toLowerCase() === 'true')
}
}
browser.storage.local.set(settings)
await browser.runtime.sendMessage({ updateSettings: true })
}
async function restoreOptions () {
restored = true
let settings = await browser.storage.local.get()
for (let setting in settings) {
if (setting !== 'version') {
let el = document.querySelector(`#${setting}`)
if (el.getAttribute('type') === 'checkbox') el.checked = settings[setting]
else el.value = settings[setting]
el.parentElement.parentElement.style.display = 'block'
}
}
checkBadgeColorManualSetting()
}
function start () {
browserReady = true
if (domReady && !restored) restoreOptions()
for (let el of document.querySelectorAll('input, select')) {
el.addEventListener('change', saveOptions)
}
}
document.addEventListener('DOMContentLoaded', () => {
domReady = true
if (browserReady && !restored) restoreOptions()
})
if (typeof browser === 'undefined') {
var script = document.createElement('script')
script.addEventListener('load', () => {
start()
})
script.src = '../node_modules/webextension-polyfill/dist/browser-polyfill.js'
script.async = false
document.head.appendChild(script)
} else start()

55
src/popup.html Normal file
View file

@ -0,0 +1,55 @@
<!--
* src/popup.html
* Originally created 3/11/2017 by DaAwesomeP
* This is the popup markup file
* https://github.com/DaAwesomeP/tab-counter
*
* Copyright 2017-present DaAwesomeP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
padding: 10px;
}
h3 {
width: 100%;
text-align: center;
margin: 0px;
border-bottom: 1px solid black;
}
</style>
</head>
<body>
<h3>Tab Counter</h3>
<table>
<tr>
<td><strong>Tabs in this window:</strong></td>
<td><span id="currentWindow"></span></td>
</tr>
<tr>
<td><strong>Tabs in all windows:</strong></td>
<td><span id="allTabs"></span></td>
</tr>
<tr>
<td><strong>Number of windows:</strong></td>
<td><span id="allWindows"></span></td>
</tr>
</table>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>

38
src/popup.js Normal file
View file

@ -0,0 +1,38 @@
/* src/popup.js
* Originally created 3/10/2017 by DaAwesomeP
* This is the popup script file
* https://github.com/DaAwesomeP/tab-counter
*
* Copyright 2017-present DaAwesomeP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
async function start () {
let currentWindow = (await browser.tabs.query({ currentWindow: true })).length
let allTabs = (await browser.tabs.query({})).length
let allWindows = (await browser.windows.getAll({ populate: false, windowTypes: ['normal'] })).length.toString()
document.getElementById('currentWindow').textContent = currentWindow
document.getElementById('allTabs').textContent = allTabs
document.getElementById('allWindows').textContent = allWindows
}
if (typeof browser === 'undefined') {
var script = document.createElement('script')
script.addEventListener('load', () => {
start()
})
script.src = '../node_modules/webextension-polyfill/dist/browser-polyfill.js'
script.async = false
document.head.appendChild(script)
} else start()