This repository was archived by the owner on Sep 5, 2022. It is now read-only.
forked from aboodman/switch-to-tab
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbackground.js
More file actions
180 lines (153 loc) · 4.35 KB
/
Copy pathbackground.js
File metadata and controls
180 lines (153 loc) · 4.35 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
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
var omnibox = chrome.omnibox;
var topMatch;
function _log(text) {
//////// comment/uncommment if you need to debug //////////
//console.log(text);
}
function escape(text) {
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
function escapeRegexp(text) {
var specialChars = [
'\\',
'[',
']',
'(',
')',
'|',
'.',
'+',
'?',
'{',
'}',
'-'
];
return text.split('').map(function(char) {
if (specialChars.indexOf(char) > -1)
return '\\' + char;
else
return char;
}).join('');
}
function parseMatches(text, search) {
//console.log("parseMatches search=" + search + " text=" + text);
var terms = escapeRegexp(search).split(/\s+/g);
var termMatchCounts = [];
terms.forEach(function() { termMatchCounts.push(0); });
var re = new RegExp("(" + terms.join(")|(") + ")", "ig");
var result = [];
var lastIndex = 0;
var match;
while (match = re.exec(text)) {
result.push({
match: false,
text: escape(text.substring(lastIndex, match.index))
});
lastIndex = match.index + match[0].length;
result.push({
match: true,
text: escape(text.substring(match.index, lastIndex))
});
for (var i = 1; i < match.length; i++) {
if (match[i])
termMatchCounts[i - 1]++;
}
}
// If any term found no matches, then we don't have a match.
if (termMatchCounts.indexOf(0) > -1) {
return [
{
match: false,
text: escape(text)
}
];
}
result.push({
match: false,
text: escape(text.substring(lastIndex))
});
return result;
}
function formatMatches(parsed) {
//_log("formatMatches parsed.match=" + parsed.match + " parsed.text=" + parsed.text);
return parsed.reduce(function(s, piece) {
if (piece.match)
return s + "<match>" + piece.text + "</match>";
else
return s + piece.text;
}, "");
}
omnibox.onInputChanged.addListener(function(text, suggest) {
_log("onInputChanged text=" + text );
text = text.toLowerCase().replace(/\W+/g, ' ').trim();
if (!text)
return; //FIXME : jump to last tab
chrome.windows.getAll({populate: true}, function(windows) {
topMatch = -1;
var tabs = windows.reduce(function(arr, win) {
return arr.concat(win.tabs);
}, []);
var suggestions = tabs.map(function(tab) {
return {
tab: tab,
title: parseMatches(tab.title, text),
url: parseMatches(tab.url, text)
};
}).filter(function(item) {
if (item.title.length > 1 || item.url.length > 1) {
if (topMatch == -1)
topMatch = item.tab.id;
return true;
} else {
return false;
}
}).map(function(item) {
return {
content: item.tab.title + ' - ' + item.tab.url + '#' + item.tab.id,
description: formatMatches(item.title) + ' - ' +
'<url>' + formatMatches(item.url) + '</url>'
};
});
_log("onInputChanged suggestions: length=" + suggestions.length + " first=" + suggestions[0].content);
if (suggestions && suggestions.length == 1 && text.length >= 4 && (""+localStorage["autoswitch"] == "true")) {
tabsearch_onInputEntered(suggestions[0].content);
return;
}
suggest(suggestions);
});
});
var tabsearch_onInputEntered = function(url) {
_log("tabsearch_onInputEntered url=" + url + " topMatch=" + topMatch);
var tabId = url.match(/#(\d+)$/);
if (tabId) {
tabId = parseInt(tabId[1]);
_log("Jump to tabId=" + tabId );
} else if (topMatch != -1) {
_log("Jump to topMatch=" + topMatch );
tabId = topMatch;
} else {
_log("Nothing found for url=" + url);
return;
}
chrome.tabs.getSelected(null, function(selected) {
// if the selected tab was the new tab page,
// assume that it was blank, and close it!
if (selected.url == 'chrome://newtab/')
chrome.tabs.remove(selected.id);
});
chrome.tabs.get(tabId, function(tab) {
if (tab) {
chrome.windows.get(tab.windowId, function (window) {
if (!(window.focused && tab.selected)) {
chrome.tabs.update(tabId, { active: true }, function() {
chrome.windows.update(tab.windowId, { focused: true });
});
}
});
}
});
};
omnibox.onInputEntered.addListener(tabsearch_onInputEntered);
omnibox.onInputCancelled.addListener(function() {
topMatch = -1;
});