簡體   English   中英

Chrome擴展程序包大小

[英]Chrome Extension Package Size

我需要獲取擴展包的大小。 為此,我有以下代碼:

chrome.runtime.getPackageDirectoryEntry(function(package) {
  package.getMetadata(function(metadata) {
    console.log(metadata.size)
}) })

問題是它總是4096。始終。 這是一個錯誤,還是我缺少什么?

您必須枚舉包中的所有文件並計算大小,這是在后台/事件頁面中可用的示例:

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();
    }
}

用法:

calcPackageSize(function(size) { console.log(size) });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM