Skip to content

Instantly share code, notes, and snippets.

@MartinKei
Created June 2, 2025 16:44
Show Gist options
  • Save MartinKei/52ca0c9f4605201589252976fd0dcce1 to your computer and use it in GitHub Desktop.
Save MartinKei/52ca0c9f4605201589252976fd0dcce1 to your computer and use it in GitHub Desktop.
Test-Hotel Messaging
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