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

51 lines
1.6 KiB
JavaScript

const tabQuery = browser.tabs.query({currentWindow: true});
const searchBar = document.getElementById("searchBar");
const errHandler = document.getElementById("errorZone");
const listZone = document.getElementById("listZone");
const notFoundText = document.createTextNode("Search not found...");
function logTabs(tabs) {
// tab.url requires the `tabs` permission
for (let tab of tabs) {
createContainer(tab);
}
// Set window position to bottom of list
window.scrollTo(0,document.body.scrollHeight);
}
function createContainer(tab) {
var id = tab.id;
var spanTag = document.createElement("SPAN");
var iconText = document.createTextNode(tab.title);
var centerTag = document.createElement("CENTER");
var closeImgTag = document.createElement("IMG");
var icoImgTag = document.createElement("IMG");
spanTag.title = tab.title;
spanTag.id = "iconElm";
spanTag.className = "block";
spanTag.setAttribute("tabID",tab.id);
closeImgTag.id = "closeBttn";
closeImgTag.className = "closeImg";
closeImgTag.src = "../icons/x.png";
icoImgTag.id = "faveIcon";
icoImgTag.className = "thumbImg";
icoImgTag.onerror = function() { icoImgTag.src = "../icons/tab.png"; }
icoImgTag.src = tab.favIconUrl;
centerTag.appendChild(icoImgTag);
spanTag.appendChild(closeImgTag);
spanTag.appendChild(centerTag);
spanTag.appendChild(iconText);
listZone.appendChild(spanTag);
}
function onError(error) { console.log(`Error: ${error}`); }
function getAllTabs() { tabQuery.then(logTabs, onError); }
getAllTabs();