Greasy Fork

Greasy Fork is available in English.

Pixiv AI Tag

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

当前为 2023-02-25 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @license MIT
// @name        Pixiv AI Tag
// @description 对Pixiv中的AI生成图像添加一个标注
// @author      BAKAOLC
// @version     0.0.1
// @icon        http://www.pixiv.net/favicon.ico
// @match       https://www.pixiv.net/*
// @namespace   none
// @grant       none
// @noframes
// ==/UserScript==
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
            .split(",")
            .map((n) => n + '[href*="/artworks/"]:not(.add_ai_tag)')
            .join(","))
    );
    let datas = 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(datas, illusts);
    }).observe(document.body, { childList: true, subtree: true });
})();

function start_Interval() {
    let datas = [];
    setInterval(async function () {
        if (datas.length > 0) {
            let data = datas.shift();
            let json = await (
                await fetch(
                    "https://www.pixiv.net/ajax/illust/" + data.id,
                    { credentials: "omit" }
                )
            ).json();
            if (json.body.aiType == 2) {
                data.a.querySelector("div.sc-rp5asc-13").insertAdjacentHTML(
                    "afterbegin",
                    `<div class="sc-1ovn4zb-0 add_ai_tag_view">AI</div>`
                );
            }
        }
    }, 200)
    return datas;
}

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

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>
`);
}