I need to get the size of the extension package. To do that, I have this code:
chrome.runtime.getPackageDirectoryEntry(function(package) {
package.getMetadata(function(metadata) {
console.log(metadata.size)
}) })
The problem is that it is always 4096. Always. Is this a bug, or am I missing something?
You'll have to enumerate all files in the package and calculate the size, here's an example that works in a background/event page:
function calcPackageSize(callback) {
var totalSize = 0;
var queue = [], xhr = new XMLHttpRequest();
xhr.responseType = "blob";
xhr.onload = function() {
totalSize += this.response.size;
calcFile();
};
chrome.runtime.getPackageDirectoryEntry(function(root) {
var rootDirNameLength = root.fullPath.length;
calcDir(root);
function calcDir(dir) {
dir.createReader().readEntries(function(entries) {
entries.forEach(function(entry) {
if (entry.isFile) {
queue.push(entry.fullPath.substr(rootDirNameLength));
!xhr.readyState && calcFile(); // start the request chain
} else {
calcDir(entry);
}
});
});
}
});
function calcFile() {
if (!queue.length)
return callback && callback(totalSize);
xhr.open("HEAD", queue.pop());
xhr.send();
}
}
Usage:
calcPackageSize(function(size) { console.log(size) });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.