Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
452 lines (378 loc) · 15 KB
/
Copy pathworker.js
File metadata and controls
452 lines (378 loc) · 15 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"use strict";
// 문구는 콘텐츠 스크립트와 같은 정의를 씁니다. 서비스 워커도 chrome.i18n을
// 그대로 쓸 수 있으므로 조회 함수를 복제하지 않습니다.
importScripts("shared/i18n.js");
const { readMessages, t, useMessages } = globalThis.BetterGesture;
const PROTOCOL_VERSION = "1.3";
const MAX_HISTORY_ENTRIES = 20;
const MAX_TRACKED_OPENERS = 300;
// 실제 히스토리 항목 id는 양수라서, 이 탭을 연 탭을 가리키는 가상 항목과
// 섞이지 않습니다.
const OPENER_ENTRY_ID = -1;
// 사용자에게 그대로 보여도 되는 오류입니다. 그 밖의 오류 원문은 노출하지 않습니다.
class HistoryError extends Error {
constructor(message) {
super(message);
this.name = "HistoryError";
}
}
// 콘텐츠 스크립트는 확장 리소스를 직접 읽지 못하므로 문구표를 여기서 읽어
// 넘겨 줍니다. 워커 자신이 만드는 오류 문구도 같은 표를 씁니다.
let languagePromise = null;
function ensureLanguage() {
languagePromise ??= applyLanguage();
return languagePromise;
}
async function applyLanguage() {
const stored = await chrome.storage.sync.get({ language: "auto" });
useMessages(await readMessages(stored.language));
}
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === "sync" && changes.language) languagePromise = applyLanguage();
});
const attachedTabs = new Set();
const tabQueues = new Map();
let openerQueue = Promise.resolve();
chrome.debugger.onDetach.addListener((source) => {
if (Number.isInteger(source?.tabId)) attachedTabs.delete(source.tabId);
});
// target="_blank" 링크로 열린 탭에는 돌아갈 기록이 없습니다. 그 탭을 연 탭을
// 기억해 두면 뒤로 제스처를 원래 보던 곳으로 돌려보낼 수 있습니다. Chrome도
// openerTabId를 들고 있지만 사용자가 탭을 손으로 옮겨 다니면 지워 버리므로,
// 관계가 확실한 열린 순간에 따로 적어 둡니다.
chrome.tabs.onCreated.addListener((tab) => {
if (!Number.isInteger(tab?.id) || !Number.isInteger(tab.openerTabId)) return;
void rememberOpener(tab.id, tab.openerTabId);
});
chrome.tabs.onRemoved.addListener((tabId) => {
if (Number.isInteger(tabId)) void forgetOpener(tabId);
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (sender.id !== chrome.runtime.id || !Number.isInteger(sender.tab?.id)) {
return undefined;
}
const tabId = sender.tab.id;
if (message?.type === "GET_TAB_HISTORY") {
const direction = normalizeDirection(message.direction);
getTabHistory(tabId, direction)
.then((entries) => sendResponse({ ok: true, entries }))
.catch((error) => sendResponse(toErrorResponse(error)));
return true;
}
if (message?.type === "NAVIGATE_ONE_STEP") {
const direction = normalizeDirection(message.direction);
navigateOneStep(tabId, direction)
.then((navigated) => sendResponse({ ok: true, navigated }))
.catch((error) => sendResponse(toErrorResponse(error)));
return true;
}
if (message?.type === "GET_MESSAGES") {
readMessages(message.language)
.then((messages) => sendResponse({ ok: true, messages }))
.catch(() => sendResponse({ ok: true, messages: null }));
return true;
}
if (message?.type === "NAVIGATE_HISTORY") {
if (!Number.isInteger(message.entryId)) {
sendResponse({ ok: false, error: t("errorInvalidEntry") });
return undefined;
}
const direction = normalizeDirection(message.direction);
navigateToHistoryEntry(tabId, message.entryId, direction)
.then(() => sendResponse({ ok: true }))
.catch((error) => sendResponse(toErrorResponse(error)));
return true;
}
return undefined;
});
// 탭 관계 기록은 한 줄로 세워 씁니다. 탭이 연달아 열리고 닫히면 읽고 쓰는
// 사이에 서로의 결과를 덮어씁니다.
function queueOpenerWrite(update) {
openerQueue = openerQueue.then(update, update);
return openerQueue;
}
async function readOpeners() {
const stored = await chrome.storage.session.get({ tabOpeners: {} });
const openers = stored.tabOpeners;
return openers && typeof openers === "object" ? openers : {};
}
function rememberOpener(tabId, openerTabId) {
return queueOpenerWrite(async () => {
const openers = { ...(await readOpeners()), [tabId]: openerTabId };
await chrome.storage.session.set({ tabOpeners: limitOpeners(openers) });
});
}
// 탭이 닫히면 그 탭의 기록과, 그 탭을 가리키던 기록을 함께 지웁니다.
function forgetOpener(tabId) {
return queueOpenerWrite(async () => {
const openers = await readOpeners();
const remaining = Object.fromEntries(
Object.entries(openers).filter(
([openedId, openerId]) => Number(openedId) !== tabId && openerId !== tabId
)
);
if (Object.keys(remaining).length === Object.keys(openers).length) return;
await chrome.storage.session.set({ tabOpeners: remaining });
});
}
// 탭을 닫아도 onRemoved를 놓칠 수 있으므로(브라우저 종료, 서비스 워커 교체)
// 상한을 둡니다. 탭 id는 증가하기만 하므로 작은 id가 오래된 기록입니다.
function limitOpeners(openers) {
const ids = Object.keys(openers);
if (ids.length <= MAX_TRACKED_OPENERS) return openers;
const recent = ids
.sort((left, right) => Number(left) - Number(right))
.slice(-MAX_TRACKED_OPENERS);
return Object.fromEntries(recent.map((id) => [id, openers[id]]));
}
// 이 탭을 연 탭을 찾습니다. Chrome이 들고 있는 openerTabId가 가장 정확하고,
// 그것이 지워졌을 때만 기억해 둔 관계를 씁니다. 두 탭 모두 살아 있어야 합니다.
async function findOpener(tabId) {
const tab = await getTab(tabId);
const openerTabId = Number.isInteger(tab?.openerTabId)
? tab.openerTabId
: (await readOpeners())[tabId];
if (!Number.isInteger(openerTabId) || openerTabId === tabId) return null;
const opener = await getTab(openerTabId);
return opener ? { tab, opener } : null;
}
async function getTab(tabId) {
try {
return await chrome.tabs.get(tabId);
} catch {
// 이미 닫힌 탭입니다.
return null;
}
}
// 메뉴에서 "이 탭을 연 페이지" 항목을 고른 경우입니다. 그 페이지로 가는 것이
// 목적이므로, 연 탭이 없으면 할 일이 없습니다.
async function returnToOpener(tabId) {
const found = await findOpener(tabId);
if (!found) return false;
await activateOpener(found);
// 닫기를 기다리지 않고 응답합니다. 페이지의 beforeunload 확인창에 막히면
// 여기서 멈춰 서서 이미 사라질 탭의 메뉴가 응답을 기다리게 됩니다.
void closeReturnedTab(tabId);
return true;
}
// 뒤로 갈 기록이 없는 탭에서 뒤로 제스처를 한 경우입니다. 이 탭에서 볼 것은
// 끝났고 사용자는 그 전 화면으로 돌아가려는 것이므로 탭을 닫습니다. 이 탭을
// 연 탭이 있으면 그쪽을 앞으로 가져오고, 없으면 닫기만 합니다.
//
// 연 탭이 없고 창에 이 탭 하나만 남았을 때는 아무것도 하지 않습니다. 닫는
// 순간 창이 통째로 사라지는데 돌아갈 곳도 정해져 있지 않으므로, 뒤로가기 한
// 번으로 시킬 만한 일이 아닙니다. 연 탭이 있으면 이야기가 다릅니다 — 어디서
// 왔는지가 분명하니 창이 닫혀도 그쪽으로 이어집니다.
// 여기까지 오는 것은 뒤로 갈 곳이 없는 제스처뿐이라 자주 읽지 않습니다. 값을
// 들고 있으면 팝업에서 바꾼 것을 따라가는 코드가 따로 필요하므로 그때 읽습니다.
async function shouldCloseOnDeadEnd() {
try {
const stored = await chrome.storage.sync.get({ closeOnDeadEnd: true });
return stored.closeOnDeadEnd !== false;
} catch {
// 설정을 못 읽었으면 기본 동작대로 둡니다.
return true;
}
}
async function closeDeadEndTab(tabId) {
const tab = await getTab(tabId);
if (!tab) return false;
const found = await findOpener(tabId);
if (!found && await isLastTabInWindow(tab)) return false;
if (found) await activateOpener(found);
void closeReturnedTab(tabId);
return true;
}
async function activateOpener({ tab, opener }) {
await chrome.tabs.update(opener.id, { active: true });
if (Number.isInteger(opener.windowId) && opener.windowId !== tab?.windowId) {
try {
await chrome.windows.update(opener.windowId, { focused: true });
} catch {
// 창을 앞으로 못 가져와도 탭 자체는 이미 활성화되어 있습니다.
}
}
}
async function isLastTabInWindow(tab) {
if (!Number.isInteger(tab.windowId)) return true;
try {
const tabs = await chrome.tabs.query({ windowId: tab.windowId });
return tabs.length <= 1;
} catch {
// 몇 개인지 모르면 닫지 않는 쪽으로 둡니다.
return true;
}
}
async function closeReturnedTab(tabId) {
try {
await chrome.tabs.remove(tabId);
} catch {
// 이미 닫혔거나 페이지가 닫기를 막았습니다. 연 탭은 이미 앞에 나와 있습니다.
}
await forgetOpener(tabId);
}
async function getOpenerEntry(tabId) {
const found = await findOpener(tabId);
if (!found) return null;
const url = cleanText(found.opener.url);
return {
id: OPENER_ENTRY_ID,
title: cleanText(found.opener.title) || url || t("openerPageTitle"),
url,
distance: 1,
opener: true
};
}
async function navigateOneStep(tabId, direction = "back") {
await ensureLanguage();
try {
if (direction === "forward") {
await chrome.tabs.goForward(tabId);
} else {
await chrome.tabs.goBack(tabId);
}
return true;
} catch (error) {
if (!isUnavailableHistoryError(error)) throw error;
// 돌아갈 기록이 없는 탭입니다. 뒤로 제스처는 이 탭을 닫아 그 전에 보던
// 화면으로 돌려보냅니다. 이 동작을 끈 사람에게는 아무 일도 하지 않습니다.
if (direction !== "back") return false;
return (await shouldCloseOnDeadEnd()) ? closeDeadEndTab(tabId) : false;
}
}
async function getTabHistory(tabId, direction = "back") {
await ensureLanguage();
const entries = await readNavigationHistory(tabId, direction);
if (entries.length || direction !== "back") return entries;
// 뒤로 갈 기록이 없는 탭이라도 링크로 열렸다면 돌아갈 곳이 있습니다.
const openerEntry = await getOpenerEntry(tabId);
return openerEntry ? [openerEntry] : [];
}
async function readNavigationHistory(tabId, direction) {
return withDebugger(tabId, async (target) => {
const history = await chrome.debugger.sendCommand(
target,
"Page.getNavigationHistory"
);
if (!history || !Array.isArray(history.entries)) {
throw new HistoryError(t("errorHistoryRead"));
}
const currentIndex = Number.isInteger(history.currentIndex)
? history.currentIndex
: 0;
const entries = direction === "forward"
? history.entries.slice(currentIndex + 1)
: history.entries.slice(0, currentIndex).reverse();
// distance는 실제 히스토리 거리라서 걸러내기 전 순서로 계산하고,
// 개수 제한은 걸러낸 뒤에 적용해야 20개보다 적게 남지 않습니다.
return entries
.map((entry, index) => ({
id: entry.id,
title: cleanText(entry.title) || cleanText(entry.url) || t("untitledPage"),
url: cleanText(entry.url),
distance: index + 1
}))
.filter((entry) => Number.isInteger(entry.id) && entry.url)
.slice(0, MAX_HISTORY_ENTRIES);
});
}
async function navigateToHistoryEntry(tabId, entryId, direction = "back") {
await ensureLanguage();
if (entryId === OPENER_ENTRY_ID) {
if (direction === "back" && await returnToOpener(tabId)) return;
throw new HistoryError(t("errorOpenerGone"));
}
return withDebugger(tabId, async (target) => {
const history = await chrome.debugger.sendCommand(
target,
"Page.getNavigationHistory"
);
const currentIndex = Number.isInteger(history?.currentIndex)
? history.currentIndex
: 0;
const validEntries = (direction === "forward"
? history?.entries?.slice(currentIndex + 1)
: history?.entries?.slice(0, currentIndex)) ?? [];
const validEntry = validEntries
.some((entry) => entry.id === entryId);
if (!validEntry) {
throw new HistoryError(t("errorHistoryChanged"));
}
await chrome.debugger.sendCommand(
target,
"Page.navigateToHistoryEntry",
{ entryId }
);
});
}
// 같은 탭에 디버거를 겹쳐 붙이면 두 번째 attach가 실패하고, 먼저 끝난 작업이
// 아직 쓰이는 연결을 떼어 버립니다. 탭 단위로 한 줄로 세워서 실행합니다.
function withDebugger(tabId, operation) {
const previous = tabQueues.get(tabId) ?? Promise.resolve();
const result = previous.then(
() => attachAndRun(tabId, operation),
() => attachAndRun(tabId, operation)
);
const tail = result.then(() => {}, () => {});
tabQueues.set(tabId, tail);
void tail.then(() => {
if (tabQueues.get(tabId) === tail) tabQueues.delete(tabId);
});
return result;
}
async function attachAndRun(tabId, operation) {
const target = { tabId };
await chrome.debugger.attach(target, PROTOCOL_VERSION);
attachedTabs.add(tabId);
try {
return await operation(target);
} catch (error) {
// 디버거 안내 배너의 "취소"나 DevTools 연결로 끊긴 경우입니다.
if (!attachedTabs.has(tabId)) {
throw new HistoryError(t("errorDebuggerDetached"));
}
throw error;
} finally {
if (attachedTabs.delete(tabId)) {
try {
await chrome.debugger.detach(target);
} catch {
// The tab can close or navigate while detaching. There is nothing left to clean up.
}
}
}
}
function cleanText(value) {
return typeof value === "string" ? value.trim().slice(0, 2048) : "";
}
function normalizeDirection(value) {
return value === "forward" ? "forward" : "back";
}
function isUnavailableHistoryError(error) {
const message = error instanceof Error ? error.message : String(error);
return /Cannot find (?:a )?(?:next|previous) page in history/i.test(message) ||
/Cannot find a page to go (?:back|forward) to/i.test(message);
}
function toErrorResponse(error) {
if (error instanceof HistoryError) {
return { ok: false, error: error.message };
}
const rawMessage = error instanceof Error ? error.message : String(error);
const message = toFriendlyMessage(rawMessage);
if (!message) {
console.warn("Better Gesture: 처리하지 못한 오류입니다.", error);
}
return { ok: false, error: message || t("errorGeneric") };
}
function toFriendlyMessage(rawMessage) {
if (/Another debugger|already attached|Cannot attach/i.test(rawMessage)) {
return t("errorDebuggerBusy");
}
if (/Cannot access|not allowed|restricted/i.test(rawMessage)) {
return t("errorProtectedPage");
}
if (/No tab with given id|target closed/i.test(rawMessage)) {
return t("errorTabGone");
}
return "";
}