From a3cffe80e1bd4ef57abf811bfeb7b24bed9bcfcc Mon Sep 17 00:00:00 2001 From: abansal4032 Date: Tue, 30 Jun 2020 23:52:21 -0700 Subject: [PATCH 1/2] Refined find tabs : finds, groups and highlights the matching tabs --- src/js/core.js | 38 ++++++++++++++++++++++++++++++++++++++ src/js/plugins/tab.js | 5 ++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/js/core.js b/src/js/core.js index d7526b4..10c1079 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -102,3 +102,41 @@ 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); + 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); }); } }, From 960d74516802c9ba19c990fa674b19d3bec1cce9 Mon Sep 17 00:00:00 2001 From: Vaibhav Tulsyan Date: Sat, 4 Jul 2020 16:17:51 +0530 Subject: [PATCH 2/2] Fix runtime error - no groups to tab if no titles match --- src/js/core.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/js/core.js b/src/js/core.js index 10c1079..34a1461 100644 --- a/src/js/core.js +++ b/src/js/core.js @@ -136,7 +136,9 @@ export function groupTabsWithTitle(windowId, title) { console.log(tab.title, tab.url); } console.log("matchingIdxs", matchingIdxs); - chrome.tabs.highlight({'tabs': matchingIdxs}); + if (matchingIds.length != 0) { + chrome.tabs.highlight({'tabs': matchingIdxs}); + } }); }); }