diff --git a/app/packs/entrypoints/controllers/push_notification_controller.js b/app/packs/entrypoints/controllers/push_notification_controller.js index 4a6e08fe7..6e3f12486 100644 --- a/app/packs/entrypoints/controllers/push_notification_controller.js +++ b/app/packs/entrypoints/controllers/push_notification_controller.js @@ -1,13 +1,13 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { - static values = { + static values = { vapidPublic: String, userLogin: Boolean }; connect() { - if(!this.userLoginValue) return; + if (!this.userLoginValue) return; let subscribed = localStorage.getItem('block-webpush-modal'); if (subscribed === 'true') { @@ -25,8 +25,10 @@ export default class extends Controller { } } } - )} - )} + ) + } + ) + } } requestPermission() { @@ -42,41 +44,41 @@ export default class extends Controller { setupPushNotifications() { const applicationServerKey = this.urlBase64ToUint8Array(this.vapidPublicValue); - console.log(this.vapidPublicValue); - - navigator.serviceWorker.register("/service-worker.js", {scope: "./" }).then((registration) => { - console.log('Service Worker registered successfully:', registration); - - registration.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: applicationServerKey - }).then((subscription) => { - - const endpoint = subscription.endpoint; - const p256dh = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh')))); - const auth = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth')))); - - fetch('/push_subscriptions', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content') - }, - body: JSON.stringify({ - subscription: { - endpoint: endpoint, - p256dh: p256dh, - auth: auth - } - }) + if (window.isSecureContext) { + navigator.serviceWorker.register("/service-worker.js", { scope: "./" }).then((registration) => { + console.log('Service Worker registered successfully:', registration); + + registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: applicationServerKey + }).then((subscription) => { + + const endpoint = subscription.endpoint; + const p256dh = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh')))); + const auth = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth')))); + + fetch('/push_subscriptions', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content') + }, + body: JSON.stringify({ + subscription: { + endpoint: endpoint, + p256dh: p256dh, + auth: auth + } + }) + }); + + localStorage.setItem('block-webpush-modal', 'true'); + document.querySelector('.webpush-modal').style.display = 'none' }); - - localStorage.setItem('block-webpush-modal', 'true'); - document.querySelector('.webpush-modal').style.display = 'none' - }); - }).catch(err => { - console.log('Service Worker registration failed:', err); - });; + }).catch(err => { + console.log('Service Worker registration failed:', err); + });; + } } close() { @@ -91,14 +93,14 @@ export default class extends Controller { urlBase64ToUint8Array(base64String) { var padding = '='.repeat((4 - base64String.length % 4) % 4); var base64 = (base64String + padding) - .replace(/\-/g, '+') - .replace(/_/g, '/'); + .replace(/\-/g, '+') + .replace(/_/g, '/'); var rawData = window.atob(base64); var outputArray = new Uint8Array(rawData.length); for (var i = 0; i < rawData.length; ++i) { - outputArray[i] = rawData.charCodeAt(i); + outputArray[i] = rawData.charCodeAt(i); } return outputArray; diff --git a/app/packs/entrypoints/controllers/wishlist_controller.js b/app/packs/entrypoints/controllers/wishlist_controller.js index b54e4f4ae..9599ec3e2 100644 --- a/app/packs/entrypoints/controllers/wishlist_controller.js +++ b/app/packs/entrypoints/controllers/wishlist_controller.js @@ -1,5 +1,5 @@ import { Controller } from "@hotwired/stimulus" -import Rails from 'rails-ujs'; +// import Rails from 'rails-ujs'; export default class extends Controller { connect() {