From db742a87c359d47eb619e626dc74b56681588947 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Thu, 28 Jun 2018 20:33:08 -0400 Subject: [PATCH] Add ability to switch to tab on click --- src/ts/browser-action.ts | 11 +++++++++++ src/ts/tabs.ts | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ts/browser-action.ts b/src/ts/browser-action.ts index 1d661b9..7a6bb6d 100644 --- a/src/ts/browser-action.ts +++ b/src/ts/browser-action.ts @@ -9,5 +9,16 @@ const MAIN_TEMPLATE: twig.Template = twig.twig({href: MAIN_TEMPLATE_URI, async: document.addEventListener("DOMContentLoaded", () => { tabs.getWindows((windows: chrome.windows.Window[]) => { document.querySelector("body").innerHTML = String(MAIN_TEMPLATE.render({windows})); + registerTabClickListeners(); }); }); + +function registerTabClickListeners(): void { + let tabElements = document.getElementsByClassName("tab"); + [].forEach.call(tabElements, (element) => { + element.addEventListener("click", (event) => { + let tabID = parseInt(event.target.getAttribute("tab-id")); + tabs.switchTo(tabID); + }); + }); +} diff --git a/src/ts/tabs.ts b/src/ts/tabs.ts index 458ded8..166e45b 100644 --- a/src/ts/tabs.ts +++ b/src/ts/tabs.ts @@ -1,4 +1,4 @@ -export {getWindows}; +export {getWindows, switchTo}; let windowCache: chrome.windows.Window[] = []; @@ -14,3 +14,13 @@ function getWindows(callback: (windows: chrome.windows.Window[]) => void): void callback(windows); }); } + +/** + * switchTo makes the tab with the given id active. + * + * @param {number} tabID The tab to switch to. + * @returns {void} + */ +function switchTo(tabID: number): void { + chrome.tabs.update(tabID, {active: true}); +}