Skip to content

Instantly share code, notes, and snippets.

@andrewiankidd
Created July 8, 2022 18:09
Show Gist options
  • Select an option

  • Save andrewiankidd/dc6917685ca575c80bf5b4e978dbbde1 to your computer and use it in GitHub Desktop.

Select an option

Save andrewiankidd/dc6917685ca575c80bf5b4e978dbbde1 to your computer and use it in GitHub Desktop.

Revisions

  1. andrewiankidd created this gist Jul 8, 2022.
    76 changes: 76 additions & 0 deletions gallerix-scraper.user.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    // ==UserScript==
    // @name Gallerix Scraper
    // @namespace http://andrewkidd.co.uk/
    // @version 0.1
    // @description Batch grab gallerix images
    // @author You
    // @match https://gallerix.org/storeroom/*
    // @match https://gallerix.org/pic/*
    // @icon https://www.google.com/s2/favicons?sz=64&domain=gallerix.org
    // @grant GM_openInTab
    // @grant GM_download
    // @grant window.close
    // ==/UserScript==

    (function() {
    'use strict';
    const sleep = seconds => new Promise(r => setTimeout(r, seconds * 1000));

    function log(message) {
    console.log('[gallerix-scraper]', message)
    }

    // click the show all/load more button if not already clicked
    function showAllInGallery() {
    while (document.querySelector('button#lazy').style.display != 'none') {
    document.querySelector('button#lazy').click()
    }
    }

    // get all links that go to images in this storeroom
    async function getAllGalleryLinks() {
    var linkCollection = document.getElementsByTagName('a');
    var storeroomId = window.location.href.match(/storeroom\/([0-9]+)\//)[1];
    for(var i = 0; i < linkCollection.length; i++){
    var link = linkCollection[i].href;
    if (link.includes(`${storeroomId}/N`)) {
    log(`${link}?gallerix-scraper`);
    GM_openInTab(`${link}?gallerix-scraper`);
    await (1);
    }
    }
    }

    if (window.location.href.startsWith('https://gallerix.org/pic/') && window.location.hash.startsWith('#gallerix-scraper=')) {
    log('Raw Image! Downloading');

    var downloadInfo = {
    url: document.querySelector('a').href,
    name: decodeURI(window.location.hash.split('#gallerix-scraper=')[1])
    };
    log(downloadInfo);
    GM_download(downloadInfo);
    window.close();
    } else if (document.querySelectorAll('div.tab-content div.pic').length) {
    log('Gallery Page! Adding download button');

    // add download button
    document.querySelector('ul.nav-tabs').insertAdjacentHTML('beforeEnd', '<li><a class="nav-link" id="gallerix-scraper" href="#"><i class="icon fa fa-download"></i></a></li>');

    // wait for user to click download button
    document.querySelector('a#gallerix-scraper').addEventListener('click', function() {
    showAllInGallery();
    getAllGalleryLinks();
    });
    } else if (window.location.href.endsWith('?gallerix-scraper') && document.querySelectorAll('div.tab-content p.xpic').length) {
    log('Image View! Scraping raw image link');
    var artworkTitle = document.title;
    var artworkLink = document.querySelector('a#axpicd');
    var tmp = artworkLink.href.split('.');
    var filename = `${artworkTitle}.${tmp[tmp.length-1]}`
    log(`${artworkTitle} - ${artworkLink.href}`);
    artworkLink.href = `${artworkLink.href}#gallerix-scraper=${filename}`;
    artworkLink.click();
    window.close();
    }
    })();