-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
121 lines (100 loc) · 2.84 KB
/
Copy pathbackground.js
File metadata and controls
121 lines (100 loc) · 2.84 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
const webExt = typeof browser !== 'undefined' ? browser : chrome;
const MAX_HISTORY_RESULTS = 100000;
function toTimeString(lastVisitTime) {
return new Date(lastVisitTime).toLocaleTimeString('en-GB', {
hour12: false,
hour: '2-digit',
minute: '2-digit'
});
}
function generateId(item) {
const safeUrl = typeof item.url === 'string' ? item.url : '';
const seconds = Number.isFinite(item.lastVisitTime)
? Math.floor(item.lastVisitTime / 1000)
: 0;
let hash = 2166136261;
const input = `${safeUrl}|${seconds}`;
for (let i = 0; i < input.length; i++) {
hash ^= input.charCodeAt(i);
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
}
function hostnameOf(url) {
try {
return new URL(url).hostname;
} catch {
return null;
}
}
function mapItem(item, parentId = null) {
return {
id: generateId(item),
time: toTimeString(item.lastVisitTime),
url: item.url,
title: item.title || item.url,
type: 'visit',
parent: parentId
};
}
async function buildHistoryExport() {
const results = await webExt.history.search({
text: '',
startTime: 0,
maxResults: MAX_HISTORY_RESULTS
});
results.sort((a, b) => a.lastVisitTime - b.lastVisitTime);
const output = [];
const lastIdByDomain = new Map();
for (const item of results) {
if (!item || typeof item.url !== 'string' || !item.url) {
continue;
}
const domain = hostnameOf(item.url);
const parentId = domain ? (lastIdByDomain.get(domain) ?? null) : null;
const mapped = mapItem(item, parentId);
output.push(mapped);
if (domain) {
lastIdByDomain.set(domain, mapped.id);
}
}
return output;
}
async function triggerDownload() {
const output = await buildHistoryExport();
const json = JSON.stringify(output, null, 2);
let blobUrl = null;
try {
blobUrl = URL.createObjectURL(new Blob([json], { type: 'application/json' }));
await webExt.downloads.download({
url: blobUrl,
filename: 'history-export.json',
saveAs: true
});
} catch (firstError) {
const dataUrl = `data:application/json;charset=utf-8,${encodeURIComponent(json)}`;
await webExt.downloads.download({
url: dataUrl,
filename: 'history-export.json',
saveAs: true
}).catch((secondError) => {
throw new Error(
`Primary download failed (${firstError?.message || String(firstError)}); fallback failed (${secondError?.message || String(secondError)})`
);
});
} finally {
if (blobUrl) {
setTimeout(() => URL.revokeObjectURL(blobUrl), 5000);
}
}
return { ok: true, count: output.length };
}
webExt.runtime.onMessage.addListener((message) => {
if (!message || message.action !== 'exportHistory') {
return undefined;
}
return triggerDownload().catch((error) => ({
ok: false,
error: error?.message || String(error)
}));
});