-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtab.js
252 lines (228 loc) · 6.18 KB
/
tab.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* Browser-tabs
*
* Open source browser extension
* Naviagte to opened tabs like your favourite editor or IDE
*
* @author: Madhankumar<[email protected]>
*/
(function () {
"use strict";
const VK_UP = 38;
const VK_DOWN = 40;
const VK_ESC = 27;
const VK_TAB = 13;
const element = document.createElement("div");
const shadow = element.attachShadow({
mode: "open",
}); // shadowdom
const inputElement = document.createElement("input");
const tabList = document.createElement("div");
const CONFIG = {
DEFAULT_FAVICON:
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAMklEQVR4AWMgEkT9R4INWBUgKX0Q1YBXQYQCkhKEMDILogSnAhhEV4AGRqoCTEhkPAMAbO9DU+cdCDkAAAAASUVORK5CYII=",
BASIC_SWITCH_TAB_MARKUP: `
<div class="search__container">
<input class="search-box" type="search" id="chrome-tab-search">
</div>
<div id="open-tabs" class="container"></div>
<div class="attributes__block"></div>
`,
style: `
<style type="text/css">
.search-box {
font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
padding: 10px 16px;
font-size: 14px;
color: #fff;
width: 100%;
box-sizing: border-box;
background: #1b1d2e;
border: none;
border-radius: 16px;
}
.search-box:focus {
border: none;
outline: none;
box-shadow: 0 2px 4px 0 #1b1d2e;
}
.container {
display: -webkit-flex;
-webkit-flex-flow: row wrap;
display: flex;
flex-flow: row wrap;
max-height: 70vh;
flex: 1;
overflow: auto;
margin-top: 10px;
}
.tab-item {
margin: 0px;
padding: 10px;
display: flex;
width: 100%;
margin-bottom: 12px;
}
.tab-item:hover {
cursor:pointer;
background: #9e999921;
border-radius: 16px;
}
.tab-item_icon {
width: 20px;
height: 20px;
display: block;
margin-right: 16px;
}
.tab-item_name {
flex: 1;
font-size: 15px;
text-align: left;
margin: 0;
white-space: nowrap;
max-width: 80%;
overflow: hidden;
text-overflow: ellipsis;
}
.tab-item_name:hover {
color: #fff;
}
.empty-state {
height: 100px;
text-align: center;
opacity: 0.6;
}
</style>
`,
};
/**
* Listener for background scripts
*/
chrome.runtime.onMessage.addListener(function (req, sender, senderResponse) {
if (req === "toggle-feature-foo") {
toggleTabList(false);
senderResponse(`${sender.id} Received the command`);
}
});
function filterTabs(event) {
let userInput = event.currentTarget.value;
let matchedTabs = [];
if (userInput.length) {
matchedTabs = chromeTabsManager.allOpenedTabs.filter(function (tab, index) {
if (
tab.title.toLowerCase().includes(userInput.toLowerCase()) ||
tab.url.toLowerCase().includes(userInput.toLowerCase())
) {
return tab;
}
});
chromeTabsManager.constructTabs(matchedTabs);
} else {
chromeTabsManager.constructTabs(chromeTabsManager.allOpenedTabs);
}
}
function render() {
shadow.innerHTML = CONFIG.style;
element.setAttribute('id', 'tab-host');
element.classList.add('chrome-tab-switch');
document.body.appendChild(element);
inputElement.setAttribute('type', 'text');
inputElement.setAttribute('id', 'chrome-tab-search');
inputElement.setAttribute('class', 'search-box');
inputElement.setAttribute('placeholder', 'Search opened tabs');
inputElement.addEventListener('keyup', filterTabs);
tabList.setAttribute('id', 'open-tabs');
tabList.setAttribute('class', 'container');
shadow.appendChild(inputElement);
shadow.appendChild(tabList);
}
function toggleTabList(stopToggle) {
let chromeTab = document.querySelector(".chrome-tab-switch");
if (stopToggle) {
chromeTab.classList.remove("show");
return;
} else {
chromeTabsManager.getAllTabs(chromeTabsManager.constructTabs);
chromeTab.classList.add("show");
inputElement.focus();
inputElement.value = ""; // clearing the input field while hiding the tab-item
}
}
function keyHandler(event) {
if ((event.keyCode || event.which) === VK_ESC) {
toggleTabList(element.classList.contains('show'))
}
}
const chromeTabsManager = {
allOpenedTabs: [],
switchTab: (event) => {
let selectedTab = {};
Array.from(event.currentTarget.attributes).forEach((data) => {
if (data.name !== "class") {
selectedTab["tabId"]
? (selectedTab["windowId"] = data.value)
: (selectedTab["tabId"] = data.value);
}
});
if (
event.keyCode === VK_TAB ||
event.which === VK_TAB ||
event.type === "click"
) {
chrome.extension.sendMessage(
{
type: "switchTab",
selectedTab,
},
function () {}
);
toggleTabList(false);
}
},
getAllTabs: (callback) => {
chrome.extension.sendMessage(
{
type: "getAllTabs",
},
function (tabs) {
chromeTabsManager.allOpenedTabs = tabs;
callback(chromeTabsManager.allOpenedTabs);
}
);
},
loadChromeExtension: () => {
render();
},
constructTabs: (tabs) => {
if (tabs.length) {
let tabItemsHTML = "";
tabs.forEach((tab, index) => {
tabItemsHTML += `
<div
class="tab-item"
data-tab-id="${tab.id}"
data-window-id="${tab.windowId}"
title="${tab.title}"
tabindex="0"
>
<img class="tab-item_icon" src="${
tab.favIconUrl
? tab.favIconUrl
: CONFIG.DEFAULT_FAVICON
}">
<p class="tab-item_name">${tab.title}</p>
</div>
`;
});
tabList.innerHTML = tabItemsHTML;
tabList.querySelectorAll(".tab-item").forEach((tabitem) => {
tabitem.addEventListener("click", chromeTabsManager.switchTab);
tabitem.addEventListener("keyup", chromeTabsManager.switchTab);
});
}
document.addEventListener("keyup", keyHandler);
},
shareTab: (mediaName, tabId) => {},
};
chromeTabsManager.loadChromeExtension();
})();