Stripmunk Exporter

Adds an export button to stripmunk.be series page which exports all the entries to CSV that can easily be imported in Excel

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Stripmunk Exporter
// @name:nl      Stripmunk Exporteren
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Adds an export button to stripmunk.be series page which exports all the entries to CSV that can easily be imported in Excel
// @description:nl Voegt een knop toe in de "series" pagina van stripmunk.be waarmee de lijst naar CSV geëxporteerd kan worden om te importeren in Excel.
// @author       dbPieter
// @match        https://app.stripmunk.be/series/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=stripmunk.be
// @license      The Unlicense
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function installButton(attempt = 0) {
        const optionsContainer = document.querySelector('#optionsMenu ul li ul li');
        // Wait for the optionsContainer to render
        if (!optionsContainer) {
            if (attempt >= 10) {
                console.error('Stripmunk Exporter: optionsContainer not found after 10 attempts, giving up.');
                return;
            }
            setTimeout(() => installButton(attempt + 1), 75);
            return;
        }
        console.info('Installing Stripmunk export button')
        const addButton = document.createElement('a');
        addButton.textContent = 'Exporteren';
        addButton.addEventListener('click', () => {
            const comic = document.getElementById('menuTitel')?.innerText.trim();
            const lines = scrape(comic);
            downloadAsFile(lines, comic);
        });

        const listItem = document.createElement('li');
        listItem.append(addButton)
        optionsContainer.parentElement.append(listItem);
    }

    function scrape(comic) {
        //All selectors. Normally, only one ID is allowed to be displayed on a page, but the app doesn't seem to
        //honor that
        const selectors = {
            /**
             * A single entry containing the serial number, title and whether it's in the users collection
             */
            entries: '#comicGridCell',
            /**
             * If present in an entry, the user has this
             */
            inCollection: '#collectionCircleFull',
            /**
             * Serial number. Not the ISBN.
             */
            serialNumber: '#comicGridCellTitle',
            title: '#comicGridCellSubTitle',
        };

        const lines = ['comic;number;title;inCollection']
        for (const entry of document.querySelectorAll(selectors.entries)) {
            const serialNumber = entry.querySelector(selectors.serialNumber)?.innerText.trim().replace(/^#/, '')
            const title = entry.querySelector(selectors.title)?.innerText.trim();
            const inCollection = !!entry.querySelector(selectors.inCollection);
            lines.push([comic, serialNumber, title, inCollection].join(';'))
        }
        return lines;
    }

    function downloadAsFile(lines, comic) {
        const csvContent = lines.join('\r\n');
        const blob = new Blob([csvContent], {type: 'text/csv;charset=utf-8;'});
        const url = URL.createObjectURL(blob);
        const link = document.createElement('a');
        link.href = url;
        link.download = `${comic ?? 'stripmunk'}.csv`;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        URL.revokeObjectURL(url);
    }


    installButton();
})();