Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Check image sizes of current page in bulk, using Javascript in console
- async function getImageContentLength(url) {
- const response = await fetch(url, { method: 'HEAD' });
- const contentLength = response.headers.get('Content-Length');
- return parseInt(contentLength, 10);
- }
- function formatSize(sizeInBytes) {
- if (sizeInBytes < 1024) {
- return sizeInBytes + ' B';
- } else if (sizeInBytes < 1024 * 1024) {
- return (sizeInBytes / 1024).toFixed(2) + ' KB';
- } else {
- return (sizeInBytes / (1024 * 1024)).toFixed(2) + ' MB';
- }
- }
- async function getImageInfo(img) {
- const width = img.width;
- const height = img.height;
- if (width >= 100 || height >= 100) {
- const src = img.src.toLowerCase();
- if (
- !src.startsWith('data:image/svg+xml') &&
- !src.endsWith('.svg') &&
- !src.includes('icon')
- ) {
- const size = await getImageContentLength(src);
- console.log(`Image: ${src}`);
- console.log(`Size: ${width}x${height}`);
- console.log(`File Size: ${formatSize(size)}`);
- console.log('---');
- }
- }
- }
- const images = document.getElementsByTagName('img');
- for (let i = 0; i < images.length; i++) {
- getImageInfo(images[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement