buildOnLoadEndScript function
Implementation
String buildOnLoadEndScript(String channelName, String baseChatbotUrl, String chatIframeUrl) {
return '''
if (!document.querySelector('meta[name="viewport"]')) {
var meta = document.createElement('meta');
meta.name = 'viewport';
meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';
document.getElementsByTagName('head')[0].appendChild(meta);
}
if (!document.querySelector('#injectedStyle')) {
var style = document.createElement('style');
style.id = 'injectedStyle';
style.textContent = "html, body, #root, #__next { height: 100% !important; min-height: 100% !important; overflow: hidden !important; }";
document.head.appendChild(style);
}
// Mirror console logs to native via the WebView channel for debugging.
(function() {
if (!window.__RBLYN_CONSOLE_PATCHED__) {
window.__RBLYN_CONSOLE_PATCHED__ = true;
var origLog = console.log;
var origWarn = console.warn;
var origError = console.error;
function sendToNative(level, args) {
try {
var parts = [];
for (var i = 0; i < args.length; i++) {
var v = args[i];
if (typeof v === 'string') {
parts.push(v);
} else {
try {
parts.push(JSON.stringify(v));
} catch (e) {
parts.push(String(v));
}
}
}
window.$channelName.postMessage(JSON.stringify({
type: 'WEB_CONSOLE',
level: level,
message: parts.join(' ')
}));
} catch (e) {}
}
console.log = function() {
sendToNative('log', arguments);
if (origLog) origLog.apply(console, arguments);
};
console.warn = function() {
sendToNative('warn', arguments);
if (origWarn) origWarn.apply(console, arguments);
};
console.error = function() {
sendToNative('error', arguments);
if (origError) origError.apply(console, arguments);
};
}
})();
// Debug: mirror window \"message\" events to native so we can see what
// the web app actually receives from window.postMessage.
(function() {
if (!window.__RBLYN_MESSAGE_DEBUG_PATCHED__) {
window.__RBLYN_MESSAGE_DEBUG_PATCHED__ = true;
window.addEventListener('message', function(event) {
try {
window.$channelName.postMessage(JSON.stringify({
type: 'WEB_MESSAGE_DEBUG',
data: event.data
}));
} catch (e) {}
}, false);
}
})();
window.sendToChatbot = function(message) {
window.$channelName.postMessage(JSON.stringify(message));
};
if (!window.closeButtonObserver) {
window.closeButtonObserver = new MutationObserver(function(mutations) {
var closeButton = document.querySelector('.chatbot-close-button');
if (closeButton && !closeButton.getAttribute('listener-attached')) {
closeButton.setAttribute('listener-attached', 'true');
closeButton.addEventListener('click', function() {
try {
window.$channelName.postMessage(JSON.stringify({ type: 'close_chatbot' }));
} catch (e) { console.error('Error sending close message:', e); }
});
}
});
window.closeButtonObserver.observe(document.body, { childList: true, subtree: true });
}
var existingButton = document.querySelector('.chatbot-close-button');
if (existingButton && !existingButton.getAttribute('listener-attached')) {
existingButton.setAttribute('listener-attached', 'true');
existingButton.addEventListener('click', function() {
try {
window.$channelName.postMessage(JSON.stringify({ type: 'close_chatbot' }));
} catch (e) { console.error('Error sending close message:', e); }
});
}
// APP_READY is sent by the web app via sendToChatbot({ type: 'APP_READY' }) when ready.
true;
''';
}