Reputation: 477
I'm trying to make a tab manager extension. The popup generates a list of all your open tabs. I'm now attempting to add a "close" option to every single one of those tabs in the list. This is what the list looks like.
It shows the tab ID, which I temporarily put there just to make sure that it's got the correct IDs. See code below:
let allTabs = [];
chrome.tabs.query({}, tabs => {
allTabs = tabs;
displayTabs();
});
function displayTabs() {
allTabs.forEach(tab => {
$("ul").append("<li class='tab-item'>"+ tab.id + " - " + trimTitle(tab.title) + " <a href='#' class='close-tab' id="+ tab.id +">Close tab</a></li>")
});
}
So far so good. The code for closing the tab looks like this:
$("ul").on("click", "a", function () {
let tabId = $(this).attr("id");
alert(tabId);
chrome.tabs.remove(tabId);
});
It takes the Tab ID from the ID attribute. I also made it alert the tab ID; so when I (for instance) close try closing the Extensions
tab with it, I get:
Which... seems correct to me. Yet I get the following error:
Uncaught TypeError: Error in invocation of tabs.remove([integer|array] tabIds, optional function callback): No matching signature.
Which... I do not understand. It should be the right ID, and the internet tells me that this is the way to close a tab.
Any help would be appreciated.
Upvotes: 3
Views: 4152
Reputation: 7891
.attr
return value of type string
and the chrome.tabs.remove
function - expects first parameter of type integer or array of integer
. Source - https://developer.chrome.com/extensions/tabs#method-remove.
First convert the tabId
to integer and then pass it to the remove
funtion.
$("ul").on("click", "a", function () {
let tabId = +$(this).attr("id"); // <-- or you can use parseInt
alert(tabId);
chrome.tabs.remove(tabId);
});
Upvotes: 3