124 lines
4.0 KiB
JavaScript
124 lines
4.0 KiB
JavaScript
const tabsAction = browser.tabs;
|
|
const searchBar = document.getElementById("searchBar");
|
|
const errHandler = document.getElementById("errorZone");
|
|
const listZone = document.getElementById("listZone");
|
|
const notFoundText = document.createTextNode("Search not found...");
|
|
const tabImg = browser.extension.getURL("icons/tab.png");
|
|
var oldElm = "";
|
|
var plusTag = ""
|
|
var currentWinId = undefined;
|
|
var newWinId = undefined;
|
|
var focusedWinID = undefined;
|
|
var windowIndex = 0;
|
|
|
|
function logTabs(tabs) {
|
|
windowIndex = 0;
|
|
tabsAction.query({currentWindow: true, active: true}).then((tab) => {
|
|
focusedWinID = tab[0].windowId;
|
|
}, focusedWinID);
|
|
|
|
for (let tab of tabs) {
|
|
currentWinId = tab.windowId;
|
|
if (currentWinId == newWinId) {
|
|
createContainer(tab);
|
|
} else {
|
|
if (windowIndex != 0) {
|
|
var pTag = document.createElement("P");
|
|
var msg = (focusedWinID == tab.windowId)
|
|
? "[ Current Window ] " : "Window: " + tab.windowId;
|
|
var pText = document.createTextNode(msg);
|
|
pTag.className = "windowIdHeaders";
|
|
pTag.appendChild(pText);
|
|
listZone.appendChild(pTag);
|
|
windowIndex++;
|
|
createContainer(tab);
|
|
} else {
|
|
windowIndex = 1;
|
|
}
|
|
}
|
|
newWinId = currentWinId;
|
|
}
|
|
|
|
newWinId = undefined;
|
|
// Set poped-out-window position and 100px up from selected elm
|
|
oldElm.scrollIntoView();
|
|
window.scrollBy(0, -100);
|
|
}
|
|
|
|
function createContainer(tab) {
|
|
var id = tab.id;
|
|
var spanTag = document.createElement("DIV");
|
|
var pTag = document.createElement("P");
|
|
var iconText = document.createTextNode(tab.title);
|
|
var closeImgTag = document.createElement("IMG");
|
|
var hidnStImgTag = document.createElement("IMG");
|
|
var icoImgTag = document.createElement("IMG"); // Used to properly set bg
|
|
|
|
spanTag.setAttribute("tabID", tab.id);
|
|
spanTag.title = tab.title;
|
|
spanTag.id = "iconElm";
|
|
closeImgTag.id = "closeBttn";
|
|
closeImgTag.className = "closeImg";
|
|
closeImgTag.src = "../icons/x.png";
|
|
hidnStImgTag.id = "hideTgglBttn"
|
|
hidnStImgTag.className= "hiderImg";
|
|
pTag.className = "pTagTitleText";
|
|
|
|
if (!tab.hidden) {
|
|
spanTag.className = "block";
|
|
hidnStImgTag.src = "../icons/eyeOpen.png";
|
|
} else {
|
|
spanTag.className = "block hiddenBGColor";
|
|
hidnStImgTag.src = "../icons/eyeClosed.png";
|
|
}
|
|
|
|
spanTag.style.backgroundImage = "url(" + tab.favIconUrl + ")";
|
|
icoImgTag.src = tab.favIconUrl;
|
|
icoImgTag.onerror = function() { spanTag.style.backgroundImage = "url(" + tabImg + ")"; }
|
|
|
|
spanTag.appendChild(closeImgTag);
|
|
spanTag.appendChild(hidnStImgTag);
|
|
pTag.appendChild(iconText);
|
|
spanTag.appendChild(pTag);
|
|
listZone.appendChild(spanTag);
|
|
|
|
// Set oldElm so eventListeners.js has starting ref
|
|
if (tab.active == true) {
|
|
spanTag.className = "block block-focused";
|
|
if (oldElm) {
|
|
oldElm.setAttribute("class", "block");
|
|
}
|
|
oldElm = spanTag;
|
|
}
|
|
}
|
|
|
|
function createTab() {
|
|
tabsAction.create({})
|
|
.then(function (tab) {
|
|
createContainer(tab);
|
|
}).then(function () {
|
|
listZone.appendChild(plusTag);
|
|
});
|
|
}
|
|
|
|
function onError(error) { console.log(`Error: ${error}`); }
|
|
|
|
function getTabs() {
|
|
var elm = document.getElementById("searchMode");
|
|
var currentMode = (elm.getAttribute("searchwindowsmode") == "false");
|
|
|
|
if (currentMode == false) {
|
|
tabsAction.query({}).then(logTabs, onError)
|
|
.then(resetWinIndex, onError);
|
|
} else {
|
|
tabsAction.query({currentWindow: currentMode})
|
|
.then(logTabs, onError)
|
|
}
|
|
}
|
|
|
|
function resetWinIndex() {
|
|
windowIndex = 0;
|
|
}
|
|
|
|
getTabs();
|