Geoguessr Duels Notification

Plays a sound when you find a duel

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Geoguessr Duels Notification
// @description  Plays a sound when you find a duel
// @version      1.2
// @author       Tyow#3742
// @match        *://*.geoguessr.com/*
// @license      MIT
// @namespace    https://greasyfork.org/users/1011193
// ==/UserScript==

const audio = document.createElement('audio');

/* ############################################################################### */
/* ##### DON'T MODIFY ANYTHING ABOVE HERE UNLESS YOU KNOW WHAT YOU ARE DOING ##### */
/* ############################################################################### */

audio.volume = 0.5; // Can be replaced by any number between 0 and 1

// The below can be replaced by any link that goes directly to an mp3 file, to change the sound played on join
audio.src = 'https://www.dropbox.com/scl/fi/5lzk0a5gsh56l2nrqjy28/mixkit-alert-quick-chime-766.mp3?rlkey=mb2o7bde7zquyxbmxw84nx368&dl=1';

/* ############################################################################### */
/* ##### DON'T MODIFY ANYTHING BELOW HERE UNLESS YOU KNOW WHAT YOU ARE DOING ##### */
/* ############################################################################### */

audio.id = "duelSoundAudioPlayer";
audio.preload = 'auto';


const addAudio = () => {
    document.body.appendChild(audio);
}

const playSound = () => {
    audio.play();
}

/*
* Sound is played when inLobby is true and the game_layout class is visible
* This happens when you move from the lobby to a game
* When the sound is played, it sets inLobby to false, and played to true
* While the game_layout class is visible and played is true, nothing happens
* When game is no longer visible, played is reset to false
* Which brings the values of inLobby and played back to the original values
* Allowing for the next game
*/
let inLobby = false;
let played = false

const checkStatus = () => {
    let lobbyRoot = document.querySelector("[class^='matchmaking-lobby_queueRoot__']");
    let game = document.querySelector("[class^='duels_root__']");
    let finished = document.querySelector("[class^='game-finished-ranked_container__']");
    if (played && game) {
        return
    } else if (played) {
        played = false;
    }
     if (lobbyRoot && !inLobby) {
         console.log("primed");
         inLobby = true;
         played = false;
         addAudio();
         return;
     } else if (game && !finished && inLobby) {
         console.log("playing");
         playSound();
         played = true;
         inLobby = false;
    }
}

// For testing

//document.addEventListener('mousedown', () => {
//    addAudio();
//    playSound();
//});

new MutationObserver(async (mutations) => {
    checkStatus();
}).observe(document.body, { subtree: true, childList: true });