-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud-sync.js
More file actions
275 lines (238 loc) · 9.96 KB
/
Copy pathcloud-sync.js
File metadata and controls
275 lines (238 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
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
// ==================== Cloud Sync Module ====================
const CloudSync = (function() {
const API_BASE_URL = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
? 'http://localhost:3000/api'
: '/gtr/api';
let _isSyncing = false;
let _pushTimer = null;
let _lastPulledData = { preferences: null, visitedPages: null, pov_progress: null };
const PUSH_DEBOUNCE_MS = 2000;
// Helper: get auth token from session
function _getToken() {
try {
const session = localStorage.getItem('userSession');
if (session) {
const parsed = JSON.parse(session);
return parsed.token || null;
}
} catch (e) {}
return null;
}
// Helper: authenticated fetch
async function _authFetch(url, options = {}) {
const token = _getToken();
if (!token) return null;
const headers = { 'Content-Type': 'application/json', ...options.headers };
headers['Authorization'] = 'Bearer ' + token;
const response = await fetch(url, { ...options, headers });
return response.json();
}
// Synchronous XHR pull - runs immediately at script load time
// so localStorage is updated BEFORE any other script reads it
function pullCloudDataSync() {
const token = _getToken();
if (!token) return;
try {
const xhr = new XMLHttpRequest();
xhr.open('GET', API_BASE_URL + '/user/data', false);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send();
if (xhr.status !== 200) {
console.warn('[CloudSync] Sync pull failed, status:', xhr.status);
return;
}
const result = JSON.parse(xhr.responseText);
if (!result || !result.success || !result.data) {
console.log('[CloudSync] No cloud data available');
return;
}
_applyPulledData(result.data);
} catch (error) {
console.warn('[CloudSync] Sync pull error:', error);
}
}
// Pull cloud data and merge into local
async function pullCloudData() {
if (_isSyncing) return;
const token = _getToken();
if (!token) return;
_isSyncing = true;
try {
console.log('[CloudSync] Pulling cloud data...');
const result = await _authFetch(API_BASE_URL + '/user/data');
if (!result || !result.success || !result.data) {
console.log('[CloudSync] No cloud data available');
return;
}
const cloudData = result.data;
_applyPulledData(cloudData);
} catch (error) {
console.error('[CloudSync] Pull failed:', error);
_updateSyncUI('error');
} finally {
_isSyncing = false;
}
}
// Merge two visitedPages arrays: union by page+params, keep latest timestamp
function _mergeVisitedPages(localPages, cloudPages) {
const map = {};
// Add all cloud entries first
for (const item of cloudPages) {
if (!item || !item.page) continue;
const key = (item.page || '') + '_' + (item.params || '');
map[key] = item;
}
// Override/merge with local entries (local may be newer)
for (const item of localPages) {
if (!item || !item.page) continue;
const key = (item.page || '') + '_' + (item.params || '');
if (!map[key] || item.timestamp > map[key].timestamp) {
map[key] = item;
}
}
return Object.values(map).sort((a, b) => (b.timestamp || 0) - (a.timestamp || 0));
}
// Apply pulled cloud data into localStorage
function _applyPulledData(cloudData) {
if (cloudData.preferences) {
_lastPulledData.preferences = JSON.parse(JSON.stringify(cloudData.preferences));
localStorage.setItem('preferences', JSON.stringify(cloudData.preferences));
console.log('[CloudSync] Preferences synced from cloud');
}
if (cloudData.visitedPages && Array.isArray(cloudData.visitedPages)) {
_lastPulledData.visitedPages = JSON.parse(JSON.stringify(cloudData.visitedPages));
const localPages = _getLocalVisitedPages();
const merged = _mergeVisitedPages(localPages, cloudData.visitedPages);
localStorage.setItem('visitedPages', JSON.stringify(merged));
console.log('[CloudSync] VisitedPages merged, total:', merged.length);
}
if (cloudData.pov_progress) {
_lastPulledData.pov_progress = JSON.parse(JSON.stringify(cloudData.pov_progress));
localStorage.setItem('pov_progress', JSON.stringify(cloudData.pov_progress));
console.log('[CloudSync] POV progress synced from cloud');
}
console.log('[CloudSync] Pull complete, lastSync:', cloudData.lastSync);
_updateSyncUI('synced', cloudData.lastSync);
if (typeof initStorageList === 'function') {
try { initStorageList(); } catch(e) {}
}
}
// Get local visitedPages safely
function _getLocalVisitedPages() {
try {
const data = localStorage.getItem('visitedPages');
if (data) {
const parsed = JSON.parse(data);
return Array.isArray(parsed) ? parsed : [];
}
} catch (e) {}
return [];
}
// Push local data to cloud (debounced)
function pushCloudData() {
if (_pushTimer) clearTimeout(_pushTimer);
_pushTimer = setTimeout(_doPush, PUSH_DEBOUNCE_MS);
}
async function _doPush() {
if (_isSyncing) return;
const token = _getToken();
if (!token) return;
_isSyncing = true;
_updateSyncUI('syncing');
try {
// Read local data
let preferences = null;
try {
const p = localStorage.getItem('preferences');
if (p) preferences = JSON.parse(p);
} catch (e) {}
// For visitedPages, merge local with cloud-stored version (ignore history limit)
const localPages = _getLocalVisitedPages();
const cloudPages = _lastPulledData.visitedPages || [];
const mergedPages = _mergeVisitedPages(localPages, cloudPages);
let pov_progress = null;
try {
const pp = localStorage.getItem('pov_progress');
if (pp) pov_progress = JSON.parse(pp);
} catch (e) {}
const payload = {};
if (preferences !== null) payload.preferences = preferences;
if (mergedPages.length > 0) payload.visitedPages = mergedPages;
if (pov_progress !== null) payload.pov_progress = pov_progress;
if (Object.keys(payload).length === 0) return;
// Update local cache of what we've pushed
if (preferences) _lastPulledData.preferences = JSON.parse(JSON.stringify(preferences));
if (mergedPages.length > 0) _lastPulledData.visitedPages = JSON.parse(JSON.stringify(mergedPages));
if (pov_progress) _lastPulledData.pov_progress = JSON.parse(JSON.stringify(pov_progress));
console.log('[CloudSync] Pushing data to cloud...');
const result = await _authFetch(API_BASE_URL + '/user/data', {
method: 'PUT',
body: JSON.stringify(payload)
});
if (result && result.success) {
console.log('[CloudSync] Push successful, lastSync:', result.data?.lastSync);
_updateSyncUI('synced', result.data?.lastSync);
} else {
console.error('[CloudSync] Push failed:', result);
_updateSyncUI('error');
}
} catch (error) {
console.error('[CloudSync] Push error:', error);
_updateSyncUI('error');
} finally {
_isSyncing = false;
}
}
// Update sync status UI element
function _updateSyncUI(status, lastSync) {
const el = document.getElementById('cloudSyncStatus');
if (!el) return;
const strings = window.strings;
const lang = window.lang || 'zh_hans';
if (status === 'syncing') {
el.textContent = (strings?.preferences?.auto_sync?.[lang]) || '同步中...';
el.style.color = 'var(--color-primary)';
} else if (status === 'synced') {
const syncText = (strings?.preferences?.auto_sync_success?.[lang]) || '已同步';
if (lastSync) {
const time = new Date(lastSync);
const timeStr = time.toLocaleTimeString();
el.textContent = syncText + ' ' + timeStr;
} else {
el.textContent = syncText;
}
el.style.color = '';
} else if (status === 'error') {
el.textContent = (strings?.preferences?.sync_error?.[lang]) || '同步失败';
el.style.color = 'crimson';
}
}
// Check if user is logged in
function _isLoggedIn() {
try {
const session = localStorage.getItem('userSession');
if (session) {
const parsed = JSON.parse(session);
return !!(parsed && parsed.token);
}
} catch (e) {}
return false;
}
// Initialize: pull on page load if logged in
function init() {
if (_isLoggedIn()) {
// Pull cloud data on page load
pullCloudData();
}
}
return {
pullCloudDataSync: pullCloudDataSync,
pullCloudData: pullCloudData,
pushCloudData: pushCloudData,
isLoggedIn: _isLoggedIn
};
})();
// ===== Immediate sync pull on script load =====
// Uses synchronous XHR so localStorage is updated BEFORE other scripts read it
CloudSync.pullCloudDataSync();