Skip to content

Instantly share code, notes, and snippets.

@kevinkub
Created August 22, 2024 20:31
Show Gist options
  • Select an option

  • Save kevinkub/20d7e089a337086a70f00aeb0ec3edd1 to your computer and use it in GitHub Desktop.

Select an option

Save kevinkub/20d7e089a337086a70f00aeb0ec3edd1 to your computer and use it in GitHub Desktop.

Revisions

  1. kevinkub created this gist Aug 22, 2024.
    23 changes: 23 additions & 0 deletions andante.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    (($0) => {
    const pages = Array.from($0.querySelectorAll(':scope > div[style^="width"]')).slice(1); // drop first page
    const urls = new Set();

    const scanNextPage = () => {
    const oldUrlsSize = urls.size;
    $0.querySelectorAll('img[src*=".svg"]').forEach(img => urls.add(img.src));

    if (oldUrlsSize !== urls.size) {
    if(pages.length > 0) {
    pages.shift().scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' });
    } else if (pages.length === 0) {
    document.body.innerHTML = [...urls].sort().map(i => '<img src="'+i+'">').join('') +
    '<style>img { max-width: 100%; max-height: 100%; page-break-after: always;</style>';
    }
    }
    };

    const observer = new MutationObserver(scanNextPage);
    observer.observe($0, { subtree: true, childList: true, attributes: true });

    scanNextPage();
    })(document.querySelector('[id*="-scroller-"]'));