add and hide button

This commit is contained in:
Karl 2025-07-18 17:36:25 +01:00
parent 7ad67a80f5
commit e84441e7f1
2 changed files with 26 additions and 4 deletions

View File

@ -59,10 +59,25 @@
const isLoggedIn = document.getElementById('is-logged-in').value === 'true';
navigator.serviceWorker.register('{{ url_for("static", filename="service-worker.js") }}').then(function(registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
// Check for the 'loggedin' query parameter to trigger the prompt
const urlParams = new URLSearchParams(window.location.search);
if (isLoggedIn && urlParams.has('loggedin')) {
const enableNotificationsBtn = document.getElementById('enable-notifications-btn');
function setupNotificationButton() {
registration.pushManager.getSubscription().then(function(subscription) {
if (enableNotificationsBtn) {
if (subscription) {
enableNotificationsBtn.style.display = 'none';
} else {
enableNotificationsBtn.style.display = 'block';
enableNotificationsBtn.addEventListener('click', function() {
askPermission(registration);
});
}
}
});
}
if (enableNotificationsBtn) {
setupNotificationButton();
}
}, function(err) {
console.log('ServiceWorker registration failed: ', err);
@ -143,6 +158,10 @@
.then(response => {
if (response.ok) {
console.log('Subscription saved successfully.');
const enableNotificationsBtn = document.getElementById('enable-notifications-btn');
if (enableNotificationsBtn) {
enableNotificationsBtn.style.display = 'none';
}
return response.json();
} else {
console.error('Failed to save subscription. Status:', response.status);

View File

@ -5,6 +5,9 @@
{% block content %}
<h1>Welcome {{ username }}!</h1>
<br>
<button id="enable-notifications-btn" class="btn btn-primary">Enable Notifications</button>
<br>
<br>
<h2>You have {{ accounts }} active accounts</h2>
<br>