-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
122 lines (108 loc) · 4.03 KB
/
Copy pathbackground.js
File metadata and controls
122 lines (108 loc) · 4.03 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
const STARTUP_GRACE_MS = 8000;
chrome.runtime.onStartup.addListener(() => {
chrome.storage.session.set({ startupTime: Date.now() });
chrome.alarms.create('startupGrace', { delayInMinutes: STARTUP_GRACE_MS / 60000 });
console.log('[TabRight] Startup grace period started.');
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'startupGrace') {
chrome.storage.session.remove('startupTime');
console.log('[TabRight] Ready.');
}
});
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.session.remove('startupTime');
});
function shouldProcess(callback) {
chrome.storage.session.get('startupTime', ({ startupTime }) => {
if (!startupTime) { callback(true); return; }
callback((Date.now() - startupTime) >= STARTUP_GRACE_MS);
});
}
// Persist last active tab per window — survives service worker restarts
chrome.tabs.onActivated.addListener(({ tabId, windowId }) => {
chrome.tabs.get(tabId, (tab) => {
if (chrome.runtime.lastError || !tab) return;
// Don't record the new blank tab as "last active"
const url = tab.url || tab.pendingUrl || '';
if (url === 'chrome://newtab/' || url === '') return;
chrome.storage.session.set({
[`lastActive_${windowId}`]: {
tabId: tab.id,
index: tab.index,
groupId: tab.groupId
}
});
console.log('[TabRight] Recorded active tab', tab.id, 'index', tab.index, 'window', windowId);
});
});
// Also update index when tabs are moved (indices shift)
chrome.tabs.onMoved.addListener((tabId, moveInfo) => {
chrome.storage.session.get(`lastActive_${moveInfo.windowId}`, (data) => {
const last = data[`lastActive_${moveInfo.windowId}`];
if (last && last.tabId === tabId) {
chrome.storage.session.set({
[`lastActive_${moveInfo.windowId}`]: { ...last, index: moveInfo.toIndex }
});
}
});
});
chrome.tabs.onCreated.addListener((newTab) => {
shouldProcess((ready) => {
if (!ready || newTab.pinned) return;
const windowId = newTab.windowId;
if (newTab.openerTabId != null) {
// Opened from a link — use opener tab
chrome.tabs.get(newTab.openerTabId, (opener) => {
if (chrome.runtime.lastError || !opener) return;
moveTab(newTab, opener.index + 1, windowId, opener.groupId);
});
return;
}
// Ctrl+T or Ctrl+Shift+T — use persisted last active tab
chrome.storage.session.get(`lastActive_${windowId}`, (data) => {
const last = data[`lastActive_${windowId}`];
if (!last) {
console.log('[TabRight] No last active tab recorded, skipping.');
return;
}
console.log('[TabRight] Using persisted last active tab', last.tabId, 'index', last.index);
moveTab(newTab, last.index + 1, windowId, last.groupId);
});
});
});
function moveTab(newTab, targetIndex, windowId, sourceGroupId) {
setTimeout(() => {
chrome.tabs.get(newTab.id, (tab) => {
if (chrome.runtime.lastError || !tab) return;
if (tab.windowId !== windowId) return;
const alreadyInGroup = tab.groupId !== chrome.tabGroups.TAB_GROUP_ID_NONE;
const shouldBeInGroup = sourceGroupId !== chrome.tabGroups.TAB_GROUP_ID_NONE;
const doMove = (callback) => {
if (tab.index === targetIndex) {
callback();
return;
}
chrome.tabs.move(newTab.id, { index: targetIndex }, () => {
if (chrome.runtime.lastError) {
console.log('[TabRight] Move error:', chrome.runtime.lastError.message);
} else {
console.log('[TabRight] Moved to index', targetIndex);
}
callback();
});
};
doMove(() => {
if (shouldBeInGroup && !alreadyInGroup) {
chrome.tabs.group({ tabIds: [newTab.id], groupId: sourceGroupId }, () => {
if (chrome.runtime.lastError) {
console.log('[TabRight] Group error:', chrome.runtime.lastError.message);
} else {
console.log('[TabRight] Added tab to group', sourceGroupId);
}
});
}
});
});
}, 150);
}