Chin Fast Image Downloader

One click to download current hovered image/webmeme

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Chin Fast Image Downloader
// @namespace    https://greasyfork.org/en/users/86284-benjababe
// @version      1.04
// @description  One click to download current hovered image/webmeme
// @author       Benjababe

// @match        https://boards.4channel.org/*
// @match        https://arch.b4k.co/*
// @match        https://archived.moe/*
// @match        https://archive.nyafuu.org/*
// @match        https://archive.wakarimasen.moe/*
// @match        https://desuarchive.org/*
// @match        https://warosu.org/*

// @grant        GM_download
// ==/UserScript==

// jshint esversion: 6

(function () {
    'use strict';

    const HOTKEY = "Pause";

    document.onkeydown = (e) => {
        // key can be whatever you want, I choose pause as it's what I bound my mouse side keys to
        if (e.code === HOTKEY) {
            // get all elements hovered
            let els = document.querySelectorAll(":hover");
            els.forEach((el) => {
                // only download for images
                // for webms, it works only on its thumbnail
                if (el.tagName.toLowerCase() === "img") {
                    // link to original image/webmeme usually is the parent <a> element
                    let parent = el.parentNode,
                        url = parent.href,
                        filename = HDFilenameFromURL(url);
                    GM_download(url, filename);
                }
            });
        }
    }

    // eg. ".../1622014662736s.jpg -> 1622014662736.jpg"
    // this function shouldn't ever need to be called, it's ran just in case
    let HDFilenameFromURL = (url) => {
        let SDFilename = url.split("/").pop(),
            re = /([0-9]{1,})[s]{0,}([.a-zA-Z]{1,})/,
            match = SDFilename.match(re);

        return match[1] + match[2];
    }
})();