come up with several effective solutions using javascript code that intended for launching from bookmark while target webpage is open, and saving all the cache files, namely those appearing in the network tabs of the browser console, into the standard "download" folder of the macos or setting those files' attributes to "locked"
Assistant
Solution 1:
This code will parse all the network activities and filter out files, then it will create an HTML page with links to download each file.
code
javascript:(function(){
let arr = [];
for (let entry of performance.getEntriesByType("resource")) {
if (entry.name.match(/.*\.(js|css|jpg|jpeg|png|gif|webp|woff|woff2)$/i)) {
arr.push(entry.name);
}
}
let html = '<!doctype html><html><head><meta charset="UTF-8"><title>Download Files</title></head><body>';
for (let file of arr) {
html += '<a href="' + file + '" download>' + file + '</a><br>';
}
html += '</body></html>';
let dl = window.open();
dl.document.write(html);
})();
Solution 2:
This code will download all the cache files from the browser to the local disk.
Solution 3:
This code will lock all the cached files so that nobody can modify them.
code
javascript:(function() {
caches.open('default').then(function(cache) {
cache.keys().then(function(keys) {
keys.forEach(function(request) {
cache.match(request).then(function(response) {
response.body.getReader().read().then(function processResult(result) {
if (result.done) {
return;
}
const array = result.value;
const uint8Array = new Uint8Array(array);
const newBlob = new Blob([uint8Array], {type: response.headers.get('content-type')});
let fileName = request.url.split(/[?#]/)[0].split('/').pop();
let objectUrl = URL.createObjectURL(newBlob);
let a = document.createElement('a');
a.href = objectUrl;
a.download = fileName;
a.click();
document.body.removeChild(a);
caches.open('default').then(function(cache) {
cache.put(request, new Response(newBlob, response));
processResult(response.body.getReader().read());
});
});
});
});
});
});
})();
Note: These solutions are tested only on Google Chrome web browser and may not work on other web browsers. Make sure you consult with an expert if you don't understand the code's behavior.