// ==UserScript== // @name Grepolis Attack Planner Sound Alert // @namespace https://grepolis.com/ // @version 1.3.3 // @description Custom sound alerts for planned attacks. External audio hosts receive your IP address and may receive technical connection information. Custom audio URLs may contact third-party servers. HTTP URLs require explicit confirmation. Privacy Policy: https://github.com/maddiboy1997/grepolis-attackplaner-soundalert // @author jp1102 // @updateURL https://raw.githubusercontent.com/maddiboy1997/grepolis-attackplaner-soundalert/main/Grepolis%20Attack%20Planner%20Sound%20Alert.js // @downloadURL https://raw.githubusercontent.com/maddiboy1997/grepolis-attackplaner-soundalert/main/Grepolis%20Attack%20Planner%20Sound%20Alert.js // @match https://*.grepolis.com/game/* // @icon https://www.grepolis.com/favicon.ico // @homepageURL https://grepolis.com/ // @run-at document-start // @sandbox raw // @grant none // ==/UserScript== (function () { 'use strict'; const NOTIFY_ENDPOINT = '/game/notify'; const NOTIFICATION_TYPE = 'planed_attack'; const SOUND_VOLUME = 0.7; const STORAGE_SOUND = 'gp_attack_sound_selection'; const STORAGE_CUSTOM_URL = 'gp_attack_sound_custom_url'; const STORAGE_CUSTOM_URL_APPROVED = 'gp_attack_sound_custom_url_approved'; const PRIVACY_URLS = { de: 'https://github.com/maddiboy1997/grepolis-attackplaner-soundalert/blob/main/PRIVACY-DE.md', en: 'https://github.com/maddiboy1997/grepolis-attackplaner-soundalert/blob/main/PRIVACY-EN.md' }; function getPrivacyURL() { const isGermanServer = /^de\d+\.grepolis\.com$/i.test( window.location.hostname ); return isGermanServer ? PRIVACY_URLS.de : PRIVACY_URLS.en; } function getPrivacyLinkText() { const isGermanServer = /^de\d+\.grepolis\.com$/i.test( window.location.hostname ); return isGermanServer ? 'Datenschutzerklärung' : 'Privacy Policy'; } const SOUNDS = { sound1: 'https://gpde.innogamescdn.com/audio/effects/000001_Angriff_Version_01.ogg', sound2: 'https://gpzz.innogamescdn.com/audio/click/005009_Gold_kaufen_Version_01.ogg', sound3: 'https://gpzz.innogamescdn.com/audio/effects/003010_Premium_Version_02.ogg' }; // -------------------------------------------------- // Settings // -------------------------------------------------- let selectedSound = localStorage.getItem(STORAGE_SOUND) || 'sound1'; let customSoundURL = localStorage.getItem(STORAGE_CUSTOM_URL) || ''; let approvedCustomURL = localStorage.getItem(STORAGE_CUSTOM_URL_APPROVED) || ''; if (!['sound1', 'sound2', 'sound3', 'custom'].includes(selectedSound)) { selectedSound = 'sound1'; } let audioUnlocked = false; let initialized = false; const seenNotifications = new Set(); // -------------------------------------------------- // Custom URL validation // -------------------------------------------------- function parseCustomURL(value) { try { const url = new URL(value.trim()); if (!['http:', 'https:'].includes(url.protocol)) { return null; } if (!url.hostname) { return null; } return url; } catch (error) { return null; } } function getSoundURL() { if (selectedSound !== 'custom') { return SOUNDS[selectedSound]; } const parsedURL = parseCustomURL(customSoundURL); if (!parsedURL) { return ''; } // Previously stored HTTP URLs must be explicitly approved. if ( parsedURL.protocol === 'http:' && approvedCustomURL !== parsedURL.href ) { return ''; } return parsedURL.href; } // -------------------------------------------------- // Audio // -------------------------------------------------- const alertSound = new Audio(); alertSound.preload = 'auto'; alertSound.volume = SOUND_VOLUME; function updateSound() { const url = getSoundURL(); if (!url) { alertSound.pause(); alertSound.removeAttribute('src'); alertSound.load(); return; } if (alertSound.src !== new URL(url, location.href).href) { alertSound.src = url; alertSound.load(); } console.log('[Grepolis Sound] Selected:', selectedSound); } updateSound(); function playSound() { if (!audioUnlocked || !getSoundURL()) return; alertSound.currentTime = 0; alertSound.play().catch(error => { console.warn('[Grepolis Sound] Playback failed:', error); }); } // -------------------------------------------------- // Automatic audio activation // -------------------------------------------------- function unlockAudio() { if (audioUnlocked) return; const url = getSoundURL(); if (!url) return; alertSound.muted = true; alertSound.play().then(() => { alertSound.pause(); alertSound.currentTime = 0; alertSound.muted = false; audioUnlocked = true; console.log('[Grepolis Sound] Audio unlocked.'); }).catch(() => { alertSound.muted = false; }); } document.addEventListener('click', unlockAudio, true); document.addEventListener('keydown', unlockAudio, true); // -------------------------------------------------- // Sound selection UI // -------------------------------------------------- function createSoundSettings() { const windowElement = document.getElementById('gpwnd_1000'); if (!windowElement) return; const buttonsBar = windowElement.querySelector('.plan_details_screen .buttons_bar') || windowElement.querySelector('.game_inner_box.attack_planner .buttons_bar') || windowElement.querySelector('.buttons_bar'); if (!buttonsBar) return; let container = document.getElementById('gp-attack-sound-settings'); if (container && container.parentElement === buttonsBar) return; if (!container) { container = document.createElement('div'); container.id = 'gp-attack-sound-settings'; Object.assign(container.style, { display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap', gap: '6px', marginLeft: '12px', padding: '4px 8px', fontSize: '12px', color: '#ffcc66', verticalAlign: 'middle', position: 'relative', zIndex: '10' }); const label = document.createElement('label'); label.textContent = 'Sound:'; label.htmlFor = 'gp-attack-sound-select'; const select = document.createElement('select'); select.id = 'gp-attack-sound-select'; [ { value: 'sound1', text: 'Sound 1' }, { value: 'sound2', text: 'Sound 2' }, { value: 'sound3', text: 'Sound 3' }, { value: 'custom', text: 'Eigener Sound' } ].forEach(option => { const element = document.createElement('option'); element.value = option.value; element.textContent = option.text; select.appendChild(element); }); select.value = selectedSound; Object.assign(select.style, { padding: '3px 5px', maxWidth: '125px', cursor: 'pointer' }); const customInput = document.createElement('input'); customInput.type = 'url'; customInput.id = 'gp-attack-sound-custom-url'; customInput.placeholder = 'Sound-URL'; customInput.value = customSoundURL; Object.assign(customInput.style, { width: '220px', maxWidth: '25vw', padding: '3px 5px', display: selectedSound === 'custom' ? '' : 'none' }); // Apply button const applyButton = document.createElement('button'); applyButton.id = 'gp-attack-sound-apply'; applyButton.type = 'button'; applyButton.textContent = 'Übernehmen'; Object.assign(applyButton.style, { padding: '3px 7px', cursor: 'pointer', display: selectedSound === 'custom' ? '' : 'none' }); // Privacy policy link const privacyLink = document.createElement('a'); privacyLink.id = 'gp-attack-sound-privacy-link'; privacyLink.href = getPrivacyURL(); privacyLink.textContent = getPrivacyLinkText(); privacyLink.target = '_blank'; privacyLink.rel = 'noopener noreferrer'; Object.assign(privacyLink.style, { color: '#ff3333', textDecoration: 'underline', cursor: 'pointer', display: selectedSound === 'custom' ? '' : 'none' }); // Sound selection select.addEventListener('change', () => { selectedSound = select.value; localStorage.setItem(STORAGE_SOUND, selectedSound); const isCustom = selectedSound === 'custom'; customInput.style.display = isCustom ? '' : 'none'; applyButton.style.display = isCustom ? '' : 'none'; privacyLink.style.display = isCustom ? '' : 'none'; updateSound(); }); // Apply custom URL applyButton.addEventListener('click', () => { const parsedURL = parseCustomURL(customInput.value); if (!parsedURL) { alert('Please enter a valid HTTP or HTTPS URL.'); return; } // HTTP requires explicit user confirmation. if (parsedURL.protocol === 'http:') { const confirmed = confirm( 'Warning: This audio URL uses an unencrypted HTTP connection.\n\n' + 'The connection is not encrypted. Other parties may be able to intercept or modify the transmitted data.\n\n' + 'The server hosting the audio file can receive your IP address and technical connection information.\n\n' + 'Do you want to apply this URL anyway?' ); if (!confirmed) { return; } } // Only save the URL after validation and confirmation. customSoundURL = parsedURL.href; approvedCustomURL = parsedURL.href; localStorage.setItem( STORAGE_CUSTOM_URL, customSoundURL ); localStorage.setItem( STORAGE_CUSTOM_URL_APPROVED, approvedCustomURL ); customInput.value = customSoundURL; updateSound(); console.log('[Grepolis Sound] Custom URL applied:', customSoundURL); }); container.appendChild(label); container.appendChild(select); container.appendChild(customInput); container.appendChild(applyButton); container.appendChild(privacyLink); } buttonsBar.appendChild(container); console.log('[Grepolis Sound] Settings attached to:', buttonsBar); } // -------------------------------------------------- // Observe Grepolis UI // -------------------------------------------------- const uiObserver = new MutationObserver(() => { createSoundSettings(); }); function startUIObserver() { if (!document.documentElement) { requestAnimationFrame(startUIObserver); return; } uiObserver.observe(document.documentElement, { childList: true, subtree: true }); createSoundSettings(); } startUIObserver(); // -------------------------------------------------- // Notification processing // -------------------------------------------------- function processResponse(response) { if (!response || !response.json) return; const notifications = response.json.notifications; if (!Array.isArray(notifications)) return; // Ignore existing notifications on initial load. if (!initialized) { notifications.forEach(notification => { if (notification.id !== undefined) { seenNotifications.add(notification.id); } }); initialized = true; console.log('[Grepolis Sound] Initialized.'); return; } notifications.forEach(notification => { if (notification.type !== NOTIFICATION_TYPE) return; if (seenNotifications.has(notification.id)) return; seenNotifications.add(notification.id); console.log( '[Grepolis Sound] Planned attack detected:', notification.subject ); playSound(); }); } // -------------------------------------------------- // Request URL detection // -------------------------------------------------- function isNotifyRequest(url) { try { const parsed = new URL(url, location.href); return ( parsed.pathname === NOTIFY_ENDPOINT && parsed.searchParams.get('action') === 'fetch' ); } catch (error) { return false; } } // -------------------------------------------------- // XMLHttpRequest interception // -------------------------------------------------- const originalOpen = XMLHttpRequest.prototype.open; const originalSend = XMLHttpRequest.prototype.send; XMLHttpRequest.prototype.open = function (method, url, ...args) { this._grepolisNotifyRequest = isNotifyRequest(url); return originalOpen.call(this, method, url, ...args); }; XMLHttpRequest.prototype.send = function (...args) { if (this._grepolisNotifyRequest) { this.addEventListener('load', function () { try { processResponse(JSON.parse(this.responseText)); } catch (error) { console.warn( '[Grepolis Sound] Could not parse response:', error ); } }); } return originalSend.apply(this, args); }; // -------------------------------------------------- // Fetch interception // -------------------------------------------------- const originalFetch = window.fetch; window.fetch = async function (...args) { const response = await originalFetch.apply(this, args); const requestUrl = args[0] instanceof Request ? args[0].url : args[0]; if (isNotifyRequest(requestUrl)) { response.clone().json() .then(processResponse) .catch(error => { console.warn( '[Grepolis Sound] Fetch response error:', error ); }); } return response; }; console.log('[Grepolis Sound] Userscript loaded.'); })();