|
| 1 | +export default async function ({ feature, console }) { |
| 2 | + ScratchTools.waitForElements( |
| 3 | + "div[class*='asset-panel_wrapper_'] div[class*='selector_list-area_'] > div", |
| 4 | + function (asset) { |
| 5 | + if (asset.dataset.ste === "ste-file-size") return; |
| 6 | + asset.dataset.ste = "ste-file-size"; |
| 7 | + |
| 8 | + let content = asset.querySelector( |
| 9 | + "div[class*='sprite-selector-item_sprite-info_-'] div[class*='sprite-selector-item_sprite-details_']" |
| 10 | + ); |
| 11 | + |
| 12 | + asset.firstChild.addEventListener("mouseover", function () { |
| 13 | + if (!feature.self.enabled) return; |
| 14 | + |
| 15 | + let scratchAsset = ScratchTools.Scratch.vm.editingTarget; |
| 16 | + let targetAssets = |
| 17 | + feature.traps.gui().editorTab?.activeTabIndex === 1 |
| 18 | + ? scratchAsset.getCostumes() |
| 19 | + : scratchAsset.getSounds(); |
| 20 | + let data = targetAssets[getElementIndex(asset)].asset.data.byteLength; |
| 21 | + |
| 22 | + content.dataset.previousContent = content.textContent; |
| 23 | + content.textContent = formatBytes(data); |
| 24 | + }); |
| 25 | + |
| 26 | + asset.firstChild.addEventListener("mouseout", function () { |
| 27 | + if (content.dataset.previousContent) { |
| 28 | + content.textContent = content.dataset.previousContent; |
| 29 | + } |
| 30 | + }); |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + function getElementIndex(element) { |
| 35 | + const parent = element.parentElement; |
| 36 | + |
| 37 | + const children = parent.children; |
| 38 | + |
| 39 | + for (let i = 0; i < children.length; i++) { |
| 40 | + if (children[i] === element) { |
| 41 | + return i; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + function formatBytes(bytes, decimals = 2) { |
| 49 | + if (bytes === 0) return "0 Bytes"; |
| 50 | + |
| 51 | + const k = 1024; |
| 52 | + const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; |
| 53 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 54 | + |
| 55 | + const sizeInUnits = parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)); |
| 56 | + |
| 57 | + return `${sizeInUnits} ${sizes[i]}`; |
| 58 | + } |
| 59 | +} |
0 commit comments