-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTabExtract.js
More file actions
91 lines (78 loc) · 3.1 KB
/
TabExtract.js
File metadata and controls
91 lines (78 loc) · 3.1 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
(function() {
var matches = function( keywords, tab ) {
// Check if this is a Marvellous Suspender suspended tab
const suspenderPrefix = "chrome-extension://noogafoofpebimajpfpamcfhoaifemoa/suspended.html";
if (tab.url.startsWith(suspenderPrefix)) {
// Extract the original URL and title from the suspended tab URL
try {
const hashParams = new URL(tab.url).hash.substring(1);
const params = new URLSearchParams(hashParams);
const originalUrl = params.get('uri') || '';
const originalTitle = decodeURIComponent(params.get('ttl') || '');
// Search in the original URL and title
for(var i = 0; i < keywords.length; i++) {
if( originalUrl.toLowerCase().search(keywords[i]) > -1 ) { return true; }
if( originalTitle.toLowerCase().search(keywords[i]) > -1 ) { return true; }
}
} catch (e) {
// If parsing fails, fall back to normal matching
}
}
// Normal matching for non-suspended tabs
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 = {
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] );
}
}
callback( matchingTabs );
} );
};
var getPinnedTabIDs = function(tabs) {
return tabs.filter(t => t.pinned).map(t => t.id);
}
chrome.omnibox.onInputChanged.addListener( function( text, suggest ) {
getMatchingTabs( text, function( matchingTabs ) {
var pinnedTabIDs = getPinnedTabIDs(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 === "" ) {
// Do nothing - the omnibox already shows "0 tabs matching" as feedback
return;
}
else {
chrome.windows.create( {type: "normal"}, function( win ) {
var newWindow = win;
const pinnedTabIDs = getPinnedTabIDs(matchingTabs)
chrome.tabs.move( matchingTabs.map(t => t.id), { windowId: newWindow.id, index: -1 }, function() {
pinnedTabIDs.forEach(function(id) {
chrome.tabs.update(id, {pinned: true});
});
} );
chrome.tabs.remove( newWindow.tabs[newWindow.tabs.length - 1].id );
} );
}
} );
} );
})();