add and hide button
This commit is contained in:
parent
7ad67a80f5
commit
e84441e7f1
@ -59,10 +59,25 @@
|
|||||||
const isLoggedIn = document.getElementById('is-logged-in').value === 'true';
|
const isLoggedIn = document.getElementById('is-logged-in').value === 'true';
|
||||||
navigator.serviceWorker.register('{{ url_for("static", filename="service-worker.js") }}').then(function(registration) {
|
navigator.serviceWorker.register('{{ url_for("static", filename="service-worker.js") }}').then(function(registration) {
|
||||||
console.log('ServiceWorker registration successful with scope: ', registration.scope);
|
console.log('ServiceWorker registration successful with scope: ', registration.scope);
|
||||||
// Check for the 'loggedin' query parameter to trigger the prompt
|
const enableNotificationsBtn = document.getElementById('enable-notifications-btn');
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
|
||||||
if (isLoggedIn && urlParams.has('loggedin')) {
|
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);
|
askPermission(registration);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (enableNotificationsBtn) {
|
||||||
|
setupNotificationButton();
|
||||||
}
|
}
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
console.log('ServiceWorker registration failed: ', err);
|
console.log('ServiceWorker registration failed: ', err);
|
||||||
@ -143,6 +158,10 @@
|
|||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
console.log('Subscription saved successfully.');
|
console.log('Subscription saved successfully.');
|
||||||
|
const enableNotificationsBtn = document.getElementById('enable-notifications-btn');
|
||||||
|
if (enableNotificationsBtn) {
|
||||||
|
enableNotificationsBtn.style.display = 'none';
|
||||||
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
} else {
|
} else {
|
||||||
console.error('Failed to save subscription. Status:', response.status);
|
console.error('Failed to save subscription. Status:', response.status);
|
||||||
|
@ -5,6 +5,9 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Welcome {{ username }}!</h1>
|
<h1>Welcome {{ username }}!</h1>
|
||||||
<br>
|
<br>
|
||||||
|
<button id="enable-notifications-btn" class="btn btn-primary">Enable Notifications</button>
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
<h2>You have {{ accounts }} active accounts</h2>
|
<h2>You have {{ accounts }} active accounts</h2>
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user