Tab-Search-and-Manage/src/scripts/searchTabs.js

65 lines
2.1 KiB
JavaScript

function findTabs(tabs) {
var selection = [];
if (searchBar.value != "") {
for (let tab of tabs) {
var title = tab.title;
if (title.toLowerCase().includes(searchBar.value.toLowerCase())) {
selection.push(tab);
}
}
if (selection.length > 1) {
logTabs(selection);
errHandler.style.display = "none";
clearNodes(errHandler);
} else {
if (selection[0] != undefined) {
searchBar.value = "";
errHandler.style.display = "none";
clearNodes(errHandler);
if (selection[0].windowId == focusedWinID) {
loadSelTab(selection[0].id);
} else {
browser.windows.update(selection[0].windowId, {focused: true})
.finally(loadSelTab(selection[0].id));
}
getTabs();
} else {
errHandler.style.display = "block";
errHandler.appendChild(notFoundText);
}
}
} else { logTabs(tabs); }
}
function clearNodes(targetNode) {
while (targetNode.firstChild) {
targetNode.removeChild(targetNode.firstChild);
}
}
function loadSelTab(id) { browser.tabs.update(id, { active: true }); }
function searchTabs() {
var elm = document.getElementById("searchMode");
var currentMode = (elm.getAttribute("searchwindowsmode") == "false");
clearNodes(listZone);
storageArea.get("searchMode").then((results) => {
if (Object.keys(results).length > 0) {
var fileKeys = Object.keys(results);
for (let fileKey of fileKeys) {
var key = results[fileKey];
if (key) {
tabsAction.query({}).then(findTabs, onError)
.then(resetWinIndex, onError);
} else {
tabsAction.query({currentWindow: true})
.then(findTabs, onError)
}
}
}
});
}