Files
rune-docker/95-inject-javascript
Seth Van Niekerk 6272b3f1c3
All checks were successful
Build & Push Docker Image / build-and-publish (push) Successful in 27s
Test Notifications & Title Updates
2026-02-05 17:03:22 -05:00

31 lines
3.3 KiB
Plaintext

#!/usr/bin/with-contenv bash
# Inject RuneLite browser integration JavaScript into Selkies index.html
# This runs after Selkies creates the index.html file
echo "[custom-init] Injecting RuneLite browser integration..."
# Wait for Selkies to create index.html
timeout=30
while [ $timeout -gt 0 ]; do
if [ -f /usr/share/selkies/www/index.html ]; then
echo "[custom-init] Found index.html, injecting JavaScript..."
# Check if already injected
if ! grep -q "RuneLite browser integration" /usr/share/selkies/www/index.html; then
# Inject before closing body tag
sed -i 's|</body>|<script>\n// RuneLite browser integration for dynamic title updates and notifications\n(function() {\n "use strict";\n const CHECK_INTERVAL = 2000;\n let lastNotificationId = 0;\n let notificationPermission = false;\n \n // Request notification permission\n if ("Notification" in window \&\& Notification.permission === "default") {\n Notification.requestPermission().then(function(permission) {\n notificationPermission = permission === "granted";\n console.log("[RuneLite] Notification permission:", permission);\n });\n } else if (Notification.permission === "granted") {\n notificationPermission = true;\n }\n \n // Update title\n async function updateTitle() {\n try {\n const response = await fetch("/title.txt");\n if (response.ok) {\n const title = await response.text();\n if (title \&\& title.trim() \&\& document.title !== title.trim()) {\n document.title = title.trim();\n console.log("[RuneLite] Browser title updated to:", title.trim());\n }\n }\n } catch (e) {}\n }\n \n // Check for new notifications\n async function checkNotifications() {\n if (!notificationPermission) return;\n try {\n const response = await fetch("/notifications.json");\n if (response.ok) {\n const notifications = await response.json();\n for (const notif of notifications) {\n if (notif.id > lastNotificationId) {\n new Notification(notif.title, {\n body: notif.body,\n icon: "/icon.png",\n tag: "runelite-" + notif.id\n });\n lastNotificationId = notif.id;\n console.log("[RuneLite] Notification shown:", notif.title);\n }\n }\n }\n } catch (e) {}\n }\n \n setInterval(updateTitle, CHECK_INTERVAL);\n setInterval(checkNotifications, CHECK_INTERVAL);\n setTimeout(updateTitle, 1000);\n setTimeout(checkNotifications, 2000);\n console.log("[RuneLite] Browser integration loaded - title sync and notifications enabled");\n})();\n</script>\n</body>|' /usr/share/selkies/www/index.html
echo "[custom-init] JavaScript injection complete"
else
echo "[custom-init] JavaScript already injected, skipping"
fi
break
fi
sleep 1
((timeout--))
done
if [ $timeout -eq 0 ]; then
echo "[custom-init] WARNING: index.html not found after 30 seconds"
fi