Pixiv AI Tag

对Pixiv中的AI生成图像添加一个标注

目前為 2023-03-02 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @license MIT
// @name        Pixiv AI Tag
// @description 对Pixiv中的AI生成图像添加一个标注
// @author      BAKAOLC
// @version     0.2.4
// @icon        http://www.pixiv.net/favicon.ico
// @match       https://www.pixiv.net/*
// @namespace   none
// @grant       none
// @noframes
// ==/UserScript==

//查询间隔,时间单位为毫秒,0代表无延时
let query_delay = 0;
//是否移除AI作品的预览图
let remove_image = false;

let query_array = [];
let query_record = {};

const common_selector = "div.sc-k3uf3r-8>div>div>div>div>a";
const selector = [
    {
        url: /pixiv.net\/(cate_r18|manga|en\/$|$)/,
        sel: "ul div>div>div:nth-of-type(1)>a",
    },
    {
        url: /pixiv.net\/(en\/)?artworks/,
        sel: "ul div>div>div>a, main nav div>div>div>a",
    },
    {
        url: "pixiv.net/bookmark_new_illust",
        sel: "ul div>div>div>a"
    },
    {
        url: "pixiv.net/contest",
        sel: ".thumbnail-container>a"
    },
    {
        url: "pixiv.net/discovery",
        sel: "ul div>div>div:nth-of-type(1)>a"
    },
    {
        url: "pixiv.net/new_illust",
        sel: "ul div>div:nth-of-type(1)>div>a"
    },
    {
        url: "pixiv.net/ranking",
        sel: ".ranking-image-item>a"
    },
    {
        url: /pixiv.net\/request($|\/(complete|creators)\/(illust|manga|ugoira))/,
        sel: "ul div>div:nth-of-type(1)>a",
    },
    {
        url: /pixiv.net\/(en\/)?tags/,
        sel: "ul div>div>div>a"
    },
    {
        url: /pixiv.net\/(en\/)?users/,
        sel: "ul div>div:nth-of-type(1)>div:nth-of-type(1)>a, ul div>div div>div:nth-of-type(1)>a:nth-child(1)",
    },
    {
        url: /pixiv.net\/user\/\d+\/series\/\d+/,
        sel: "ul div>div>div>a"
    },
];

(function () {
    add_style();
    selector.map(
        (rule) =>
        (rule.sel = (rule.sel + ", " + common_selector)
            .split(",")
            .map((n) => n + '[href*="/artworks/"]:not(.add_ai_tag)')
            .join(","))
    );
    start_interval();
    new MutationObserver(function () {
        let rule = selector.find((s) => location.href.match(s.url));
        let illusts = rule ? document.querySelectorAll(rule.sel) : [];
        if (illusts.length) add_ai_tag_delay(illusts);
    }).observe(document.body, { childList: true, subtree: true });
})();

function add_style() {
    document.head.insertAdjacentHTML('beforeend', `
<style id="css_add_ai_tag">
.add_ai_tag_view {
    padding: 0px 6px;
    border-radius: 3px;
    color: rgb(255, 255, 255);
    background: rgb(96, 64, 255);
    font-weight: bold;
    font-size: 10px;
    line-height: 16px;
    user-select: none;
}
</style>
`);
}

function start_interval() {
    if (query_delay > 0) {
        setInterval(async function () {
            if (query_array.length > 0) {
                let data = query_array.shift();
                query_illust(data.id, data.node);
            }
        }, query_delay);
    }
    else {
        setInterval(async function () {
            while (query_array.length > 0) {
                let data = query_array.shift();
                query_illust(data.id, data.node);
            }
        }, 100);
    }
}

async function add_ai_tag_delay(illusts) {
    illusts.forEach(async (a) => {
        a.classList.add("add_ai_tag");
        let id = a.href.split("/artworks/").pop();
        query_array.push({
            id: id,
            node: a
        });
    });
}

async function query_illust(id, node) {
    if (!query_record[id]) {
        let json = await (
            await fetch(
                "https://www.pixiv.net/ajax/illust/" + id,
                { credentials: "omit" }
            )
        ).json();
        query_record[id] = json.body.aiType == 2;
    }
    if (query_record[id]) {
        add_ai_tag_view(node);
    }
}

function add_ai_tag_view(node) {
    if (remove_image) {
        node.querySelector("div>img").outerHTML = "<h5>AI Artwork</h5>";
    }
    let div12 = node.querySelector("div.sc-rp5asc-12");
    let div13 = div12.querySelector("div.sc-rp5asc-13");
    if (!div13) {
        div12.insertAdjacentHTML(
            "afterbegin",
            `<div class="sc-rp5asc-13 liXhix"></div>`
        );
        div13 = div12.querySelector("div.sc-rp5asc-13");
    }
    div13.insertAdjacentHTML(
        "afterbegin",
        `<div class="sc-1ovn4zb-0 add_ai_tag_view">AI</div>`
    );
}