Guest User

Show img-s on page in a grid

a guest
Nov 9th, 2024
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.36 KB | Source Code | 0 0
  1. javascript:(() => {
  2.     const root_class = 'my-images-overlay';
  3.     const has_root = document.querySelector('.' + root_class);
  4.     let last_big = undefined;
  5.     if (has_root) {
  6.         document.body.removeChild(has_root);
  7.     } else {
  8.         const root = document.createElement('div');
  9.         root.classList.add(root_class);
  10.         root.style = (
  11.             'width: 97vw; height: 100vh; position: fixed; z-index: 2147483647; ' +
  12.             'overflow: scroll; background: #444; padding: 10px;'
  13.         );
  14.         document.body.insertBefore(root, document.body.childNodes[0]);
  15.         const list1 = add_list(root);
  16.         const hr = document.createElement('hr');
  17.         hr.style = 'margin: 2rem 0;';
  18.         root.appendChild(hr);
  19.         const list2 = add_list(root);
  20.         for (const img of document.querySelectorAll('img')) {
  21.             let href = img.src;
  22.             if (href === '' || href.startsWith('chrome-extension://')) {continue}
  23.             if (img.srcset !== '') {
  24.                 try {
  25.                     const hrefs = img.srcset.trim().split(/\s*,\s*/)
  26.                         .map(rec => [...rec.split(/\s+/), '1x'])
  27.                         .sort((a, b) => ((+b[1].replace(/x$/, '')) - (+a[1].replace(/x$/, ''))));
  28.                     href = hrefs[0][0];
  29.                 } catch (error) { }
  30.             }
  31.             add_card(list1, list2, href, 1);
  32.         }
  33.     }
  34.     function add_list(root) {
  35.         const list = document.createElement('div');
  36.         list.classList.add('list');
  37.         list.style = 'display: flex; flex-wrap: wrap; gap: 10px;';
  38.         root.appendChild(list);
  39.         return list;
  40.     }
  41.     function add_card(list1, list2, href, attempt) {
  42.         const card = document.createElement('div');
  43.         card.classList.add('card');
  44.         card.style = (
  45.             'position: relative; display: flex; flex-direction: column; ' +
  46.             'padding: 4px; border: 1px solid #555; background: #222;'
  47.         );
  48.         const link = document.createElement('a');
  49.         link.setAttribute('href', href);
  50.         link.setAttribute('target', '_blank');
  51.         const img2 = document.createElement('img');
  52.         if (attempt === 1 && href.includes('hqdefault')) {
  53.             const href2 = href.replace('hqdefault', 'maxresdefault');
  54.             link.setAttribute('href', href2);
  55.             img2.src = href2;
  56.         } else {
  57.             img2.src = href;
  58.         }
  59.         img2.style = `max-width: 300px; max-height: 300px;`;
  60.         const cap = document.createElement('div');
  61.         cap.classList.add('cap');
  62.         img2.addEventListener('load', function () {
  63.             const w = img2.naturalWidth;
  64.             const h = img2.naturalHeight;
  65.             if (w === 120 && h === 90 && attempt === 1) {
  66.                 add_card(list1, list2, href, 2);
  67.             }
  68.             cap.innerText = (w * h / 1e6).toFixed(1).replace(/\.?0+$/, '') + ` - ${w}x${h}`;
  69.             if (w * h >= 1280 * 720) {
  70.                 list1.appendChild(card);
  71.                 last_big = card;
  72.             }
  73.         });
  74.         cap.style = (
  75.             'position: absolute; padding: 2px 4px; background: #000a; ' +
  76.             'right: 4px; bottom: 4px; font-size: 1.2rem; color:#ddd;'
  77.         );
  78.         link.appendChild(img2);
  79.         card.appendChild(link);
  80.         card.appendChild(cap);
  81.         list2.appendChild(card);
  82.     }
  83. })();
  84.  
Advertisement
Add Comment
Please, Sign In to add comment