SkinRave Rain Notify with Worker

Toca beep quando o botão "Join" aparece na página, mesmo minimizado

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         SkinRave Rain Notify with Worker
// @namespace    http://tampermonkey.net/
// @version      2025-09-15
// @description  Toca beep quando o botão "Join" aparece na página, mesmo minimizado
// @author       SkinRave: 9726
// @match        https://skinrave.gg/*
// @grant        none
// @license      GNU
// ==/UserScript==

(function() {
    'use strict';

    let lastState = false;

    // Cria AudioContext
    const audioCtx = new (window.AudioContext || window.webkitAudioContext)();

    function beep(frequency = 440, duration = 200) {
        const oscillator = audioCtx.createOscillator();
        const gainNode = audioCtx.createGain();
        oscillator.connect(gainNode);
        gainNode.connect(audioCtx.destination);
        oscillator.type = 'square';
        oscillator.frequency.value = frequency;
        oscillator.start();
        gainNode.gain.setValueAtTime(1, audioCtx.currentTime);
        gainNode.gain.exponentialRampToValueAtTime(0.0001, audioCtx.currentTime + duration / 1000);
        oscillator.stop(audioCtx.currentTime + duration / 1000);
    }

    function beepRepeat(frequency = 440, duration = 200, times = 5, interval = 300) {
        for (let i = 0; i < times; i++) {
            setTimeout(() => beep(frequency, duration), i * interval);
        }
    }

    // Função para limpar o texto e converter em número
    function parseValor(texto) {
        let limpo = texto.replace(/,/g, '').replace(/[^\d.]/g, '');
        return parseFloat(limpo) || 0;
    }

    // Worker inline para rodar interval sem throttling
    const workerCode = `
        self.onmessage = function(e) {
            const interval = e.data.interval || 2000;
            setInterval(() => {
                self.postMessage("tick");
            }, interval);
        };
    `;
    const worker = new Worker(URL.createObjectURL(new Blob([workerCode], {type: 'application/javascript'})));
    worker.postMessage({ interval: 2000 });

    // Recebe "tick" do worker
    worker.onmessage = () => {
        let span = document.querySelectorAll("span")[92];
        if(!span) return;
        var tokens = parseValor(span.textContent.trim());

        const button = document.querySelector('button[aria-label="join-rain-button"] span.inline-flex');
        const exists = button && button.textContent.trim() === "Join";

        if (exists && !lastState) {
            if(tokens <= 500.00)
                beepRepeat();
            else if(tokens > 500.00 && tokens < 1000.00)
                beepRepeat(660, 100, 5, 200);
            else
                beepRepeat(880, 100, 3, 200);
        }
        lastState = exists;
    };

})();