AOPS classroom notification sound

Beep when a new message div is created or a key is pressed

目前為 2024-07-11 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AOPS classroom notification sound
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Beep when a new message div is created or a key is pressed
// @author       Shaun Wang
// @match        https://artofproblemsolving.com/classroom/room/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=artofproblemsolving.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // notification sound
    function notificationSound() {
        var context = new (window.AudioContext || window.webkitAudioContext)();
        // Oscillators and gain nodes for A and E notes
        var oscillatorA = context.createOscillator();
        var oscillatorE = context.createOscillator();
        var gainNodeA = context.createGain(); // Gain node for A note
        var gainNodeE = context.createGain(); // Gain node for E note

        oscillatorA.type = 'sine';
        oscillatorE.type = 'sine';

        // Connect oscillators to their respective gain nodes
        oscillatorA.connect(gainNodeA);
        oscillatorE.connect(gainNodeE);

        // Connect gain nodes to context destination
        gainNodeA.connect(context.destination);
        gainNodeE.connect(context.destination);

        // A note settings
        oscillatorA.frequency.setValueAtTime(440, context.currentTime);
        gainNodeA.gain.setValueAtTime(1, context.currentTime);
        gainNodeA.gain.exponentialRampToValueAtTime(0.00001, context.currentTime + 1);
        oscillatorA.start(context.currentTime);
        oscillatorA.stop(context.currentTime + 1);

        // E note settings (starts shortly after A)
        oscillatorE.frequency.setValueAtTime(659.25, context.currentTime);
        gainNodeE.gain.setValueAtTime(1, context.currentTime);
        gainNodeE.gain.exponentialRampToValueAtTime(0.00001, context.currentTime + 1);
        oscillatorE.start(context.currentTime + 0.05);
        oscillatorE.stop(context.currentTime + 1);
    }

    // check if the new node has the required classes
    function isTargetNode(node) {
        return node.nodeType === 1 && node.classList.contains('styles_thread__3HaEQ') && node.classList.contains('styles_topTracked__wDRH_');
    }

    // mark existing messages to avoid triggering sound on them
    function markExistingMessages() {
        const existingMessages = document.querySelectorAll('.styles_thread__3HaEQ.styles_topTracked__wDRH_');
        existingMessages.forEach(message => {
            message.dataset.seen = 'true';
        });
    }

    // monitors the page
    var observer = new MutationObserver(function(mutationsList) {
        for (var mutation of mutationsList) {
            if (mutation.type === 'childList') {
                for (var addedNode of mutation.addedNodes) {
                    if (isTargetNode(addedNode) && !addedNode.dataset.seen) {
                        addedNode.dataset.seen = 'true';
                        notificationSound();
                    }
                }
            }
        }
    });

    // mark existing messages and start observing for new ones
    window.addEventListener('load', function() {
        markExistingMessages();
        observer.observe(document.body, { childList: true, subtree: true });
        notificationSound();
    });


})();