@@ -164,8 +164,8 @@ function PhpbbWebpush() {
164164 allowBtn . addEventListener ( 'click' , ( event ) => {
165165 event . stopPropagation ( ) ;
166166 popup . style . display = 'none' ;
167- subscribeButtonHandler ( {
168- preventDefault : ( ) => { }
167+ subscribeButtonHandler ( event ) . catch ( error => {
168+ console . error ( 'Subscription handler error:' , error ) ;
169169 } ) ;
170170 } ) ;
171171 }
@@ -237,48 +237,51 @@ function PhpbbWebpush() {
237237 async function subscribeButtonHandler ( event ) {
238238 event . preventDefault ( ) ;
239239
240- subscribeButton . addEventListener ( 'click' , subscribeButtonHandler ) ;
241-
242- // Prevent the user from clicking the subscribe button multiple times.
243- const result = await Notification . requestPermission ( ) ;
244- if ( result === 'denied' ) {
245- phpbb . alert ( subscribeButton . getAttribute ( 'data-l-err' ) , subscribeButton . getAttribute ( 'data-l-msg' ) ) ;
246- return ;
247- }
240+ try {
241+ // Prevent the user from clicking the subscribe button multiple times.
242+ const result = await Notification . requestPermission ( ) ;
243+ if ( result === 'denied' ) {
244+ phpbb . alert ( subscribeButton . getAttribute ( 'data-l-err' ) , subscribeButton . getAttribute ( 'data-l-msg' ) ) ;
245+ return ;
246+ }
248247
249- const registration = await navigator . serviceWorker . getRegistration ( serviceWorkerUrl ) ;
248+ const registration = await navigator . serviceWorker . getRegistration ( serviceWorkerUrl ) ;
250249
251- // We might already have a subscription that is unknown to this instance of phpBB.
252- // Unsubscribe before trying to subscribe again.
253- if ( typeof registration !== 'undefined' ) {
254- const subscribed = await registration . pushManager . getSubscription ( ) ;
255- if ( subscribed ) {
256- await subscribed . unsubscribe ( ) ;
250+ // We might already have a subscription that is unknown to this instance of phpBB.
251+ // Unsubscribe before trying to subscribe again.
252+ if ( typeof registration !== 'undefined' ) {
253+ const subscribed = await registration . pushManager . getSubscription ( ) ;
254+ if ( subscribed ) {
255+ await subscribed . unsubscribe ( ) ;
256+ }
257257 }
258- }
259258
260- const newSubscription = await registration . pushManager . subscribe ( {
261- userVisibleOnly : true ,
262- applicationServerKey : urlB64ToUint8Array ( vapidPublicKey ) ,
263- } ) ;
259+ const newSubscription = await registration . pushManager . subscribe ( {
260+ userVisibleOnly : true ,
261+ applicationServerKey : urlB64ToUint8Array ( vapidPublicKey ) ,
262+ } ) ;
264263
265- const loadingIndicator = phpbb . loadingIndicator ( ) ;
266- fetch ( subscribeUrl , {
267- method : 'POST' ,
268- headers : {
269- 'X-Requested-With' : 'XMLHttpRequest' ,
270- } ,
271- body : getFormData ( newSubscription ) ,
272- } )
273- . then ( response => {
274- loadingIndicator . fadeOut ( phpbb . alertTime ) ;
275- return response . json ( ) ;
264+ const loadingIndicator = phpbb . loadingIndicator ( ) ;
265+ fetch ( subscribeUrl , {
266+ method : 'POST' ,
267+ headers : {
268+ 'X-Requested-With' : 'XMLHttpRequest' ,
269+ } ,
270+ body : getFormData ( newSubscription ) ,
276271 } )
277- . then ( handleSubscribe )
278- . catch ( error => {
279- loadingIndicator . fadeOut ( phpbb . alertTime ) ;
280- phpbb . alert ( ajaxErrorTitle , error ) ;
281- } ) ;
272+ . then ( response => {
273+ loadingIndicator . fadeOut ( phpbb . alertTime ) ;
274+ return response . json ( ) ;
275+ } )
276+ . then ( handleSubscribe )
277+ . catch ( error => {
278+ loadingIndicator . fadeOut ( phpbb . alertTime ) ;
279+ phpbb . alert ( ajaxErrorTitle , error ) ;
280+ } ) ;
281+ } catch ( error ) {
282+ console . error ( 'Push subscription error:' , error ) ;
283+ phpbb . alert ( subscribeButton . getAttribute ( 'data-l-err' ) , subscribeButton . getAttribute ( 'data-l-msg' ) ) ;
284+ }
282285 }
283286
284287 /**
0 commit comments