diff --git a/src/js/core.js b/src/js/core.js index d7526b4..34a1461 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -102,3 +102,43 @@ export function openTabWithText(windowId, text) { chrome.tabs.update(tabs[goodIdx].id, { active: true }); }); } + +// groupTabsWithTitle finds all the tabs matching the given title and groups them together. +// TODO(abansal4032): List the matches and let the user select which one to open. +export function groupTabsWithTitle(windowId, title) { + chrome.tabs.query({windowId: windowId}, tabs => { + let matchingIds = []; + for (const tab of tabs) { + if (tab.title === undefined) { + console.log(tab.index); + continue; + } + if (tab.title.toLowerCase().includes(title.toLowerCase())) { + matchingIds.push(tab.id); + } + console.log(tab.title, tab.url); + } + console.log("matchingIds", matchingIds); + // Move the matching tabs to the end and highlight the tabs + chrome.tabs.move(matchingIds, {'windowId': windowId, 'index': -1}); + // Refetch the matching tabs for getting the updated indices to highlight. + // Can refactor here. + chrome.tabs.query({windowId: windowId}, tabs => { + let matchingIdxs = []; + for (const tab of tabs) { + if (tab.title === undefined) { + console.log(tab.index); + continue; + } + if (tab.title.toLowerCase().includes(title.toLowerCase())) { + matchingIdxs.push(tab.index); + } + console.log(tab.title, tab.url); + } + console.log("matchingIdxs", matchingIdxs); + if (matchingIds.length != 0) { + chrome.tabs.highlight({'tabs': matchingIdxs}); + } + }); + }); +} diff --git a/src/js/plugins/tab.js b/src/js/plugins/tab.js index efd2165..24c5e45 100644 --- a/src/js/plugins/tab.js +++ b/src/js/plugins/tab.js @@ -1,4 +1,4 @@ -import { performActionWithDelay, openTabWithText } from '../core'; +import { performActionWithDelay, openTabWithText, groupTabsWithTitle } from '../core'; @@ -317,9 +317,8 @@ const commands = [ action: 'TABS_FIND_TAB', callback: query => { console.log(query); - // chrome.tabs.update(tabs[0].id, { active: true }); chrome.windows.getCurrent({}, window => { - openTabWithText(window.id, query); + groupTabsWithTitle(window.id, query); }); } },