AOPS classroom notification sound

Beep when a new message div is created

当前为 2024-06-25 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AOPS classroom notification sound
// @namespace    http://tampermonkey.net/
// @version      0.21
// @description  Beep when a new message div is created
// @author       Shaun Wang
// @match        https://artofproblemsolving.com/classroom/room/*
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    // notification sound
    function notificationSound() {
    var context = new (window.AudioContext || window.webkitAudioContext)();
    var oscillator = context.createOscillator();
    var gainNode = context.createGain();

    oscillator.connect(gainNode);
    gainNode.connect(context.destination);

    oscillator.type = 'sine';

    // A (Shortened)
    oscillator.frequency.setValueAtTime(440, context.currentTime);
    gainNode.gain.setValueAtTime(1, context.currentTime);
    gainNode.gain.exponentialRampToValueAtTime(0.2, context.currentTime + 0.05);

    // E
    oscillator.frequency.setValueAtTime(659.25, context.currentTime + 0.4);
    gainNode.gain.setValueAtTime(0.2, context.currentTime + 0.4);
    gainNode.gain.exponentialRampToValueAtTime(0.00001, context.currentTime + 1);

    oscillator.start(context.currentTime);
    oscillator.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 });
    });
})();