forked from tuomassalo/tab-numbering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab-numbering.js
53 lines (45 loc) · 1.15 KB
/
tab-numbering.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const browser = window.browser || window.chrome
var update = function(details) {
var oldTitle = details.title
var newTitle = oldTitle
if(!newTitle) {
return
}
var numbers = ['¹','²','³','⁴','⁵','⁶','⁷','⁸','⁹']
if (newTitle && numbers.includes(newTitle[0])) {
newTitle = newTitle.substr(1)
}
if(details.index < 8) {
newTitle = numbers[details.index] + newTitle
}
if(oldTitle !== newTitle) {
try {
browser.tabs.executeScript(
details.id,
{
code : `document.title = ${JSON.stringify(newTitle)}`
}
)
console.log("executed: " + details.id)
} catch(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()