From 3e2820a6b4404568a36ebe279a97bb0bf2617c74 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 30 Aug 2017 21:06:31 +0900 Subject: [PATCH] throw error on multiple buffer matched --- src/background/tabs.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/background/tabs.js b/src/background/tabs.js index 201fa73..617909e 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -33,19 +33,17 @@ const selectAt = (index) => { const selectByKeyword = (keyword) => { chrome.tabs.query({ currentWindow: true }, (tabs) => { - let tab = tabs.find((tab) => tab.url.includes(keyword)) - if (tab) { - chrome.tabs.update(tab.id, { active: true }); - return; - } + let matched = tabs.filter((t) => { + return t.url.includes(keyword) || t.title.includes(keyword) + }) - tab = tabs.find((tab) => tab.title.includes(keyword)) - if (tab) { - chrome.tabs.update(tab.id, { active: true }); - return; + if (matched.length == 0) { + throw new RangeError('No matching buffer for ' + keyword); + } else if (matched.length >= 2) { + throw new RangeError('More than one match for ' + keyword); } - throw new RangeError('No matching buffer for ' + keyword); + chrome.tabs.update(matched[0].id, { active: true }); }) }