Add ability to switch to tab on click
This commit is contained in:
parent
bd336e648f
commit
db742a87c3
|
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue