Rule34 Direct Links

Provides direct links to Rule34 images in list view

当前为 2020-02-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
        }
    }
})();