在GitHub中列出文件大小

时间:2016-07-30 15:12:08

标签: github

GitHub文件浏览器列出了文件的名称和上次提交的信息:

GitHub file browser

有没有办法将每个文件的大小添加到这些列表中?

3 个答案:

答案 0 :(得分:16)

如果没有正式的方法,这里有一个使用GitHub's API添加尺寸信息的书签(赢得了在IE中的工作):

javascript:(function(){function u(n){var r=document.querySelector('table.files a[title="'+n.name+'"]'),i=r.closest("tr").querySelector("td.age"),t=document.createElement("td");n.type==="file"&&(t.textContent=(n.size/1024).toLocaleString("en-US",{minimumFractionDigits:1,maximumFractionDigits:1})+" KB",t.style.textAlign="right");i.parentNode.insertBefore(t,i)}var n=window.location.pathname.split("/"),t,r,i;n[0].length===0&&(n=n.slice(1));t=["https://api.github.com/repos",n[0],n[1],"contents"];n.length>2&&(n.length>4&&(t=t.concat(n.slice(4))),r=n[3]);i=t.join("/");r&&(i+="?ref="+r);console.log(i);fetch(i).then(n=>n.json()).then(n=>{Array.isArray(n)&&n.forEach(u)})})()

File sizes added

未经授权的来源:

(function () {
    "use strict";

    //Parse the current GitHub repo url. Examples:
    //  Repo root:           /Sphinxxxx/vanilla-picker
    //  Subfolder:           /Sphinxxxx/vanilla-picker/tree/master/src/css
    //  Subfolder at commit: /Sphinxxxx/vanilla-picker/tree/382231756aac75a49f046ccee1b04263196f9a22/src/css
    //  Subfolder at tag:    /Sphinxxxx/vanilla-picker/tree/v2.2.0/src/css
    //
    //If applicable, the name of the commit/branch/tag is always the 4th element in the url path.
    //Here, we put that in the "ref" variable:
    const [/* Leading slash */, owner, repo, /* "tree" */, ref, ...path] = window.location.pathname.split('/');

    //Create the URL to query GitHub's API: https://developer.github.com/v3/repos/contents/#get-contents
    //Example:
    //  https://api.github.com/repos/Sphinxxxx/vanilla-picker/contents/src/css?ref=382231756aac75a49f046ccee1b04263196f9a22
    const query = ['https://api.github.com/repos', owner, repo, 'contents'].concat(path || []),
          url = query.join('/') + (ref ? '?ref=' + ref : '');
    console.log(url);

    //https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
    fetch(url).then(r => r.json())
              .then(j => Array.isArray(j) ? j.forEach(handleFileInfo) : console.warn(j));

    function handleFileInfo(info) {
        //console.log(info);
        const link = document.querySelector('table.files a[title="' +info.name+ '"]');
        const ageCol = link.closest('tr').querySelector('td.age');

        const sizeCell = document.createElement('td');
        if(info.type === 'file') {
            //http://stackoverflow.com/a/17663871/1869660
            //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Parameters
            sizeCell.textContent = (info.size/1024).toLocaleString('en-US', { minimumFractionDigits: 1, maximumFractionDigits: 1 }) + ' KB';
            sizeCell.style.textAlign = 'right';
        }

        ageCol.parentNode.insertBefore(sizeCell, ageCol);
    }
})();

答案 1 :(得分:2)

答案 2 :(得分:-1)

不,GitHub文件浏览器不可配置。

获取额外信息意味着为GitHub传输大量额外数据(针对每个回购的每个页面),因此我不确定这是您很快就会看到的功能。

请注意'size' is an acceptable criteria for GitHub search(意味着大小信息存在且可以使用,而不仅仅是浏览文件)。

element language:xml size:100

  

将代码与单词" element"匹配这被标记为XML并且恰好有100个字节。