-
Notifications
You must be signed in to change notification settings - Fork 3
/
TabExtract.js
64 lines (54 loc) · 1.94 KB
/
TabExtract.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
(function() {
var matches = function( keywords, tab ) {
for(var i = 0; i < keywords.length; i++) {
if( (tab.url.toLowerCase()).search(keywords[i]) > -1 ) { return true; }
if( (tab.title.toLowerCase()).search(keywords[i]) > -1 ) { return true; }
}
return false;
};
var getMatchingTabs = function( text, callback ) {
var matchingTabs = [];
var queryInfo = {
pinned: false,
status: "complete",
windowType: "normal"
};
chrome.tabs.query( queryInfo, function( tabs ) {
var keywords = text.toLowerCase().split(" ");
if( keywords[0] == "" ) {
return callback([]);
}
for(var i = 0; i < tabs.length; i++) {
if( matches( keywords, tabs[i] ) ) {
matchingTabs.push( tabs[i].id );
}
}
callback( matchingTabs );
} );
};
chrome.omnibox.onInputChanged.addListener( function( text, suggest ) {
getMatchingTabs( text, function( matchingTabs ) {
var suggestionText = (matchingTabs.length < 1) ?
"0 tabs matching. Enter another keyword or press ESC to cancel."
: matchingTabs.length + " tabs matching. Press enter to move them to a new window.";
suggest( [{content: " ", description: suggestionText}] );
} );
} );
chrome.omnibox.onInputEntered.addListener( function( text ) {
getMatchingTabs( text, function( matchingTabs ) {
if( matchingTabs.length < 1 || text === "" ) {
alert(
'no matches found for the keywords "' + text + '".'
+ "\nNote: You do not need to press down to select a suggestion. Just press enter after entering keywords."
);
}
else {
chrome.windows.create( {type: "normal"}, function( win ) {
var newWindow = win;
chrome.tabs.move( matchingTabs, { windowId: newWindow.id, index: -1 } );
chrome.tabs.remove( newWindow.tabs[newWindow.tabs.length - 1].id );
} );
}
} );
} );
})();