← All changes
|
modules/comments/subscription-modal-on-comment/subscription-modal.js
+34
-20
13.5.2
→
16.3-beta
View file →
| @@ -1,4 +1,5 @@ | ||
| 1 | +/* global subscriptionData */ | |
| 1 | 2 | document.addEventListener( 'DOMContentLoaded', function () { |
| 2 | 3 | const modal = document.getElementsByClassName( 'jetpack-subscription-modal' )[ 0 ]; |
| 3 | 4 | |
| 4 | 5 | if ( ! modal ) { |
| @@ -17,12 +18,20 @@ | ||
| 17 | 18 | if ( destinationUrl.hostname !== window.location.hostname ) { |
| 18 | 19 | return; |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | - localStorage.setItem( 'jetpack-subscription-modal-on-comment-scroll-to', destinationUrl.hash ); | |
| 22 | + try { | |
| 23 | + localStorage.setItem( | |
| 24 | + 'jetpack-subscription-modal-on-comment-scroll-to', | |
| 25 | + destinationUrl.hash | |
| 26 | + ); | |
| 27 | + } catch { | |
| 28 | + // Ok if we can't set it. | |
| 29 | + } | |
| 22 | 30 | |
| 23 | - // For avoiding Firefox reload, we need to force reload bypassing the cache. | |
| 24 | - window.location.reload( true ); | |
| 31 | + // Add cache-busting parameter | |
| 32 | + destinationUrl.searchParams.set( '_ctn', Date.now() ); | |
| 33 | + window.location.href = destinationUrl.toString(); | |
| 25 | 34 | } |
| 26 | 35 | |
| 27 | 36 | function JetpackSubscriptionModalOnCommentMessageListener( event ) { |
| 28 | 37 | let message = event && event.data; |
| @@ -28,9 +37,9 @@ | ||
| 28 | 37 | let message = event && event.data; |
| 29 | 38 | if ( typeof message === 'string' ) { |
| 30 | 39 | try { |
| 31 | 40 | message = JSON.parse( message ); |
| 32 | - } catch ( err ) { | |
| 41 | + } catch { | |
| 33 | 42 | return; |
| 34 | 43 | } |
| 35 | 44 | } |
| 36 | 45 | |
| @@ -40,24 +49,26 @@ | ||
| 40 | 49 | if ( type !== 'subscriptionModalShow' || typeof data.url === 'undefined' ) { |
| 41 | 50 | return; |
| 42 | 51 | } |
| 43 | 52 | |
| 44 | - if ( ! event.origin.includes( window.location.host ) ) { | |
| 53 | + if ( subscriptionData.homeUrl !== event.origin ) { | |
| 45 | 54 | return; |
| 46 | 55 | } |
| 47 | 56 | |
| 48 | 57 | if ( data.email ) { |
| 49 | 58 | const emailInput = document.querySelector( |
| 50 | - '.jetpack-subscription-modal__modal-content input[type=email]' | |
| 59 | + '.jetpack-subscription-modal__modal-content input[type="email"]' | |
| 51 | 60 | ); |
| 52 | 61 | if ( ! emailInput ) { |
| 62 | + reloadOnCloseSubscriptionModal( data.url ); | |
| 53 | 63 | return; |
| 54 | 64 | } |
| 55 | 65 | |
| 56 | 66 | const appSource = document.querySelector( |
| 57 | - '.jetpack-subscription-modal__modal-content input[name=app_source]' | |
| 67 | + '.jetpack-subscription-modal__modal-content input[name="app_source"]' | |
| 58 | 68 | ); |
| 59 | 69 | if ( ! appSource ) { |
| 70 | + reloadOnCloseSubscriptionModal( data.url ); | |
| 60 | 71 | return; |
| 61 | 72 | } |
| 62 | 73 | |
| 63 | 74 | emailInput.value = data.email; |
| @@ -67,22 +78,26 @@ | ||
| 67 | 78 | } |
| 68 | 79 | } |
| 69 | 80 | |
| 70 | 81 | if ( ! hasLoaded ) { |
| 71 | - const storedCount = parseInt( | |
| 72 | - sessionStorage.getItem( 'jetpack-subscription-modal-shown-count' ) | |
| 73 | - ); | |
| 74 | - const showCount = ( isNaN( storedCount ) ? 0 : storedCount ) + 1; | |
| 75 | - sessionStorage.setItem( 'jetpack-subscription-modal-shown-count', showCount ); | |
| 82 | + try { | |
| 83 | + const storedCount = parseInt( | |
| 84 | + sessionStorage.getItem( 'jetpack-subscription-modal-shown-count' ) | |
| 85 | + ); | |
| 86 | + const showCount = ( isNaN( storedCount ) ? 0 : storedCount ) + 1; | |
| 87 | + sessionStorage.setItem( 'jetpack-subscription-modal-shown-count', showCount ); | |
| 76 | 88 | |
| 77 | - if ( showCount > 5 ) { | |
| 78 | - new Image().src = | |
| 79 | - document.location.protocol + | |
| 80 | - '//pixel.wp.com/g.gif?v=wpcom-no-pv&x_jetpack-subscribe-modal-comm=hidden_views_limit&r=' + | |
| 81 | - Math.random(); | |
| 89 | + if ( showCount > 5 ) { | |
| 90 | + new Image().src = | |
| 91 | + document.location.protocol + | |
| 92 | + '//pixel.wp.com/g.gif?v=wpcom-no-pv&x_jetpack-subscribe-modal-comm=hidden_views_limit&r=' + | |
| 93 | + Math.random(); | |
| 82 | 94 | |
| 83 | - reloadOnCloseSubscriptionModal( data.url ); | |
| 84 | - return; | |
| 95 | + reloadOnCloseSubscriptionModal( data.url ); | |
| 96 | + return; | |
| 97 | + } | |
| 98 | + } catch { | |
| 99 | + // Ignore any errors. | |
| 85 | 100 | } |
| 86 | 101 | |
| 87 | 102 | new Image().src = |
| 88 | 103 | document.location.protocol + |
| @@ -91,9 +106,8 @@ | ||
| 91 | 106 | |
| 92 | 107 | modal.classList.toggle( 'open' ); |
| 93 | 108 | hasLoaded = true; |
| 94 | 109 | redirectUrl = data.url; |
| 95 | - return; | |
| 96 | 110 | } |
| 97 | 111 | } |
| 98 | 112 | |
| 99 | 113 | window.addEventListener( 'message', JetpackSubscriptionModalOnCommentMessageListener ); |