-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshare.js
More file actions
197 lines (187 loc) · 9.96 KB
/
Copy pathshare.js
File metadata and controls
197 lines (187 loc) · 9.96 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
/* Versioned, self-contained resume snapshots. URL data never needs a backend. */
const EasyCVShare = (() => {
const PREFIX = '#resume=';
const MAX_BYTES = 256 * 1024;
const MAX_URL_LENGTH = 64000;
const memory = new Map();
let active = location.hash.startsWith(PREFIX);
let loadError = '';
let generation = 0;
function toBase64URL(bytes) {
let binary = '';
for (let i = 0; i < bytes.length; i += 8192) binary += String.fromCharCode(...bytes.subarray(i, i + 8192));
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
function fromBase64URL(encoded) {
if (!/^[A-Za-z0-9_-]+$/.test(encoded) || encoded.length % 4 === 1) throw new Error('链接内容不完整,请重新复制完整链接。');
const binary = atob(encoded.replace(/-/g, '+').replace(/_/g, '/'));
const bytes = Uint8Array.from(binary, char => char.charCodeAt(0));
if (toBase64URL(bytes) !== encoded) throw new Error('链接内容损坏,请重新生成。');
return bytes;
}
async function readLimited(stream) {
const reader = stream.getReader();
const chunks = [];
let length = 0;
try {
while (true) {
const {done, value} = await reader.read();
if (done) break;
length += value.length;
if (length > MAX_BYTES) {
await reader.cancel();
throw new Error('简历数据过大,请使用 JSON 备份传递。');
}
chunks.push(value);
}
} finally { reader.releaseLock(); }
const result = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) { result.set(chunk, offset); offset += chunk.length; }
return result;
}
async function encode(data) {
const snapshot = normalizeResume(data, {});
let bytes = new TextEncoder().encode(JSON.stringify({resume: snapshot}));
if (bytes.length > MAX_BYTES) throw new Error('简历数据过大,请使用“导出备份”传递。');
let format = '1j';
if (typeof CompressionStream === 'function') {
const compressed = await readLimited(new Blob([bytes]).stream().pipeThrough(new CompressionStream('gzip')));
if (compressed.length < bytes.length) { bytes = compressed; format = '1g'; }
}
const token = `${format}.${toBase64URL(bytes)}`;
if (token.length + PREFIX.length > MAX_URL_LENGTH) throw new Error('简历生成的链接过长,请使用“导出备份”传递。');
return token;
}
async function decode(token) {
if (token.length > MAX_URL_LENGTH) throw new Error('分享链接过长,请使用 JSON 备份导入。');
const match = /^(1[gj])\.([A-Za-z0-9_-]+)$/.exec(token);
if (!match) throw new Error('链接格式无效或版本不支持,请重新生成完整链接。');
let bytes = fromBase64URL(match[2]);
if (match[1] === '1g') {
if (typeof DecompressionStream !== 'function') throw new Error('此浏览器不支持压缩链接,请使用新版浏览器打开。');
bytes = await readLimited(new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip')));
}
if (bytes.length > MAX_BYTES) throw new Error('简历数据过大,请使用 JSON 备份导入。');
const payload = JSON.parse(new TextDecoder('utf-8', {fatal: true}).decode(bytes));
if (!payload || !payload.resume || typeof payload.resume !== 'object') throw new Error('链接缺少简历数据。');
if (!payload.resume.settings || typeof payload.resume.settings !== 'object' || Array.isArray(payload.resume.settings)) {
payload.resume.settings = {};
}
// Share defaults are deterministic, never inherited from the recipient's resume.
return normalizeResume(payload.resume, {});
}
async function createURL(data) {
if (!['https:', 'http:'].includes(location.protocol)) throw new Error('请在已发布的 EasyCV 网站上生成分享链接。直接打开的本地文件地址无法分享给他人。');
const resumeData = data || (window.resumeApp ? window.resumeApp.getData() : (typeof state !== 'undefined' ? state : null));
if (!resumeData) throw new Error('无法读取简历数据。');
const url = new URL(location.href);
url.search = '';
url.hash = PREFIX.slice(1) + await encode(resumeData);
if (url.href.length > MAX_URL_LENGTH) throw new Error('链接过长,请导出 JSON 备份传递。');
return url.href;
}
let syncTimer = null;
function syncAddressBarURL(data) {
if (!active || !['https:', 'http:'].includes(location.protocol)) return;
clearTimeout(syncTimer);
syncTimer = setTimeout(async () => {
try {
const currentData = data || (window.resumeApp ? window.resumeApp.getData() : (typeof state !== 'undefined' ? state : null));
if (!currentData || !currentData.info) return;
const token = await encode(currentData);
const newHash = PREFIX + token;
if (location.hash !== newHash) {
history.replaceState(null, '', location.pathname + location.search + newHash);
}
} catch (e) {}
}, 300);
}
async function prepare() {
const app = document.querySelector('.app-container');
app.inert = true;
app.setAttribute('aria-busy', 'true');
try {
const resume = await decode(location.hash.slice(PREFIX.length));
const {settings, ...content} = resume;
memory.set('easycv_resume_content', JSON.stringify(content));
memory.set('easycv_resume_settings_a', JSON.stringify(settings));
memory.set('easycv_resume_current_scheme', 'A');
} catch (error) {
active = false;
memory.clear();
loadError = error instanceof SyntaxError || error instanceof TypeError
? '链接数据损坏或不完整,请向分享者获取新的链接。' : error.message;
history.replaceState(null, '', location.pathname + location.search);
} finally {
app.inert = false;
app.removeAttribute('aria-busy');
}
}
function initUI() {
const dialog = document.getElementById('share-dialog');
const field = document.getElementById('share-url');
const status = document.getElementById('share-status');
const copy = document.getElementById('copy-share-link');
const note = document.getElementById('share-address-note');
document.querySelectorAll('[data-share-resume]').forEach(button => button.addEventListener('click', async () => {
const request = ++generation;
field.value = '';
copy.disabled = true;
status.textContent = '正在生成链接…';
note.hidden = true;
dialog.showModal();
try {
const url = await createURL();
if (request !== generation || !dialog.open) return;
field.value = url;
copy.disabled = false;
status.textContent = url.length > 8000 ? `链接共 ${url.length.toLocaleString()} 个字符,部分应用可能截断,请确保完整发送。` : `已包含当前内容与排版 · ${url.length.toLocaleString()} 个字符`;
try {
if (['https:', 'http:'].includes(location.protocol)) {
history.replaceState(null, '', url);
}
} catch (e) {}
if (['localhost', '127.0.0.1', '[::1]'].includes(location.hostname)) {
note.hidden = false;
note.textContent = '这是本机预览地址。请在已发布的网站上生成可发给他人的链接。';
}
} catch (error) { if (request === generation) status.textContent = error.message; }
}));
document.getElementById('close-share-dialog').addEventListener('click', () => dialog.close());
dialog.addEventListener('close', () => { generation++; });
field.addEventListener('click', () => field.select());
copy.addEventListener('click', async () => {
if (!field.value) return;
try {
if (!navigator.clipboard?.writeText) throw new Error('clipboard unavailable');
await navigator.clipboard.writeText(field.value);
status.textContent = '链接已复制,可以发给对方了。';
} catch {
field.focus();
field.select();
status.textContent = '浏览器未允许自动复制,链接已选中,请手动复制。';
}
});
if (active) {
document.getElementById('shared-resume-banner').hidden = false;
document.getElementById('return-local-resume').href = location.pathname + location.search;
document.getElementById('save-status').textContent = '链接简历 · 本次页面暂存';
document.querySelector('[data-workspace-view="preview"]').click();
adjustPreviewScale();
}
if (loadError) {
document.getElementById('share-error-message').textContent = loadError;
document.getElementById('share-error-dialog').showModal();
}
window.addEventListener('hashchange', () => {
if (active || location.hash.startsWith(PREFIX)) location.reload();
});
}
return {get active() { return active; }, encode, decode, createURL, syncAddressBarURL, prepare, initUI,
storage: {
getItem: key => active ? (memory.get(key) ?? null) : localStorage.getItem(key),
setItem: (key, value) => active ? memory.set(key, String(value)) : localStorage.setItem(key, value)
}};
})();
const resumeStorage = EasyCVShare.storage;