Hi @wOxxOm, thank you for your reply.
I am using the browser's native compression method for the first time and so I don't know if I got things right. Part of the code I got from a post of yours on SO.
I believe that I already follow most of your advice.
The only thing is that in my DB there are no blobs or arrayBuffers but json objects.
Now I'm going to create a small extension to try to understand if and how the countdown starts again after pressing the button.
Creating a function that resolves a promise after 10-20 seconds should be enough to do this test.
If the countdown does not restart then it is clear that there is a bug somewhere.
This is the code from reading the db to dowload the archive file:
Do you see something strange?
//sw script
opt = {
type: "basic",
title: "Do you want to backup your data?",
message: "bla bla bla...",
iconUrl: 'img/warning.png',
buttons: [{
title: "Proceed"
}, {
title: "Not now"
}
],
requireInteraction: true
}
chrome.notifications.create('BS', opt);
chrome.notifications.onButtonClicked.addListener((id, btnIdx) => {
if (id == 'BS') {
if (btnIdx == 0) {
chrome.notifications.clear(id, _ => {
exportBusy = true;
var req = indexedDB.open('foobar'); //opening the db
req.addEventListener('success', e => {
db = e.target.result;
var objExport = { /* This is the skeleton of the object I'm going to backup */
'name':
db.name,
'version': db.version,
'objectStores': [{
name: 'mainNetworkData
',
data: [] /* it will be populated later */
}, {
name: 'otherStuff',
data: [] /* it will be populated later */
}
]
};
var arrProm = [];
var arrOs = objExport.objectStores;
for (let i = 0; i < arrOs.length; i++)
arrProm[i] = queryKey(db, arrOs[i].name, null, null); //pulling out the objectStores from db (
asynchronously)
Promise.all(arrProm).then(results => {
results.forEach((r, i) => {
arrOs[i].data = r.slice() //populating the big object...
});
chrome.windows.create({url: 'downloader.html', state: 'minimized'});
self.addEventListener('message', function onMsg(e) {
if (e.data === 'sendBlob') {
async function send(dst) {
//all heavy stuff is done after the window is created (not too stylish!)
var content = JSON.stringify(objExport);
const stream = new Response(content).body.pipeThrough(new CompressionStream('gzip'));
blob = await new Response(stream).blob();
dst.postMessage({blob}, [await blob.arrayBuffer()])
};
self.removeEventListener('message', onMsg);
send(e.source)
} else if (e.data === 'done')
exportBusy = false
})
}).catch(err => {})
})
})
} else
chrome.notifications.clear(id)
}
});
//downloader.html
<html>
<head> <script src="./script/downloader.js"></script> </head>
<body>Don't close this window.<br>
It will close automatically when the download is complete.
</body>
</html>
//downloader.jsnavigator.serviceWorker.ready.then(swr => swr.active.postMessage('sendBlob'));
navigator.serviceWorker.onmessage = async e => {
if (e.data.blob) {
let url = URL.createObjectURL(e.data.blob);
let downloadAnchorNode = document.createElement("a");
downloadAnchorNode.download = "foobar.gz";
downloadAnchorNode.href = url;
downloadAnchorNode.addEventListener("click", _ => setTimeout(_ => {
URL.revokeObjectURL(url);
e.source.postMessage('done');
window.close()
}, 500));
downloadAnchorNode.click();
downloadAnchorNode.remove()