ChatGPT play sound when finish generating

Plays a custom chime when chatgpt finishes

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        ChatGPT play sound when finish generating
// @namespace   http://tampermonkey.net/
// @version     1.0
// @description Plays a custom chime when chatgpt finishes
// @author      Salvador Martinez
// @match       https://chatgpt.com/*
// @grant       none
// ==/UserScript==

(function() {
    'use strict';

    let isGenerating = false;

    function logEvent(message, type = 'info') {
        const timestamp = new Date().toLocaleTimeString();
        const styles = {
            info: 'color: blue; font-weight: bold;',
            success: 'color: green; font-weight: bold;',
            warning: 'color: orange; font-weight: bold;',
            error: 'color: red; font-weight: bold;'
        };
        if (type === 'warning' || type === 'error' || type === 'success' || message.includes('started') || message.includes('finished')) {
            console.log(`%c[Task Finished - ${timestamp}] ${message}`, styles[type] || styles.info);
        }
    }

    function playCustomChime() {
        const AudioContext = window.AudioContext || window.webkitAudioContext;
        const context = new AudioContext();
        const gainNode = context.createGain();
        gainNode.connect(context.destination);
        gainNode.gain.value = 0.2; // Adjust volume as needed

        // Frequencies for the chime
        const frequencies = [659.26, 659.26, 659.26, 987.76]; // E5, E5, E5, B5
        const durations = [150, 150, 150, 300]; // Note durations

        let startTime = context.currentTime;

        frequencies.forEach((freq, index) => {
            const oscillator = context.createOscillator();
            oscillator.connect(gainNode);
            oscillator.type = 'triangle'; // Triangle wave for that custom sound
            oscillator.frequency.value = freq;
            oscillator.start(startTime);
            oscillator.stop(startTime + durations[index] / 1000);
            startTime += durations[index] / 1000;
        });

    }

    function checkGeneration() {
        const generatingButton = document.querySelector('button[data-testid="stop-button"]');

        if (generatingButton) {
            if (!isGenerating) {
                isGenerating = true;
            }
        } else {
            if (isGenerating) {
                isGenerating = false;
                playCustomChime();
            }
        }
    }

    setInterval(checkGeneration, 500);
})();