status update

This commit is contained in:
cpu
2025-03-30 23:09:41 +02:00
parent c8991b81fa
commit 5ff0b07d96
3 changed files with 41 additions and 85 deletions

View File

@@ -177,6 +177,26 @@ async function sendSubscriptionToServer(subscription, buttonId) {
if (response.ok) {
const result = await response.json();
console.log('Subscription sent successfully:', result.message);
// Update the UI to show subscription status as active
const subscriptionStatusElement = document.getElementById('subscriptionStatus');
if (subscriptionStatusElement) {
subscriptionStatusElement.textContent = 'active';
subscriptionStatusElement.className = 'status-active';
// Enable unsubscribe button when subscription is active
const unsubscribeButton = document.getElementById('pushUnsubscribeButton');
if (unsubscribeButton) unsubscribeButton.disabled = false;
// Change subscribe button text to "Re-subscribe"
const resubscribeButton = document.getElementById('pushResubscribeButton');
if (resubscribeButton) resubscribeButton.textContent = 'Re-subscribe';
// Enable simulate button when subscription is active
const simulateButton = document.getElementById('simulateClickButton');
if (simulateButton) simulateButton.disabled = false;
}
// Success alert removed as requested
} else {
let errorMsg = `Server error: ${response.status}`;
@@ -227,88 +247,18 @@ export function handleFlicAction(action, buttonId, timestamp, batteryLevel) {
// --- Initialization ---
// Initialize PushFlic with action handlers
export function initPushFlic(handlers) {
// Store the handlers passed from app.js
if (handlers && Object.keys(handlers).length > 0) {
actionHandlers = handlers;
console.log('[PushFlic] Registered action handlers:', Object.keys(actionHandlers));
console.log('[PushFlic] Stored action handlers:', Object.keys(actionHandlers));
} else {
console.warn('[PushFlic] No action handlers provided to initPushFlic, actions will not work!');
}
// No longer auto-subscribe when permission is granted
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
console.log('[PushFlic] Permission granted, but not automatically subscribing.');
console.log('[PushFlic] User can subscribe through "Push notification settings" menu.');
// subscribeToPush(); // Removed automatic subscription
} else {
console.log('[PushFlic] Notification permission not granted.');
}
});
});
console.warn('[PushFlic] No action handlers provided to initPushFlic!');
}
}
// New function to manually trigger the subscription process
export function setupPushNotifications() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
console.log('[PushFlic] Manually triggering push subscription process...');
console.log('[PushFlic] Manually triggering push notification setup');
subscribeToPush();
});
} else {
console.error('[PushFlic] Service workers not supported, cannot subscribe');
alert('Your browser does not support push notifications.');
}
}
// Function to force re-authentication even if credentials exist
export function forceCredentialsPrompt() {
// Get credentials from the form fields if available
const usernameField = document.getElementById('pushUsername');
const passwordField = document.getElementById('pushPassword');
let credentialsUpdated = false;
if (usernameField && passwordField && usernameField.value.trim() && passwordField.value.trim()) {
// Save the entered credentials to localStorage
const credentials = {
username: usernameField.value.trim(),
password: passwordField.value.trim()
};
localStorage.setItem('basicAuthCredentials', JSON.stringify(credentials));
console.log('[PushFlic] Saved credentials from form fields');
credentialsUpdated = true;
} else {
// Check if we have stored credentials
try {
const storedAuth = localStorage.getItem('basicAuthCredentials');
if (storedAuth) {
const credentials = JSON.parse(storedAuth);
if (credentials.username && credentials.password) {
// We have stored credentials, no need to update
console.log('[PushFlic] Using stored credentials');
credentialsUpdated = true;
// Update the form fields if they exist
if (usernameField && passwordField) {
usernameField.value = credentials.username;
passwordField.value = credentials.password;
}
}
}
} catch (error) {
console.error('[PushFlic] Error accessing stored credentials:', error);
}
}
if (!credentialsUpdated) {
console.log('[PushFlic] No valid credentials available');
}
// Trigger the subscription process
setupPushNotifications();
}

View File

@@ -35,10 +35,6 @@ export function setupPushNotifications() {
pushFlic.setupPushNotifications();
}
export function forceCredentialsPrompt() {
pushFlic.forceCredentialsPrompt();
}
// --- Handle Messages from Service Worker ---
export function flicMessageHandler(event) {

View File

@@ -1,5 +1,5 @@
// pushSettingsUI.js - UI handling for push notification settings
import { setupPushNotifications, forceCredentialsPrompt } from '../services/serviceWorkerManager.js';
import { setupPushNotifications } from '../services/serviceWorkerManager.js';
import { FLIC_BUTTON_ID } from '../config.js';
// --- DOM Elements ---
@@ -265,7 +265,7 @@ async function unsubscribeFromPush() {
}
}
// Force subscription to push notifications
// Subscribe to push notifications
async function resubscribeToPush() {
try {
let username = elements.pushUsername.value.trim();
@@ -291,8 +291,20 @@ async function resubscribeToPush() {
}
}
// Check if we have credentials, show alert if missing
if (!username || !password) {
console.log('No credentials available. Showing alert.');
alert('Please enter your username and password to subscribe.');
return;
}
// Save the credentials to localStorage
const credentials = { username, password };
localStorage.setItem('basicAuthCredentials', JSON.stringify(credentials));
console.log('Saved credentials to localStorage');
// Use the credentials to subscribe
await forceCredentialsPrompt();
await setupPushNotifications();
// Wait a moment for the subscription to complete
await new Promise(resolve => setTimeout(resolve, 500));
@@ -324,8 +336,6 @@ async function resubscribeToPush() {
}
// Fall back to the standard update function
await updateSubscriptionStatus();
alert('Subscription failed. Please check your credentials and try again.');
}
} catch (error) {
console.error('Error subscribing:', error);