Hi there! I'm currently developing a chrome extension similar to "OneTab" and "Better OneTab". I want you to understand the source code of the extension and help me make changes to it. Here is the code:
// Load saved tab groups when the page loads
loadSavedTabGroups();
// Event listener for the 'Save Current Tabs' button
saveTabsButton.addEventListener('click', () => {
const category = categoryInput.value.trim();
if (category === '') {
alert('Please enter a category name.');
return;
}
// Query all open tabs in the current window
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const tabsData = tabs.map(tab => ({
title: tab.title,
url: tab.url
}));
// Save tabs under the specified category
chrome.storage.local.get('savedTabGroups', (data) => {
const savedTabGroups = data.savedTabGroups || [];
// Check if the category already exists
const existingGroupIndex = savedTabGroups.findIndex(group => group.category === category);
if (existingGroupIndex !== -1) {
// Append the new tabs to the existing group
savedTabGroups[existingGroupIndex].tabs = savedTabGroups[existingGroupIndex].tabs.concat(tabsData);
} else {
// Add a new group
savedTabGroups.push({
category: category,
tabs: tabsData
});
}
// Save the updated tab groups to storage
chrome.storage.local.set({ 'savedTabGroups': savedTabGroups }, () => {
// Clear the category input
categoryInput.value = '';
// Update the tab groups display
displayTabGroups(savedTabGroups);
});
});
});
});
// Event listener for the 'Restore All Tabs' button
restoreAllTabsButton.addEventListener('click', () => {
chrome.storage.local.get('savedTabGroups', (data) => {
const tabGroups = data.savedTabGroups || [];
tabGroups.forEach(group => {
group.tabs.forEach(tab => {
chrome.tabs.create({ url: tab.url });
});
});
});
});
// Function to load saved tab groups from storage
function loadSavedTabGroups() {
chrome.storage.local.get('savedTabGroups', (data) => {
const tabGroups = data.savedTabGroups || [];
displayTabGroups(tabGroups);
});
}
// Function to display tab groups on the page
function displayTabGroups(tabGroups) {
tabGroupsContainer.innerHTML = ''; // Clear existing content
tabGroups.forEach((group, groupIndex) => {
// Create a container for each group
const groupContainer = document.createElement('div');
groupContainer.className = 'tabGroup';
// Create a header for the group
const groupHeader = document.createElement('h2');
groupHeader.textContent = group.category;
// Add a remove button for the group
const removeGroupButton = document.createElement('button');
removeGroupButton.textContent = 'X';
removeGroupButton.title = 'Remove this group';
removeGroupButton.className = 'removeGroupButton';
removeGroupButton.addEventListener('click', () => {
removeTabGroup(groupIndex);
});
// Create a list for the tabs in the group
const tabList = document.createElement('ul');
tabList.className = 'tabList';
group.tabs.forEach((tab, tabIndex) => {
const listItem = document.createElement('li');
// Create a checkbox for selection
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.className = 'tabCheckbox';
// We store the tabIndex and groupIndex to identify the tab later
checkbox.dataset.tabIndex = tabIndex;
checkbox.dataset.groupIndex = groupIndex;
const link = document.createElement('a');
link.href = tab.url;
link.textContent = tab.title;
link.target = '_blank';
const removeTabButton = document.createElement('button');
removeTabButton.textContent = 'X';
removeTabButton.title = 'Remove this tab';
removeTabButton.className = 'removeTabButton';
removeTabButton.addEventListener('click', () => {
removeTabFromGroup(groupIndex, tabIndex);
});
listItem.appendChild(checkbox);
listItem.appendChild(link);
listItem.appendChild(removeTabButton);
tabList.appendChild(listItem);
});
// Add a button to remove selected tabs
const removeSelectedButton = document.createElement('button');
removeSelectedButton.textContent = 'Remove Selected Tabs';
removeSelectedButton.className = 'removeSelectedButton';
removeSelectedButton.addEventListener('click', () => {
removeSelectedTabs(groupIndex);
});
groupContainer.appendChild(removeGroupButton);
groupContainer.appendChild(groupHeader);
groupContainer.appendChild(tabList);
groupContainer.appendChild(removeSelectedButton);
// Add a restore button for the group
const restoreGroupButton = document.createElement('button');
restoreGroupButton.textContent = 'Restore Group Tabs';
restoreGroupButton.className = 'restoreGroupButton';
restoreGroupButton.addEventListener('click', () => {
restoreTabGroup(group);
});
groupContainer.appendChild(restoreGroupButton);
tabGroupsContainer.appendChild(groupContainer);
});
}
// Function to remove selected tabs from a group
function removeSelectedTabs(groupIndex) {
chrome.storage.local.get('savedTabGroups', (data) => {
const tabGroups = data.savedTabGroups || [];
const group = tabGroups[groupIndex];
// Get all checkboxes in this group's tab list
const groupContainer = tabGroupsContainer.children[groupIndex];
const checkboxes = groupContainer.querySelectorAll('.tabCheckbox');
// Collect the indices of tabs to remove
const indicesToRemove = [];
checkboxes.forEach((checkbox) => {
if (checkbox.checked) {
indicesToRemove.push(parseInt(checkbox.dataset.tabIndex));
}
});
if (indicesToRemove.length === 0) {
alert('Please select at least one tab to remove.');
return;
}
// Confirm to remove the tabs
if (!confirm('Are you sure you want to remove the selected tabs?')) {
return;
}
// Remove tabs starting from the highest index to avoid index shifting
indicesToRemove.sort((a, b) => b - a).forEach((tabIndex) => {
group.tabs.splice(tabIndex, 1);
});
// Remove the group if no tabs are left
if (group.tabs.length === 0) {
tabGroups.splice(groupIndex, 1);
}
// Save the updated tab groups to storage
chrome.storage.local.set({ 'savedTabGroups': tabGroups }, () => {
displayTabGroups(tabGroups);
});
});
}
// Function to remove an entire tab group
function removeTabGroup(groupIndex) {
chrome.storage.local.get('savedTabGroups', (data) => {
const tabGroups = data.savedTabGroups || [];
tabGroups.splice(groupIndex, 1);
chrome.storage.local.set({ 'savedTabGroups': tabGroups }, () => {
displayTabGroups(tabGroups);
});
});
}
// Function to remove a tab from a group
function removeTabFromGroup(groupIndex, tabIndex) {
chrome.storage.local.get('savedTabGroups', (data) => {
const tabGroups = data.savedTabGroups || [];
tabGroups[groupIndex].tabs.splice(tabIndex, 1);
// Remove the group if no tabs are left
if (tabGroups[groupIndex].tabs.length === 0) {
tabGroups.splice(groupIndex, 1);
}
chrome.storage.local.set({ 'savedTabGroups': tabGroups }, () => {
displayTabGroups(tabGroups);
});
});
}
// Function to restore tabs from a single group
function restoreTabGroup(group) {
group.tabs.forEach(tab => {
chrome.tabs.create({ url: tab.url });
});
}
});
"""
"""tabs.css
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 10px;
}
Hello! I'd be happy to help you understand your Chrome extension code and assist with making changes. I've reviewed the source code you've provided. Here's a breakdown of what each part does:
1. manifest.json
This file is the configuration file for your Chrome extension. It contains important metadata and specifies what the extension can do.
manifest_version: Indicates the version of the manifest file format. Version 3 is the latest for Chrome extensions.
name, version, description: Basic information about your extension.
permissions:
tabs: Allows the extension to interact with the browser's tab system.
storage: Allows the extension to store and retrieve data using Chrome's storage API.
background:
Specifies a background script (background.js) that runs in the background and listens for events.
action:
Defines the icon for the extension that appears in the browser's toolbar.
2. background.js
This script runs in the background and listens for events.