-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabCount.js
More file actions
87 lines (80 loc) · 2.44 KB
/
Copy pathtabCount.js
File metadata and controls
87 lines (80 loc) · 2.44 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
const TST_ID = 'treestyletab@piro.sakura.ne.jp';
async function registerToTST() {
try {
const result = await browser.runtime.sendMessage(TST_ID, {
type: 'register-self',
name: browser.i18n.getMessage('tst-tabcount'),
icons: browser.runtime.getManifest().icons,
listeningTypes: [
'sidebar-show',
'new-tab-processed',
'tabs-rendered',
'tree-attached',
'tree-detached',
],
allowBulkMessaging: true,
/* style "new tab" button: hide +, show text */
style: `
.newtab-button {
height: 38px;
}
.newtab-button::after {
display: none;
}
.newtab-button .extra-items-container {
display: flex;
justify-content: center;
align-items: center;
}
`,
permissions: [],
});
}
catch(_error) {
}
}
function onMessageExternal(message, sender) {
if (!(message && sender.id === TST_ID)) return;
if (message.messages) {
for (const oneMessage of message.messages) {
onMessageExternal(oneMessage, sender);
}
}
switch (message.type) {
case 'permissions-changed':
registerToTST();
break;
case 'ready':
registerToTST();
updateTabCount();
break;
case 'sidebar-show':
case 'new-tab-processed':
case 'tabs-rendered':
case 'tree-attached':
case 'tree-detached':
updateTabCount();
}
}
async function updateTabCount() {
const tabCounts = []
for (const window of await browser.windows.getAll()) {
const tabs = await browser.runtime.sendMessage(TST_ID, {
type: 'get-light-tree',
window: window.id,
tabs: '*', // flatten tabs
});
browser.runtime.sendMessage(TST_ID, {
type: 'set-extra-contents',
place: 'new-tab-button',
contents: `<span id="tst-tab-count-${window.id}">${tabs.length} tabs</span>`,
windowId: window.id,
});
}
}
registerToTST();
updateTabCount();
browser.runtime.onMessageExternal.addListener(onMessageExternal)
browser.tabs.onCreated.addListener(updateTabCount)
browser.tabs.onRemoved.addListener(updateTabCount)