forked from malcolmredheron/powerswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.html
171 lines (143 loc) · 4.14 KB
/
popup.html
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
<style>
body {
min-width:357px;
overflow-x:hidden;
margin: 100px;
}
#input {
width: 100%;
}
#tabs_list {
padding-top:10px;
}
.tab {
padding:2px 20px;
border-width: 2px;
border-style:solid;
border-color:#FFF;
-webkit-border-radius: 8px;
}
.tab.selected {
background-color: #cccccc;
border-style:solid;
border-color:#000;
}
.tab img {
width:16px;
height:16px;
padding-right: 4px;
}
.tab img[src=''] {
visibility: hidden;
}
.tab .name {
font-size: 20px;
font-family:Gill Sans;
/*font-weight:bold;*/
}
</style>
<body>
<input id="input"/>
<div id="tabs_list">
</div>
</body>
<script>
var background = chrome.extension.getBackgroundPage();
var selected_index = 0;
var matching_tabs = [];
var input = document.getElementById("input");
var onKeyPress = function(event) {
console.log('onKeyPress');
var is_up_or_down = false;
if (event.keyCode === 38) { // up
if (selected_index > 0) {
selected_index--;
}
is_up_or_down = true;
} else if (event.keyCode === 40) { // down
if (selected_index + 1 < matching_tabs.length) {
selected_index++;
}
is_up_or_down = true;
} else if (event.keyCode === 13) { // enter
selectTab(matching_tabs[selected_index].id);
} else if (event.keyCode === 27) { // escape
chrome.windows.getCurrent(function(window) {
chrome.windows.remove(window.id);
});
}
if (is_up_or_down) {
updateAndDisplayMatchingTabs();
event.stopPropagation();
event.preventDefault();
}
};
input.addEventListener('keydown', onKeyPress, /*useCapture=*/ false);
input.autocomplete = 'off';
var selectTab = function(tab_id) {
chrome.windows.getCurrent(function(window) {
chrome.tabs.update(tab_id, {selected: true});
chrome.windows.remove(window.id);
});
};
var updateAndDisplayMatchingTabs = function() {
console.log('updateAndDisplayMatchingTabs');
var tabs_list_div = document.getElementById('tabs_list');
var filter_text = input.value.toLowerCase();
chrome.tabs.getCurrent(function(this_tab) {
chrome.windows.getAll({populate: true}, function(windows) {
var current_tab_map = {};
windows.forEach(function(window) {
window.tabs.forEach(function(tab) {
current_tab_map[tab.id] = tab;
});
});
tabs_list_div.innerHTML = "";
var shouldShowTab = function(tab) {
return tab.title.toLowerCase().indexOf(filter_text) !== -1;
};
var other_tabs = background.ordered_tabs.filter(function(tab_id) {
return tab_id !== this_tab.id;
}).map(function(tab_id) {
return current_tab_map[tab_id];
});
if (other_tabs.length > 0) {
other_tabs.push(other_tabs[0]);
other_tabs = other_tabs.slice(1);
}
matching_tabs = other_tabs.filter(function(tab) {
return shouldShowTab(tab);
});
matching_tabs.forEach(function(old_tab, tab_index) {
var tab = current_tab_map[old_tab.id];
var tab_div = document.createElement('div');
var icon_img = document.createElement('img');
icon_img.src = tab.favIconUrl ? tab.favIconUrl : '';
var name_span = document.createElement('span');
name_span.innerText = tab.title;
name_span.className = 'name';
tab_div.appendChild(icon_img);
tab_div.appendChild(name_span);
var class_value = 'tab';
if (tab_index === selected_index) {
class_value += ' selected';
}
tab_div.className = class_value;
tabs_list_div.appendChild(tab_div);
tab_div.onclick = function(event) {
selectTab(tab.id);
};
});
});
});
};
setTimeout(function() {
updateAndDisplayMatchingTabs();
var input = document.getElementById("input");
input.focus();
input.addEventListener("input", function(event) {
selected_index = 0;
updateAndDisplayMatchingTabs();
}, /*useCapture=*/ false);
}, 1);
</script>