Pixiv Origin Image

Pixiv原图显示

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Pixiv Origin Image
// @name:zh-cn   Pixiv 原图显示
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Pixiv原图显示
// @author       Faper
// @license      MIT
// @match        http*://www.pixiv.net/*
// @connect      i.pximg.net
// @connect      i-f.pximg.net
// @connect      i-cf.pximg.net
// @icon         http://www.pixiv.net/favicon.ico
// @grant        none
// @run-at       document-end
// ==/UserScript==

"use strict";
(function() {
    const throttle = (fn, delay = 500) => {
        let flag = true;
        return (...args) => {
            if (!flag) return;
            flag = false;
            setTimeout(() => {
                fn.apply(this, args);
                flag = true;
            }, delay);
        };
    };
    //替换原图
    const originCallback = () => {
        let imgTags = document.querySelectorAll("a img");
        for(let imgTag of imgTags) {
            const originImg = imgTag.parentElement.href;
            if(/https:\/\/i.pximg.net\/img-original\/img\/[\w|\/]+\.(?:jpg|png)/g.test(originImg)) {
                imgTag.setAttribute("src", originImg);
            }
        }
    }
    //描述图片尺寸
    let sizeFig = [];
    const sizeCallback = () => {
        let imgTags = document.querySelectorAll("figure a img");
        for(let imgTag of imgTags) {
            if(sizeFig.find(x => x == imgTag) != undefined) {
                continue;
            }
            const height = imgTag.attributes.height.value;
            const width = imgTag.attributes.width.value;
            const id = "w-h-create-by-extension";
            const found = document.getElementById(id);
            if(found){
                found.innerText = `${width}\u00D7${height}`;
            }
            else{
                const span = document.createElement("span");
                span.id = "w-h-create-by-extension";
                const textNode = document.createTextNode(`${width}\u00D7${height}`);
                span.appendChild(textNode);
                imgTag.parentNode.parentNode.insertBefore(span, imgTag.parentNode);
                sizeFig.push(imgTag);
            }
        }
    }
    const targetNode = document.querySelector("body");
    const config = {
        childList: true,
        subtree: true,
    }
    const originObserver = new MutationObserver(throttle(originCallback));
    const sizeObserver = new MutationObserver(throttle(sizeCallback));
    originObserver.observe(targetNode, config);
    sizeObserver.observe(targetNode, config);
})();