Add ability to switch to tab on click

This commit is contained in:
Nick Krichevsky 2018-06-28 20:33:08 -04:00
parent bd336e648f
commit db742a87c3
2 changed files with 22 additions and 1 deletions

View file

@ -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);
});
});
}

View file

@ -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});
}