-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbackground.js
More file actions
101 lines (88 loc) · 3.18 KB
/
Copy pathbackground.js
File metadata and controls
101 lines (88 loc) · 3.18 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// background.js (Manifest V3 Service Worker)
// This array will keep track of the ordered tab IDs
let ordered_tab_ids = [];
// Function to show the popup window
const showPopup = () => {
// Use chrome.system.display.getInfo to get screen dimensions
chrome.system.display.getInfo((displays) => {
if (displays.length > 0) {
const display = displays[0]; // Use the primary display
const window_width = 850;
const window_height = 400;
const window_left = (display.workArea.width - window_width) / 2 + display.workArea.left;
const window_top = (display.workArea.height - window_height) / 2 + display.workArea.top;
chrome.windows.create({
url: "popup.html",
type: "popup",
width: window_width,
height: window_height,
left: Math.floor(window_left),
top: Math.floor(window_top),
});
} else {
console.error("No displays found.");
}
});
};
// Handle command events (e.g., keyboard shortcuts)
chrome.commands.onCommand.addListener((command) => {
if (command === 'switch_tab') {
showPopup();
}
});
// Listen for messages from other parts of the extension
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'showPopup') {
showPopup();
sendResponse({});
} else if (request.action === 'getOrderedTabIds') {
sendResponse({ ordered_tab_ids });
}
return true; // Required to keep the message channel open for asynchronous responses
});
// On startup, populate the ordered_tab_ids array
chrome.windows.getAll({ populate: true }).then((windows) => {
windows.forEach((window) => {
window.tabs.forEach((tab) => {
ordered_tab_ids.push(tab.id);
});
});
});
// Function to remove a tab by ID from the ordered list
const removeTabWithId = (tab_id) => {
const tab_index = ordered_tab_ids.indexOf(tab_id);
if (tab_index !== -1) {
ordered_tab_ids.splice(tab_index, 1);
}
};
// Update the ordered tab list when the current tab changes
const updateOrderedTabsWithCurrentTab = () => {
chrome.windows.getLastFocused().then((window) => {
chrome.tabs.query({ active: true, windowId: window.id }).then((tabs) => {
if (tabs.length > 0) {
const tab = tabs[0];
removeTabWithId(tab.id);
ordered_tab_ids.unshift(tab.id);
}
});
});
};
// Add event listeners for tab creation, removal, selection changes, and window focus changes
chrome.tabs.onCreated.addListener((tab) => {
ordered_tab_ids.unshift(tab.id);
});
chrome.tabs.onRemoved.addListener((tab_id) => {
removeTabWithId(tab_id);
});
chrome.tabs.onActivated.addListener(() => {
updateOrderedTabsWithCurrentTab();
});
chrome.windows.onFocusChanged.addListener((window_id) => {
if (window_id !== chrome.windows.WINDOW_ID_NONE) {
updateOrderedTabsWithCurrentTab();
}
});
chrome.tabs.onReplaced.addListener((added_tab_id, removed_tab_id) => {
removeTabWithId(removed_tab_id);
ordered_tab_ids.unshift(added_tab_id);
});