PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / DonationForms / resources / app / utilities / getCurrentFormUrlData.js

getCurrentFormUrlData.js in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/DonationForms/resources/app/utilities/getCurrentFormUrlData.js

68 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * @since 4.17.0 Support cross-origin embeds: guard window.top and window.frameElement access with URL parameter fallbacks.
3 * @since 3.22.0 Add locale support
4 */
5 export default function getCurrentFormUrlData() {
6 const urlParams = new URLSearchParams(window.location.search);
7
8 let originUrl;
9 let isCrossOriginEmbed = false;
10
11 try {
12 originUrl = window.top.location.href;
13 } catch (e) {
14 // Cross-origin embed: the parent page URL is unreadable, so use the
15 // origin-url param passed by the embed script, then the referrer.
16 isCrossOriginEmbed = true;
17 originUrl = urlParams.get('origin-url') || document.referrer || window.location.href;
18 }
19
20 const isEmbed = window.self !== window.top;
21
22 const getEmbedId = () => {
23 if (!isEmbed) {
24 return null;
25 }
26
27 if (window.frameElement) {
28 if (window.frameElement.hasAttribute('data-givewp-embed-id')) {
29 return window.frameElement.getAttribute('data-givewp-embed-id');
30 }
31
32 return window.frameElement.id;
33 }
34
35 return urlParams.get('embed-id');
36 };
37
38 const getLocale = () => {
39 if (!isEmbed) {
40 return null;
41 }
42
43 if (window.frameElement) {
44 if (window.frameElement.hasAttribute('data-form-locale')) {
45 return window.frameElement.getAttribute('data-form-locale');
46 }
47
48 let locale = '';
49 if (window.frameElement.src) {
50 const url = new URL(window.frameElement.src);
51 locale = url.searchParams.get('locale') || '';
52 }
53
54 return locale;
55 }
56
57 return urlParams.get('locale') || '';
58 };
59
60 return {
61 originUrl,
62 isEmbed,
63 isCrossOriginEmbed,
64 embedId: getEmbedId(),
65 locale: getLocale(),
66 };
67 }
68