ChatGTP answer alert

Notify when ChatGPT is done answering.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGTP answer alert
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Notify when ChatGPT is done answering.
// @author       Jonathan Lepage
// @match        https://chatgpt.com/c/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    console.log("Answer detection script")
    let thinking = false
    setInterval(() => {
        //console.log("Checking for answer. hidden=", document.hidden, " hasFocus=", document.hasFocus())
        if (document.querySelector('button[aria-label="Stop streaming"]')) {
            if (!thinking) {
                thinking = true
                console.log("Thinking")
            }
        } else {
            if (thinking) {
                thinking = false
                console.log("Answer is ready")
                if (document.hidden || !document.hasFocus()) {
                    console.log("Tab is not focused: Showing notification")
                    const notif = new Notification("Answer is ready")
                    notif.onclick = () => {
                        window.focus();
                        notif.close();
                    };
                    chime()
                } else {
                    console.log("Tab is focused: Skipping notification")
                }
            }
        }
    }, 100)

    function chime() {
        const context = new AudioContext()
        const gainNode = context.createGain()
        gainNode.connect(context.destination)
        gainNode.gain.value = 0.2

        const biquadFilter = context.createBiquadFilter()
        biquadFilter.type = "lowpass"
        biquadFilter.frequency.value = 2000
        biquadFilter.connect(gainNode)

        const notes = [0, 12, 24]

        let startTime = context.currentTime
        for (const [index, note] of notes.entries()) {
            const freq = 440 * Math.pow(2, note / 12)
            const duration = 0.06

            const oscillator = context.createOscillator()
            oscillator.connect(biquadFilter)
            oscillator.type = "sawtooth"
            oscillator.frequency.value = freq
            oscillator.start(startTime)
            oscillator.stop(startTime + duration)
            startTime += duration
        }
    }
})();