Skip to content

Commit

Permalink
ES6 syntax, comments, listen for tab creation added. LICENSE and mani…
Browse files Browse the repository at this point in the history
…fest.json modified
  • Loading branch information
amoore17 committed Jan 17, 2020
1 parent 72390c3 commit 46c70ca
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 39 deletions.
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
Expand Down
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://*/*"
],
Expand Down
100 changes: 62 additions & 38 deletions tab-numbering.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,77 @@
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
const browser = window.browser || window.chrome;

if(!newTitle) {
return
}
/*
* 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;

var numbers = ['¹','²','³','⁴','⁵','⁶','⁷','⁸','⁹']
if (!newTitle)
return;

if (newTitle && numbers.includes(newTitle[0])) {
newTitle = newTitle.substr(1)
}
const numbers = ['¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸'];

if(details.index < 8) {
newTitle = numbers[details.index] + newTitle
}
if(oldTitle !== newTitle) {
// Take out one of these numbers if it already exists in the title
if (numbers.includes(newTitle[0]))
newTitle = newTitle.substring(1);

if (tab.index < 8)
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)
})
}
/*
* 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);
});
};

browser.tabs.onMoved.addListener(updateAll)
// firefox seems to do this inconsistently, thus this setTimeout kludge:
// Must listen for opening anchors in new tabs
browser.tabs.onCreated.addListener(updateAll);

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()
updateAll();
setTimeout(updateAll, 100);
setTimeout(updateAll, 500);
setTimeout(updateAll, 1000);
});

browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
update(tab);
});

updateAll();

0 comments on commit 46c70ca

Please sign in to comment.