-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.js
More file actions
239 lines (199 loc) · 7.07 KB
/
Copy pathsync.js
File metadata and controls
239 lines (199 loc) · 7.07 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
let autoSyncEnabled = true;
let syncIntervalId = null;
let lastSyncTime = 0;
const DEFAULT_SYNC_INTERVAL = 10 * 60 * 1000;
async function initSync() {
console.log('Inicializando módulo de sincronização...');
const settings = await getSyncSettings();
autoSyncEnabled = settings.autoSync;
if (autoSyncEnabled) {
startAutoSync(settings.syncInterval);
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'updateSyncSettings') {
handleSyncSettingsUpdate(message.settings);
sendResponse({ success: true });
return false;
} else if (message.action === 'syncNow') {
syncData()
.then(result => sendResponse({ success: true, result }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true;
} else if (message.action === 'getSyncStatus') {
getSyncStatus()
.then(status => sendResponse({ success: true, status }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true;
}
return false;
});
if (autoSyncEnabled) {
setTimeout(() => syncData(), 5000);
}
}
function handleSyncSettingsUpdate(settings) {
console.log('Configurações de sincronização atualizadas:', settings);
autoSyncEnabled = settings.autoSync;
if (syncIntervalId) {
clearInterval(syncIntervalId);
syncIntervalId = null;
}
if (autoSyncEnabled) {
startAutoSync(settings.syncInterval);
}
}
function startAutoSync(interval = DEFAULT_SYNC_INTERVAL) {
const syncInterval = Math.max(interval, 60000);
console.log(`Configurando sincronização automática, intervalo: ${syncInterval}ms`);
syncIntervalId = setInterval(() => {
syncData().catch(error => {
console.error('Erro na sincronização automática:', error);
});
}, syncInterval);
}
async function getSyncSettings() {
const data = await chrome.storage.local.get(['syncSettings']);
const defaultSettings = {
autoSync: true,
syncInterval: DEFAULT_SYNC_INTERVAL,
syncProvider: 'chrome',
lastSyncTime: 0
};
return data.syncSettings || defaultSettings;
}
async function saveSyncSettings(settings) {
return chrome.storage.local.set({
syncSettings: {
...await getSyncSettings(),
...settings
}
});
}
async function getSyncStatus() {
const settings = await getSyncSettings();
const now = Date.now();
return {
autoSyncEnabled: autoSyncEnabled,
lastSyncTime: settings.lastSyncTime,
nextSyncTime: autoSyncEnabled ? (settings.lastSyncTime + settings.syncInterval) : null,
timeUntilNextSync: autoSyncEnabled ? Math.max(0, (settings.lastSyncTime + settings.syncInterval) - now) : null,
syncProvider: settings.syncProvider
};
}
async function syncData() {
console.log('Iniciando sincronização de dados...');
try {
const settings = await getSyncSettings();
const provider = settings.syncProvider;
let result;
if (provider === 'chrome') {
result = await syncWithChromeSync();
} else {
throw new Error(`Provedor de sincronização não suportado: ${provider}`);
}
await saveSyncSettings({
lastSyncTime: Date.now()
});
console.log('Sincronização de dados concluída:', result);
return result;
} catch (error) {
console.error('Falha na sincronização de dados:', error);
throw error;
}
}
async function syncWithChromeSync() {
const localData = await chrome.storage.local.get(['scripts', '_disabledScripts', 'darkMode']);
const cloudData = await chrome.storage.sync.get(['scripts', '_disabledScripts', 'theme', 'lastSyncTimestamp']);
const localTimestamp = (await chrome.storage.local.get(['lastSyncTimestamp'])).lastSyncTimestamp || 0;
const cloudTimestamp = cloudData.lastSyncTimestamp || 0;
const mergeResult = {
direction: null,
changes: {
scripts: 0,
disabled_scripts: 0
}
};
let mergedScripts = {};
let mergedDisabledScripts = {};
if (cloudTimestamp > localTimestamp) {
console.log('Dados da nuvem atualizados, sincronizando da nuvem para local');
mergeResult.direction = 'cloud_to_local';
mergedScripts = { ...(localData.scripts || {}), ...(cloudData.scripts || {}) };
mergedDisabledScripts = { ...(localData._disabledScripts || {}), ...(cloudData._disabledScripts || {}) };
mergeResult.changes.scripts = Object.keys(cloudData.scripts || {}).length;
mergeResult.changes.disabled_scripts = Object.keys(cloudData._disabledScripts || {}).length;
const localDataToSave = {
scripts: mergedScripts,
_disabledScripts: mergedDisabledScripts,
lastSyncTimestamp: Date.now()
};
const themeValue = cloudData.theme !== undefined ? cloudData.theme : cloudData.darkMode;
if (themeValue !== undefined) {
localDataToSave.darkMode = themeValue;
}
await chrome.storage.local.set(localDataToSave);
} else {
console.log('Dados locais atualizados ou primeira sincronização, sincronizando do local para a nuvem');
mergeResult.direction = 'local_to_cloud';
mergedScripts = localData.scripts || {};
mergedDisabledScripts = localData._disabledScripts || {};
mergeResult.changes.scripts = Object.keys(mergedScripts).length;
mergeResult.changes.disabled_scripts = Object.keys(mergedDisabledScripts).length;
try {
const dataToSync = {
scripts: mergedScripts,
_disabledScripts: mergedDisabledScripts
};
if (localData.darkMode !== undefined) {
dataToSync.theme = localData.darkMode;
}
const dataStr = JSON.stringify(dataToSync);
if (dataStr.length > 100000) {
console.warn('Dados muito grandes, excedendo limite de armazenamento de sincronização do Chrome, sincronizando apenas lista de scripts desabilitados');
const minimalData = {
_disabledScripts: mergedDisabledScripts,
lastSyncTimestamp: Date.now()
};
if (localData.darkMode !== undefined) {
minimalData.theme = localData.darkMode;
}
await chrome.storage.sync.set(minimalData);
mergeResult.dataTooLarge = true;
} else {
await chrome.storage.sync.set({
...dataToSync,
lastSyncTimestamp: Date.now()
});
}
} catch (error) {
console.error('Falha ao salvar no armazenamento de sincronização do Chrome:', error);
if (error.message.includes('QUOTA_BYTES')) {
const minimalData = {
_disabledScripts: mergedDisabledScripts,
lastSyncTimestamp: Date.now()
};
if (localData.darkMode !== undefined) {
minimalData.theme = localData.darkMode;
}
await chrome.storage.sync.set(minimalData);
mergeResult.dataTooLarge = true;
} else {
throw error;
}
}
}
return mergeResult;
}
const CodeInjectionSync = {
init: initSync,
syncNow: syncData,
getStatus: getSyncStatus,
getSettings: getSyncSettings,
saveSettings: saveSyncSettings
};
if (typeof self !== 'undefined') {
self.CodeInjectionSync = CodeInjectionSync;
}
if (typeof window !== 'undefined') {
window.CodeInjectionSync = CodeInjectionSync;
}