handling deeplinks

This commit is contained in:
cpu
2025-03-24 01:21:53 +01:00
parent df1e316930
commit d959f4929d

94
apps.js
View File

@@ -677,15 +677,34 @@ deletePlayerButton.addEventListener('click', () => {
audioManager.play('modalClose');
});
// Flic button action handler - Parse URL parameters and execute corresponding actions
function handleDeepLink() {
if (!window.location.hash) return;
// Function to get action from URL parameters (search or hash)
function getActionFromUrl() {
// Check for action in both search params and hash
const searchParams = new URLSearchParams(window.location.search);
const hashParams = new URLSearchParams(window.location.hash.substring(1));
// Parse the hash to get action parameters
const params = new URLSearchParams(window.location.hash.substring(1));
const action = params.get('action');
// First check search params (for direct curl or navigation)
const searchAction = searchParams.get('action');
if (searchAction) {
console.log('Found action in search params:', searchAction);
return searchAction;
}
console.log('Received action from deep link:', action);
// Then check hash params (existing deep link mechanism)
const hashAction = hashParams.get('action');
if (hashAction) {
console.log('Found action in hash params:', hashAction);
return hashAction;
}
return null;
}
// Function to handle deep link actions
function handleActionFromUrl(action) {
if (!action) return;
console.log('Processing action:', action);
// Execute action based on the parameter
switch (action) {
@@ -731,29 +750,67 @@ function handleDeepLink() {
default:
console.log('Unknown action:', action);
}
// Clear the hash to prevent duplicate actions if page is refreshed
history.replaceState(null, null, ' ');
}
// Function to handle deep links from URL or Flic button
function handleDeepLink() {
// Get action from URL parameters
const action = getActionFromUrl();
// Process the action if found
if (action) {
handleActionFromUrl(action);
// Clear the parameters to prevent duplicate actions if page is refreshed
if (window.history && window.history.replaceState) {
// Create new URL without the action parameter
const newUrl = window.location.pathname;
window.history.replaceState({}, document.title, newUrl);
}
}
}
// Listen for service worker messages (for receiving actions while app is running)
navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data && event.data.type === 'ACTION') {
console.log('Received action from service worker:', event.data.action);
handleActionFromUrl(event.data.action);
}
});
// Service Worker Registration
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then(registration => {
console.log('ServiceWorker registered: ', registration);
// Check for and handle deep links after service worker is ready
handleDeepLink();
})
.catch(error => {
console.log('ServiceWorker registration failed: ', error);
// Still try to handle deep links even if service worker failed
handleDeepLink();
});
});
} else {
// If service workers aren't supported, still handle deep links
window.addEventListener('load', handleDeepLink);
}
// Check for deep links when the page loads
window.addEventListener('load', handleDeepLink);
// Also check for hash changes (needed for handling link activation when app is already open)
window.addEventListener('hashchange', handleDeepLink);
window.addEventListener('hashchange', () => {
console.log('Hash changed, checking for actions');
handleDeepLink();
});
// Also check for navigation events that might include search parameters
window.addEventListener('popstate', () => {
console.log('Navigation occurred, checking for actions');
handleDeepLink();
});
// Make sure to handle rotation by adding window event listener for orientation changes
window.addEventListener('orientationchange', () => {
@@ -783,4 +840,11 @@ function cleanupCameraData() {
}
// Initialize the app
loadData();
loadData();
// Process URL parameters on initial load (needed for direct curl requests)
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', handleDeepLink);
} else {
handleDeepLink();
}