Created
June 2, 2025 16:44
-
-
Save MartinKei/52ca0c9f4605201589252976fd0dcce1 to your computer and use it in GitHub Desktop.
Test-Hotel Messaging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendReadyMessage() { | |
if (window.opener) { | |
// Send "ready" message to the parent window that opened this page | |
window.opener.postMessage('ready', 'https://hotel.keib.dev'); | |
console.log('Sent "ready" message to parent window'); | |
} else if (window.parent && window.parent !== window) { | |
// Alternative: if this page is in an iframe instead of popup | |
window.parent.postMessage('ready', 'https://hotel.keib.dev'); | |
console.log('Sent "ready" message to parent frame'); | |
} else { | |
console.log('No parent window or opener found'); | |
} | |
} | |
// Function to handle received messages | |
function handleMessage(event) { | |
// Verify the message is from the expected origin | |
if (event.origin !== 'https://hotel.keib.dev') { | |
console.log('Received message from unexpected origin:', event.origin); | |
return; | |
} | |
try { | |
// Parse the received consent data | |
const consentData = JSON.parse(event.data); | |
// Log the received consent object to console | |
console.log('Received consent data:', consentData); | |
console.log('Consent details:'); | |
console.log('- Marketing:', consentData.marketing); | |
console.log('- Statistics:', consentData.statistics); | |
console.log('- Preferences:', consentData.preferences); | |
console.log('- Necessary:', consentData.necessary); | |
console.log('- Stamp:', consentData.stamp); | |
if (typeof window.CookiebotDialog.submitConsent === 'function') { | |
window.CookiebotDialog.submitConsent( | |
consentData.necessary, | |
consentData.preferences, | |
consentData.statistics, | |
consentData.marketing | |
); | |
console.log('Applied consent via CookiebotDialog.submitConsent'); | |
} | |
// You can add additional processing here | |
// For example, store the consent data or use it to configure tracking | |
} catch (error) { | |
console.error('Failed to parse consent data:', error); | |
console.log('Raw message data:', event.data); | |
} | |
} | |
// Set up message listener | |
window.addEventListener('message', handleMessage); | |
// Send ready message when page is loaded | |
if (document.readyState === 'loading') { | |
document.addEventListener('DOMContentLoaded', sendReadyMessage); | |
} else { | |
sendReadyMessage(); | |
} | |
// Also send ready message when window is fully loaded (as backup) | |
window.addEventListener('load', sendReadyMessage); | |
console.log('Booking page consent recipient script initialized'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment