-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
157 lines (139 loc) · 5.11 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const STORAGE_KEY = 'maxTabsPerWindow';
const LIMIT_ENABLED_KEY = 'isLimitEnabled';
let maxTabsPerWindow = 20; // Default value
let isLimitEnabled = true; // Default value
let windowCounts = {};
let totalTabCount = -1;
async function loadStoredSettings() {
try {
const data = await chrome.storage.local.get([STORAGE_KEY, LIMIT_ENABLED_KEY]);
maxTabsPerWindow = data[STORAGE_KEY] || 20;
isLimitEnabled = data[LIMIT_ENABLED_KEY] !== undefined ? data[LIMIT_ENABLED_KEY] : true;
console.log(`Loaded settings - Max Tabs: ${maxTabsPerWindow}, Limit Enabled: ${isLimitEnabled}`);
} catch (error) {
console.error('Error loading stored settings:', error);
}
}
async function saveSettings() {
try {
await chrome.storage.local.set({
[STORAGE_KEY]: maxTabsPerWindow,
[LIMIT_ENABLED_KEY]: isLimitEnabled
});
console.log(`Saved settings - Max Tabs: ${maxTabsPerWindow}, Limit Enabled: ${isLimitEnabled}`);
} catch (error) {
console.error('Error saving settings:', error);
}
}
async function getStoredMaxTabs() {
try {
const data = await chrome.storage.sync.get(STORAGE_KEY);
return data[STORAGE_KEY] || 20;
} catch (error) {
console.error('Error retrieving stored max tabs:', error);
return 20;
}
}
async function setStoredMaxTabs(value) {
try {
await chrome.storage.sync.set({ [STORAGE_KEY]: value });
} catch (error) {
console.error('Error storing max tabs:', error);
}
}
function getImageData(maxWindows) {
const canvas = new OffscreenCanvas(100, 100);
const ctx = canvas.getContext("2d");
ctx.fillStyle = "green";
ctx.font = "normal 80px Arial";
ctx.fillText(maxWindows.toString(), 10, 65);
return ctx.getImageData(10, 10, 100, 100);
}
async function updateBadge() {
try {
const tabs = await chrome.tabs.query({});
windowCounts = {};
for (const tab of tabs) {
windowCounts[tab.windowId] = (windowCounts[tab.windowId] || 0) + 1;
}
totalTabCount = tabs.length;
for (const tab of tabs) {
const windowTabCount = windowCounts[tab.windowId];
const badgeText = `${windowTabCount}/${totalTabCount}`;
await chrome.action.setBadgeText({ tabId: tab.id, text: badgeText });
let color;
if (windowTabCount >= maxTabsPerWindow) {
color = "#FF0000";
} else if (windowTabCount >= maxTabsPerWindow * 0.75) {
color = "#FFBD33";
} else {
color = "#33FF57";
}
await chrome.action.setBadgeBackgroundColor({ tabId: tab.id, color });
}
} catch (error) {
console.error('Error updating badge:', error);
}
}
chrome.runtime.onInstalled.addListener(async () => {
console.log("Configurable Tab Limiter extension installed!");
await loadStoredSettings();
await chrome.action.setIcon({ imageData: getImageData(maxTabsPerWindow) });
await updateBadge();
});
// Modify the tabs.onCreated listener to respect the isLimitEnabled setting
chrome.tabs.onCreated.addListener(async (tab) => {
if (isLimitEnabled) {
try {
const tabs = await chrome.tabs.query({ currentWindow: true });
if (tabs.length > maxTabsPerWindow) {
await chrome.tabs.remove(tab.id);
console.log("Tab closed! Reached the maximum limit.");
}
} catch (error) {
console.error('Error handling new tab:', error);
}
}
await updateBadge();
});
chrome.tabs.onRemoved.addListener(updateBadge);
chrome.tabs.onAttached.addListener(updateBadge);
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (totalTabCount > -1) {
try {
const windowTabCount = windowCounts[tab.windowId];
const badgeText = `${windowTabCount}/${totalTabCount}`;
await chrome.action.setBadgeText({ tabId: tab.id, text: badgeText });
let color;
if (windowTabCount >= maxTabsPerWindow) {
color = "#FF0000";
} else if (windowTabCount >= maxTabsPerWindow * 0.75) {
color = "#FFBD33";
} else {
color = "#33FF57";
}
await chrome.action.setBadgeBackgroundColor({ tabId: tab.id, color });
} catch (error) {
console.error('Error updating badge for tab:', error);
}
} else {
await updateBadge();
}
});
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if (message.action === 'updateExtensionState') {
const { isEnabled, maxTabs } = message.data;
if (typeof isEnabled === 'boolean') {
isLimitEnabled = isEnabled;
}
if (typeof maxTabs === 'number' && maxTabs > 0) {
maxTabsPerWindow = maxTabs;
}
await saveSettings();
await chrome.action.setIcon({ imageData: getImageData(maxTabsPerWindow) });
await updateBadge();
}
});
// Initial load of settings
loadStoredSettings();
updateBadge();