The issue arises solely in the compiled application, leading me to believe it's a browser-specific problem. However, I'm uncertain:
I've inserted the following script into a webpage:
async function exportChatAsXMLHelper(params){
let displayname = params[0];
let includeMedia = params[1];
let debug = params[2];
await exportChatAsXML(displayname, includeMedia, debug);
}
async function exportChatAsXML(displayname, includeMedia, debug)
{
let chat = (await WPP.chat.list()).find(m => m.contact && m.contact.name && m.contact.name.includes(displayname));
await WPP.chat.openChatBottom(chat.id);
let msgs = await WPP.chat.getMessages(chat.id, {count : -1});
const log = (obj) => debug && console.log(obj);
log('Total messages: ' + msgs.length);
let count=msgs.length;
for (var i = 0; i < count; i++) {
log('Message number: ' + i);
let message = msgs[i];
let xml='';
xml+= '<message>';
xml+= '<sender>'+ message.from.user +'</sender>';
xml+= '<receiver>'+ message.to.user +'</receiver>';
xml+= '<type>'+ (message.type || '') +'</type>';
if(message.type == 'chat')
{
xml+= '<body>'+ message.body +'</body>';
}
if(message.type != 'chat' && includeMedia)
{
xml+= '<media>';
xml+= '<caption>'+ (message.caption || '') +'</caption>';
xml+= '<filename>'+ (message.filename || '') +'</filename>';
log('Downloading media');
try
{
let mediabody = await mediatoBase64(message.id);
xml+= '<MediaDownloadStatus>success</MediaDownloadStatus>';
xml+= '<base64>'+ mediabody +'</base64>';
}
catch(e)
{
xml+= '<base64></base64>';
xml+= '<MediaDownloadStatus>fail</MediaDownloadStatus>';
}
xml+= '</media>';
}
xml+= '</message>';
alert('before'); //this is still shown
//where is JSCallbackReceiver defined? it is from vb6
window.JSCallbackReceiver.OnWhatsAppXMLReceived(i, count, xml);
alert('after'); //this is not shown
xml='';
}
}
//-----
async function mediatoBase64(msgid) {
let blob = await WPP.chat.downloadMedia(msgid);
return new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
It performs well on my developer's device but throws an error on a client's machine:
Uncaught (in promise) TypeError: Cannot read property 'OnWhatsAppXMLReceived' of undefined https://web.whatsapp.com/ 80
To pinpoint the exact cause, I've included some alerts.
window.JSCallbackReceiver.OnWhatsAppXMLReceived(i, count, xml);
alert('after');
What could be done to diagnose the disparities between environments?
The callback object is initially set up in VB6 like this:
Private m_JSCallback As clsJSCallbackReceiver
Set m_JSCallback = New clsJSCallbackReceiver
Me.webkit1.AddObject "JSCallbackReceiver", m_JSCallback
Does the error imply that the browser cannot locate the object added via AddObject
?
I am utilizing mobileFx webkit browser, which derives from Chromium, although I reckon that's inconsequential.
The appearance of the class clsJSCallbackReceiver
in VB6 is as follows:
Option Explicit
Public Sub OnWhatsAppXMLReceived(ByVal uIndex As Long, ByVal uCount As Long, ByVal uXml As String)
Debug.Print("I was called!")
End Sub
Public Sub SaySomething(ByVal u As String)
MsgBox(u)
End Sub
Thank you!
Edit:
The issue arises only when compiled. It runs smoothly within the VB6 IDE.
Even after encountering the error, executing
m_JSCallback.SaySomething("Hello!")
yields the intended outcome.
Hence, the object persists but seems disconnected from the browser.