pub const HOST_API_BRIDGE_SCRIPT: &str = r#"
(function() {
'use strict';
if (window.__hostApiBridge) { return; }
window.__hostApiBridge = true;
window.__HOST_WEBVIEW_MARK__ = true;
var ch = new MessageChannel();
window.__HOST_API_PORT__ = ch.port2;
ch.port2.start();
var port1 = ch.port1;
port1.start();
port1.onmessage = function(ev) {
var data = ev.data;
if (!data) { console.warn('[host-bridge] data is falsy, dropping'); return; }
var bytes;
if (data instanceof Uint8Array) { bytes = data; }
else if (data instanceof ArrayBuffer) { bytes = new Uint8Array(data); }
else if (ArrayBuffer.isView(data)) { bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); }
else { console.warn('[host-bridge] unknown data type: ' + typeof data + ' constructor=' + (data.constructor ? data.constructor.name : '?') + ', dropping'); return; }
var binary = '';
for (var i = 0; i < bytes.length; i++) { binary += String.fromCharCode(bytes[i]); }
try {
window.webkit.messageHandlers.hostApi.postMessage(btoa(binary));
} catch(e) {
console.error('[host-bridge] postMessage to hostApi FAILED:', e.message);
}
};
window.__hostApiReply = function(b64) {
try {
var binary = atob(b64);
var bytes = new Uint8Array(binary.length);
for (var i = 0; i < binary.length; i++) { bytes[i] = binary.charCodeAt(i); }
port1.postMessage(bytes);
} catch(e) { console.error('[host-bridge] reply failed:', e.message); }
};
})();
"#;Expand description
JavaScript injected before the Polkadot app loads. Sets up:
window.__HOST_WEBVIEW_MARK__ = true— SDK webview detectionMessageChannelwith port2 aswindow.__HOST_API_PORT__- Binary message forwarding between port1 and native (base64)