forked from levelsio/superlevels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
225 lines (198 loc) · 6.63 KB
/
background.js
File metadata and controls
225 lines (198 loc) · 6.63 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const DEFAULT_TIMEOUT_MIN = 5;
const CHECK_INTERVAL_MIN = 1;
// { tabId: lastActiveTimestamp }
const tabActivity = {};
// Record activity for a tab
function markActive(tabId) {
tabActivity[tabId] = Date.now();
}
// On tab activated (user switches to it)
chrome.tabs.onActivated.addListener(({ tabId }) => {
markActive(tabId);
});
// On tab updated (page load, navigation)
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
if (changeInfo.status === "complete" || changeInfo.url) {
markActive(tabId);
}
});
// On tab created
chrome.tabs.onCreated.addListener((tab) => {
markActive(tab.id);
});
// On tab removed, clean up
chrome.tabs.onRemoved.addListener((tabId) => {
delete tabActivity[tabId];
});
// Initialize: mark all existing tabs as active now
chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) {
markActive(tab.id);
}
});
// Periodic cleanup check
chrome.alarms.create("tabCleanup", { periodInMinutes: CHECK_INTERVAL_MIN });
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name !== "tabCleanup") return;
const { exclusions = [], enabled = true, timeoutMin = DEFAULT_TIMEOUT_MIN } =
await chrome.storage.local.get(["exclusions", "enabled", "timeoutMin"]);
if (!enabled) return;
const now = Date.now();
const timeoutMs = timeoutMin * 60 * 1000;
const tabs = await chrome.tabs.query({});
// Never close the last tab in a window
const windowTabCounts = {};
for (const tab of tabs) {
windowTabCounts[tab.windowId] = (windowTabCounts[tab.windowId] || 0) + 1;
}
// Find the currently active tab so we never close it
const activeTabs = new Set();
const windows = await chrome.windows.getAll();
for (const win of windows) {
const [active] = await chrome.tabs.query({
active: true,
windowId: win.id,
});
if (active) activeTabs.add(active.id);
}
for (const tab of tabs) {
// Skip active tabs
if (activeTabs.has(tab.id)) {
markActive(tab.id);
continue;
}
// Skip pinned tabs
if (tab.pinned) continue;
// Skip if it's the last tab in its window
if (windowTabCounts[tab.windowId] <= 1) continue;
// Skip excluded hosts
if (tab.url) {
try {
const host = new URL(tab.url).hostname;
if (
exclusions.some(
(ex) => host === ex || host.endsWith("." + ex)
)
) {
continue;
}
} catch {}
}
// Check inactivity
const lastActive = tabActivity[tab.id] || 0;
if (now - lastActive >= timeoutMs) {
windowTabCounts[tab.windowId]--;
// Save to closed history before removing
saveClosedTab(tab);
chrome.tabs.remove(tab.id);
delete tabActivity[tab.id];
}
}
});
// Save closed tab to history
function saveClosedTab(tab) {
if (!tab.url || tab.url.startsWith("chrome://")) return;
chrome.storage.local.get(["closed_tabs"], (data) => {
const closed = data.closed_tabs || [];
closed.unshift({
url: tab.url,
title: tab.title || tab.url,
favIconUrl: tab.favIconUrl || "",
time: Date.now(),
});
if (closed.length > 50) closed.length = 50;
chrome.storage.local.set({ closed_tabs: closed });
});
}
// ═══════════════════════════════════
// Redirect Tracer
// ═══════════════════════════════════
// { tabId: { chain: [{url, statusCode, statusLine}], finalUrl, finalStatus } }
const redirectData = {};
// When a new main-frame navigation starts, reset the chain
chrome.webNavigation.onBeforeNavigate.addListener((details) => {
if (details.frameId !== 0) return;
redirectData[details.tabId] = { chain: [], finalUrl: null, finalStatus: null };
});
// Capture each redirect hop
chrome.webRequest.onBeforeRedirect.addListener(
(details) => {
if (details.type !== "main_frame") return;
if (!redirectData[details.tabId]) {
redirectData[details.tabId] = { chain: [], finalUrl: null, finalStatus: null };
}
redirectData[details.tabId].chain.push({
url: details.url,
statusCode: details.statusCode,
statusLine: details.statusLine || "",
redirectUrl: details.redirectUrl,
});
},
{ urls: ["<all_urls>"] }
);
// Capture final completed request
chrome.webRequest.onCompleted.addListener(
(details) => {
if (details.type !== "main_frame") return;
if (!redirectData[details.tabId]) {
redirectData[details.tabId] = { chain: [], finalUrl: null, finalStatus: null };
}
redirectData[details.tabId].finalUrl = details.url;
redirectData[details.tabId].finalStatus = details.statusCode;
},
{ urls: ["<all_urls>"] }
);
// Clean up on tab close
chrome.tabs.onRemoved.addListener((tabId) => {
delete redirectData[tabId];
});
// Respond to popup requests
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === "getRedirects") {
sendResponse(redirectData[msg.tabId] || { chain: [], finalUrl: null, finalStatus: null });
}
if (msg.type === "pip") {
chrome.scripting.executeScript({
target: { tabId: msg.tabId },
func: () => {
if (document.pictureInPictureElement) {
document.exitPictureInPicture();
return { action: "exited" };
}
const videos = Array.from(document.querySelectorAll("video"));
if (!videos.length) return { error: "No video found on this page" };
const playing = videos.filter(v => !v.paused && !v.ended);
let video;
if (playing.length) {
video = playing.reduce((a, b) =>
(b.videoWidth * b.videoHeight) > (a.videoWidth * a.videoHeight) ? b : a
);
} else {
video = videos.reduce((a, b) =>
(b.videoWidth * b.videoHeight) > (a.videoWidth * a.videoHeight) ? b : a
);
}
return video.requestPictureInPicture()
.then(() => ({ action: "entered" }))
.catch(e => ({ error: e.message }));
},
}).then(results => {
sendResponse(results[0]?.result || { error: "No result" });
}).catch(err => {
sendResponse({ error: err.message });
});
return true; // async sendResponse
}
});
// Set defaults on install
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get(["enabled", "timeoutMin", "exclusions"], (data) => {
const defaults = {};
if (data.enabled === undefined) defaults.enabled = true;
if (data.timeoutMin === undefined) defaults.timeoutMin = DEFAULT_TIMEOUT_MIN;
if (data.exclusions === undefined) defaults.exclusions = [];
if (Object.keys(defaults).length) {
chrome.storage.local.set(defaults);
}
});
});