Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: tuomassalo/tab-numbering
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: amoore17/tab-numbering
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 5 commits
  • 4 files changed
  • 1 contributor

Commits on Jan 17, 2020

  1. Copy the full SHA
    46c70ca View commit details
  2. Added strict mode

    amoore17 committed Jan 17, 2020
    Copy the full SHA
    9dd409a View commit details
  3. Copy the full SHA
    cf7112a View commit details
  4. Copy the full SHA
    5cb71ce View commit details

Commits on Jan 22, 2020

  1. Copy the full SHA
    509b3b2 View commit details
Showing with 93 additions and 45 deletions.
  1. +1 −0 LICENSE
  2. +0 −3 README.md
  3. +6 −1 manifest.json
  4. +86 −41 tab-numbering.js
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2017 Tuomas Salo
Copyright (c) 2020 Austin Moore

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,4 @@ This extension writes the tab number to the first eight tabs, the ones accessibl
## Known issues

- does not add numbers to pinned tabs, internal error pages, "new tab" pages or other special tabs

- does not keep in sync when dragging tabs to/from another window

- will mess up with titles already starting with the characters `¹`...``
7 changes: 6 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"browser_specific_settings": {
"gecko": {
"id": "{d4004003-765f-477d-be7b-0b15ff761585}"
}
},
"name": "Tab Numbering",
"description": "This extension writes the tab number to the first eight tabs, the ones accessible with ctrl/cmd + number",
"version" : "0.1.1",
"version" : "1.0.0",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
127 changes: 86 additions & 41 deletions tab-numbering.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,98 @@
const browser = window.browser || window.chrome
/*
* Title: tab-numbering.js
* Description: Numbers your tabs!
* Created by: Tuomas Salo
* Contributions by: Austin Moore
*/

var update = function(details) {
var oldTitle = details.title
var newTitle = oldTitle
'use strict';

if(!newTitle) {
return
}
const browser = window.browser || window.chrome;

var numbers = ['¹','²','³','⁴','⁵','⁶','⁷','⁸','⁹']
/*
* Function: update
* Description: Updates a tab to have the desired tab number
* Parameters: tab (tabs.Tab)
* - The current tab
* Returns: void
*/
const update = tab => {
const oldTitle = tab.title;
let newTitle = oldTitle;

if (newTitle && numbers.includes(newTitle[0])) {
newTitle = newTitle.substr(1)
}
if (!newTitle)
return;

if(details.index < 8) {
newTitle = numbers[details.index] + newTitle
}
if(oldTitle !== newTitle) {
const numbers = ['¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'];

// Take out one of these numbers if it already exists in the title
if (numbers.includes(newTitle[0]))
newTitle = newTitle.substring(1);

let tabCount = 9;

// If we are using Firefox
if (browser === window.browser)
tabCount = 8;

if (tab.index < tabCount)
newTitle = numbers[tab.index] + newTitle;

if (oldTitle !== newTitle) {
try {
browser.tabs.executeScript(
details.id,
tab.id,
{
code : `document.title = ${JSON.stringify(newTitle)}`
code: `document.title = ${JSON.stringify(newTitle)};`
}
)
console.log("executed: " + details.id)
);
console.log(`Executed: ${tab.id}`);
} catch(e) {
console.log("Tab numbering error:", e)
console.log('Tab numbering error:', e);
}
}
}

function updateAll() {
browser.tabs.query({}, function(tabs) {
tabs.forEach(update)
})
}

browser.tabs.onMoved.addListener(updateAll)
// firefox seems to do this inconsistently, thus this setTimeout kludge:
browser.tabs.onRemoved.addListener(() => {
updateAll()
setTimeout(updateAll, 100)
setTimeout(updateAll, 500)
setTimeout(updateAll, 1000)
})
browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
update(tab)
})

updateAll()
};

/*
* Function: updateAll
* Description: Updates all tabs to have the desired tab numbers
* Parameters: void
* Returns: void
*/
const updateAll = () => {
browser.tabs.query({}, tabs => {
tabs.forEach(update);
});
};

// Must listen for opening anchors in new tabs
browser.tabs.onCreated.addListener(updateAll);

// Must listen for tabs being attached from other windows
browser.tabs.onAttached.addListener(updateAll);

// Must listen for tabs being moved
browser.tabs.onMoved.addListener(updateAll);

// Must listen for tabs being removed
browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
/* Check that the tab has been removed every 100ms
Firefox fires onRemoved BEFORE it removes the tab */
const checkTabRemoval = () => {
browser.tabs.query({}, tabs => {
if (tabs.filter(tab => tab.id === tabId).length === 0)
updateAll();
else
setTimeout(checkTabRemoval, 100);
});
};

checkTabRemoval();
});

// Must listen for tab updates
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
update(tab);
});

updateAll();