Rule34 Direct Links

Provides direct links to Rule34 images in list view

נכון ליום 16-02-2020. ראה הגרסה האחרונה.

// ==UserScript==
// @name         Rule34 Direct Links
// @namespace    potato_potato
// @version      0.2.1
// @description  Provides direct links to Rule34 images in list view
// @author       potato_potato
// @match        https://rule34.xxx/index.php?page=post&s=list*
// @exclude      https://rule34.xxx/index.php?page=post&s=view*
// @grant        none
// @license      MIT
// ==/UserScript==

const TEST_BANNER = false;

(() => {
    'use strict';
    let thumbs = Array.from(document.getElementsByClassName('thumb'));
    let errors = [];

    thumbs.forEach((thumb, index) => {
        try {
            // Just in case something changes, so we don't just flat out crash on our end.
            if (thumb.children.length > 0 && thumb.children[0].children.length > 0 && thumb.children[0].children[0].tagName == 'IMG') {
                // Guaranteed due to the above check.
                let img = thumb.children[0].children[0];
                let span = document.createElement('span');
                let link = img.src;

                // TODO: Determine a better way to check for the `animated` tag, and potentially a way to determine if the
                // content is a gif or webm file.
                if (img.getAttribute('alt').includes('animated')) {
                    let gifLink = link.replace("thumbnails", "\/images").replace("thumbnail_", "").replace(/\.jpg/, ".gif");
                    let webmLink = link.replace("thumbnails", "\/images").replace("thumbnail_", "").replace(/\.jpg\?\d+/, ".webm");
                    span.innerHTML = `<br><a href=${gifLink}>gif</a> &nbsp; <a href=${webmLink}>webm</a>`;
                } else {
                    let jpegLink = link.replace("thumbnails", "\/images").replace("thumbnail_", "").replace(/\.jpg\?\d+/, ".jpeg");
                    span.innerHTML = `<br><a href=${jpegLink}>jpeg</a>`;
                }
                thumb.append(span);
            } else {
                errors.push({
                    cause: 'Incorrect data structure for `thumb` instance',
                    link: null, // We don't HAVE a link, due to bad data structure
                    thumb_index: index,
                });
            }
        } catch (e) {
            errors.push({
                cause: 'Unknown cause. Page structure change?',
                link: null,
                thumb_index: index,
            });
        }
        index++;
    });

    // TODO: Look into making the banner less hacky.
    if (errors.length > 0 || TEST_BANNER) {
        console.log("One or more errors have occured while attaching direct links to thumbnail elements:");
        console.log(errors);
        try {
            let banner_container = document.createElement('div');
            let banner_text = document.createElement('span');
            banner_text.textContent = 'Errors have occured while attaching direct links to thumbnails! Please check the console output!';
            banner_text.style['padding-top'] = '1em';
            banner_text.style['padding-bottom'] = '1em';
            banner_text.style.display = 'block';
            banner_text.style['font-size'] = 'large';
            let banner_source_info = document.createElement('span');
            banner_source_info.textContent = 'Rule34 Direct Links Userscript';
            banner_source_info.style['font-size'] = 'x-small';
            banner_source_info.style.right = '0.5em';
            banner_source_info.style.bottom = '0.25em';
            banner_source_info.style.position = 'absolute';
            banner_container.style.position = 'relative';
            banner_container.style.width = '100%';
            banner_container.style['text-align'] = 'center';
            banner_container.style.backgroundColor = '#E0A0A0';
            banner_container.appendChild(banner_text);
            banner_container.appendChild(banner_source_info);
            document.getElementById('content').prepend(banner_container);
        } catch (e) {
            console.log("Unable to attach error banner!");
            console.log(e);
        }
    }
})();