-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
71 lines (65 loc) · 2.08 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const default_options = {
background: false,
};
browser.runtime.onMessage.addListener((message) => {
if (message.action === "openChangelog") {
browser.tabs.create({url: browser.runtime.getURL("CHANGELOG.md")});
}
});
browser.commands.onCommand.addListener(async (command) => {
console.log(`Command received: ${command}`);
try {
const options = await browser.storage.sync.get(default_options);
console.log(`Options: ${JSON.stringify(options)}`);
switch (command) {
case 'duplicate-tab':
await duplicateTab(options);
break;
case 'open-new-tab':
await openNewTab();
break;
case 'open-recent-closed-tab':
await openRecentClosedTab();
break;
case 'mute-tab':
await muteTab();
break;
}
} catch (error) {
console.error(`Error executing command ${command}:`, error);
}
});
async function duplicateTab(options) {
// Existing duplicateTab functionality...
}
async function openNewTab() {
console.log(`Executing open-new-tab command`);
await browser.tabs.create({ active: true });
console.log(`New tab opened`);
}
async function openRecentClosedTab() {
console.log(`Executing open-recent-closed-tab command`);
const recentlyClosed = await browser.sessions.getRecentlyClosed({ maxResults: 1 });
if (recentlyClosed.length > 0) {
const mostRecentTab = recentlyClosed[0];
if (mostRecentTab.tab) {
await browser.sessions.restore(mostRecentTab.tab.sessionId);
console.log(`Restored recently closed tab: ${mostRecentTab.tab.url}`);
} else {
console.log(`No recently closed tab found`);
}
} else {
console.log(`No recently closed sessions found`);
}
}
async function muteTab() {
console.log(`Executing mute-tab command`);
const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
if (activeTab) {
const muted = !activeTab.mutedInfo.muted;
await browser.tabs.update(activeTab.id, { muted });
console.log(`Tab ${activeTab.id} ${muted ? 'muted' : 'unmuted'}`);
} else {
console.log(`No active tab found`);
}
}