NovelAI Execution Audio Notifier

NovelAIで生成が成功 or 失敗したら音を鳴らす

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         NovelAI Execution Audio Notifier
// @namespace    http://tampermonkey.net/
// @version      2024-01-19
// @description  NovelAIで生成が成功 or 失敗したら音を鳴らす
// @description:en  This script triggers an audible notification whenever NovelAI successfully generates an image, or fails to do so.
// @author       K. K.
// @match        https://novelai.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=novelai.net
// @grant        none
// @require      http://code.jquery.com/jquery-2.2.4.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/js/ion.sound.min.js
// @license MIT
// ==/UserScript==

const $ = window.jQuery;

// 宛先が変わった?
const POST_TARGET_URL_ARRAY = [
    'https://api.novelai.net/ai/generate-image',
    'https://image.novelai.net/ai/generate-image',
];

$.ionSound({
    sounds: [
        {
            name: "button_tiny",
            volume: 0.5
        },
        {
            name: "water_droplet_2",
            volume: 0.1
        },
    ],

    path: "https://cdn.jsdelivr.net/npm/[email protected]/sounds/",
    preload: true,
    multiplay: true
});

(function() {
    'use strict';

    // 元のfetch関数を保存
    let originalFetch = window.fetch;

    // fetch関数をオーバーライド
    window.fetch = function() {
        let fetchArgs = arguments;
        let fetchUrl = fetchArgs[0];

        // 元のfetch関数を呼び出してプロミスを取得
        let fetchPromise = originalFetch.apply(this, fetchArgs);

        // POST先を見て生成先のURLなら完了時に処理を実行
        if (POST_TARGET_URL_ARRAY.includes(fetchUrl)) {
            console.log(`target url: ${fetchUrl}`);
            fetchPromise
                .then(response => {
                    if (!response.ok) {
                        console.error('Server Error:', response.url);
                        $.ionSound.play("water_droplet_2");
                    } else {
                        // console.log('Request Success:', response.url);
                        $.ionSound.play("button_tiny");
                    }
                })
                .catch(error => {
                    console.error('Request Error:', error);
                    $.ionSound.play("water_droplet_2");
                });
        }

        // オリジナルのプロミスを返す
        return fetchPromise;
    };
})();