-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
155 lines (140 loc) · 4.4 KB
/
content.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
;(async function () {
const options = await chrome.storage.sync.get()
const configuredTheme = options.theme ?? 'system'
const isAutoHide = options.autoHideMode || false
const tabListHeight = 32
const matchMedia = window.matchMedia('(prefers-color-scheme: dark)')
const isDarkTheme = (() => {
switch (configuredTheme) {
case 'light':
return false
case 'dark':
return true
default:
return matchMedia?.matches ? true : false
}
})()
if (configuredTheme === 'system') {
matchMedia.addEventListener('change', (e) => {
const isDarkTheme = matchMedia?.matches ? true : false
container.className = isDarkTheme ? 'dark' : 'light'
})
}
const container = document.createElement('div')
container.id = 'tab-list-container'
container.className = isDarkTheme ? 'dark' : 'light'
if (isAutoHide) {
container.setAttribute('data-hidden', true)
}
document.body.prepend(container)
if (!isAutoHide) {
document.body.style.paddingTop = `${tabListHeight}px`
const fixedElements = document.querySelectorAll('*')
fixedElements.forEach((el) => {
if (el.id === 'tab-list-container') return
const computedStyle = window.getComputedStyle(el)
const top = parseInt(computedStyle.top, 10)
if (isNaN(top)) {
return
}
if (computedStyle.position === 'fixed') {
el.style.top = `${top + tabListHeight}px`
}
if (computedStyle.position === 'sticky') {
if (top === 0) {
el.style.top = `${tabListHeight}px`
}
}
})
}
async function updateTabList(tabs) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError)
return
}
container.innerHTML = ''
const list = document.createElement('ul')
let index = 0
tabs.forEach((tab) => {
index++
const listItem = document.createElement('li')
let tabTitleLength = tab.title.length
let tabTitle = tabTitleLength <= 30 ? tab.title : `${tab.title.substr(0, 30)}...`
const textNode = document.createTextNode(tabTitle)
const img = document.createElement('img')
img.src = tab.favIconUrl
img.style.width = '16px'
img.style.height = '16px'
img.style.margin = '0'
img.style.marginRight = '8px'
listItem.appendChild(img)
listItem.appendChild(textNode)
if (tab.active) {
listItem.className = 'active'
}
listItem.setAttribute('data-tabid', tab.id)
listItem.addEventListener('click', () => {
try {
chrome.runtime.sendMessage(
{ action: 'navigateToTab', tabId: tab.id },
(response) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError)
}
document.querySelector(`[data-tabid=${tab.id}]`).scrollIntoView()
}
)
} catch (error) {
console.error('Error sending message to navigate to tab:', error)
}
})
list.appendChild(listItem)
})
container.appendChild(list)
}
function fetchTabs() {
if (chrome.runtime?.id) {
try {
chrome.storage.local.get(['tabsList'], async (result) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError)
return
}
if (result.tabsList) {
updateTabList(result.tabsList)
}
})
} catch (error) {
console.error('Error fetching tabs:', error)
}
}
}
fetchTabs()
if (isAutoHide) {
document.addEventListener('keyup', (event) => {
if (event.key === 'Meta') {
container.setAttribute('data-hidden', true)
}
})
}
document.addEventListener('keydown', (event) => {
if (event.metaKey && isAutoHide) {
chrome.runtime.sendMessage({ action: 'showTabList' })
}
if (event.metaKey && event.key >= '1' && event.key <= '9') {
const tabIndex = event.key === '9' ? -1 : parseInt(event.key, 10) - 1
chrome.runtime.sendMessage({ action: 'switchToTab', tabIndex }, () => {
document.querySelector(`[data-tabid=${tab.id}]`).scrollIntoView()
})
event.preventDefault()
}
})
chrome.runtime.onMessage.addListener((message) => {
if (message.messageType === 'update') {
fetchTabs()
}
if (message.messageType === 'showTabList') {
container.removeAttribute('data-hidden')
}
})
})()