-
Notifications
You must be signed in to change notification settings - Fork 2k
/
view.js
49 lines (46 loc) · 1.68 KB
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
document.addEventListener( 'DOMContentLoaded', function () {
let premiumContentJWTToken = '';
/**
*
* @param {event} eventFromIframe - message event that gets emitted in the checkout iframe.
* @listens message
*/
function handleIframeResult( eventFromIframe ) {
if ( eventFromIframe.origin === 'https://subscribe.wordpress.com' && eventFromIframe.data ) {
const data = JSON.parse( eventFromIframe.data );
if ( data && data.result && data.result.jwt_token ) {
// We save the token for now, doing nothing.
premiumContentJWTToken = data.result.jwt_token;
// We will also set this in a cookie - just in case. This will be reloaded in the refresh, when user clicks OK.
// But user can close the browser window before clicking OK. IN that case, we want to leave a cookie behind.
const date = new Date();
date.setTime( date.getTime() + 365 * 24 * 60 * 60 * 1000 );
document.cookie =
'jp-premium-content-session' +
'=' +
premiumContentJWTToken +
'; expires=' +
date.toGMTString() +
'; path=/';
}
if ( data && data.action === 'close' && premiumContentJWTToken ) {
document.location.href = updateQueryStringParameter(
document.location.href,
'token',
premiumContentJWTToken
);
}
}
}
function updateQueryStringParameter( uri, key, value ) {
const re = new RegExp( '([?&])' + key + '=.*?(&|$)', 'i' );
const separator = uri.indexOf( '?' ) !== -1 ? '&' : '?';
if ( uri.match( re ) ) {
return uri.replace( re, '$1' + key + '=' + value + '$2' );
}
return uri + separator + key + '=' + value;
}
if ( typeof window !== 'undefined' ) {
window.addEventListener( 'message', handleIframeResult, false );
}
} );