I have three files in a Chrome Extension. Explain what its doing.
manifest.json:
{
"manifest_version": 3,
"name": "Tab Prioritizer",
"version": "1.0",
"permissions": ["tabs", "storage", "activeTab", "scripting"],
"action": {
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": [""],
"js": ["content.js"]
}
],
"commands": {
"assign_priority": {
"suggested_key": {
"default": "Alt+P"
},
"description": "Assign or re-assign priority level to the active tab"
},
"sort_tabs": {
"suggested_key": {
"default": "Alt+8"
},
"description": "Re-order tabs by priority level"
}
},
"icons": {
}
}
background.js:
chrome.commands.onCommand.addListener((command) => {
if (command === 'assign_priority' || command === 'sort_tabs') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];
chrome.scripting.executeScript(
{
target: { tabId: activeTab.id },
files: ['content.js'],
},
() => {
chrome.tabs.sendMessage(activeTab.id, { action: command });
}
);
});
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'assign_priority') {
assignPriority(sender.tab, message.priority, message.timestamp);
} else if (message.action === 'sort_tabs') {
sortTabs();
}
});
function assignPriority(tab, priority, timestamp) {
const priorityData = {
level: priority,
timestamp: timestamp,
};
chrome.storage.sync.set({ [tab-${tab.id}]: priorityData });
}
function sortTabs() {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
const tabPriorities = tabs.map((tab) => {
return new Promise((resolve) => {
chrome.storage.sync.get([tab-${tab.id}], (result) => {
const priorityData = result[tab-${tab.id}] || {
level: null,
timestamp: null,
};
resolve({ tabId: tab.id, ...priorityData });
});
});
});
Promise.all(tabPriorities).then((priorities) => {
reorderTabs(priorities);
});
});
}
function reorderTabs(tabPriorities) {
tabPriorities.sort((a, b) => {
if (a.level === null && b.level !== null) return 1;
if (a.level !== null && b.level === null) return -1;
if (a.level === null && b.level === null) return 0;
if (a.level !== b.level) {
return a.level - b.level;
} else {
return a.timestamp - b.timestamp;
}
});
for (let i = 0; i < tabPriorities.length; i++) {
chrome.tabs.move(tabPriorities[i].tabId, { index: i });
}
chrome.tabs.update(tabPriorities[0].tabId, { active: true });
}
content.js:
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'assign_priority') {
assignPriority();
} else if (message.action === 'sort_tabs') {
sortTabs();
}
});
async function assignPriority() {
const priority = prompt('Enter priority level (0 to remove priority):');
if (priority === null) return;
const message = {
action: 'assign_priority',
priority: parseInt(priority, 10),
timestamp: priority === '0' ? null : Date.now(),
};
chrome.runtime.sendMessage(message);
}
function sortTabs() {
const message = {
action: 'sort_tabs',
};
chrome.runtime.sendMessage(message);
}