Antwise community forums
How to set cookie information for a page? - Printable Version

+- Antwise community forums (https://forums.antwise.com)
+-- Forum: Antview (https://forums.antwise.com/forumdisplay.php?fid=11)
+--- Forum: Antview for Windows (https://forums.antwise.com/forumdisplay.php?fid=12)
+--- Thread: How to set cookie information for a page? (/showthread.php?tid=134)



How to set cookie information for a page? - Leeson - 2022-09-21

Hello, I have a few questions here hoping to get a response, thanks!
1. I read the documentation and can't find a way to set cookies, how can I set cookies?
2. How do I get DOM events, such as a button click event?
3. Is there a better way to call JavaScript functions synchronously?
4. OnWebMessageReceived was unable to receive messages in PowerBuilder 9.0, OS: WINDOWS 11。


RE: How to set cookie information for a page? - wila - 2022-09-21

(2022-09-21, 03:40:41)Leeson Wrote: Hello, I have a few questions here hoping to get a response, thanks!
1. I read the documentation and can't find a way to set cookies, how can I set cookies?
2. How do I get DOM events, such as a button click event?
3. Is there a better way to call JavaScript functions synchronously?
4. OnWebMessageReceived was unable to receive messages in PowerBuilder 9.0, OS: WINDOWS 11。

Hi,

Thank you for your questions.

1. Currently the only way to set cookies is via Javascript. The cookiemanager isn't yet exposed, but I will look into that.

2. For catching DOM events you will have to inject javascript into the current page, for example using addEventListener.
Then in that event use postmessage to notify that back to the host application. A technique that leads us back to your question 4)

3. Not sure what you tried. There is RunAnonymousFunctionSync if you want to run javascript synchronously, but it has some limitations, like not being able to run that from AntView events.
The problem here is that all of the WebView2 control is designed by Microsoft to be used asynchronously.
Adding a synchronous layer on top of that has its problems. This is something that will be expanded upon to make it easier to use and work around some of the limitations.

4. I don't have PowerBuilder 9 here to test with. Only tested against the most recent version of PowerBuilder at the time (PB 2021 from the looks of it)
Have you tried the WebMessage example ? Is OnWebMessageReceived not working in that example either?

Can you even run the examples in PowerBuilder 9?

--
Wil


RE: How to set cookie information for a page? - Leeson - 2022-09-22

Thanks for your patient answers, questions 1, 2, 3 are now clear.
The fourth question, in PowerBuilder 9.0 can not accept the postMessage sent by the OnWebMessageReceived event, I am exactly in accordance with the official website of the DEMO to test, since you can not send screenshots, please allow me to send the code:

HTML PAGE CODE:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="StyleSheet" href="WebMessage.css" type="text/css">
<script>
function sendString() {
const textData = document.getElementById("data");
if (textData && window.chrome.webview) {
window.chrome.webview.postMessage(textData.value);
}
}
function sendJSON() {
const textData = document.getElementById("data");
if (textData && window.chrome.webview) {
let msg = {
name: "Bob",
age: 38,
comment: textData.value
};
window.chrome.webview.postMessage(msg);
}
}

window.chrome.webview.addEventListener('message', (event) => {
const textData = document.getElementById("data");
if (textData) {
if (typeof event.data === 'object' || typeof event.data === 'null') {
textData.value = JSON.stringify(event.data);
} else {
textData.value = event.data;
}
}
});
</script>
</head>
<body>
<section class="container">
<p>Send data between HOST application and control via messages</p>
<textarea id="data">Text that you want to sent to the application hosting the browser control.</textarea>
<div>
<span>
<button id="postString" onclick="sendString();">Post String</button>
</span>
<span>
<button id="postJSON" onclick="sendJSON();">Post JSON</button>
</span>
<div>
</section>
</body>
</html>

PB CODE:

event oncreatewebviewcompleted(long hresult);this.object.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0"
this.object.EventsUseHexadecimal = true
this.object.WebMessageEnabled = true
end event

event onwebmessagereceived(string uri, string messageasjson, string messageasstring);
messagebox("postmessage",messageasjson)
mle_1.text = messageasstring
end event


RE: How to set cookie information for a page? - wila - 2022-09-22

The forum software should accept screenshots, upload the file via the "Attachments" option below the reply text and then use "insert into Post".

I'm not seeing anything wrong with your code.

Other events do trigger?

Like if you put a breakpoint in the oncreatewebviewcompleted event then it stops there?

Just trying to make sure I understand correctly.

If you put a breakpoint in the onwebmessagereceived event then it does not trigger?

Note that webview2 does not like a messagebox in an event. (see https://doc.antview.dev/hs201.htm )
I'm pretty sure that it is not causing your issue though as it should still trigger the event.

--
Wil


RE: How to set cookie information for a page? - Leeson - 2022-09-23

Other events do trigger?

Yes, I tried the onExecuteScript event to accept the return value of the JS function normally.

Like if you put a breakpoint in the oncreatewebviewcompleted event then it stops there?

Yes, pauses at the breakpoint

If you put a breakpoint in the onwebmessagereceived event then it does not trigger?

Yes, there is no pause at the breakpoint, and I've removed the MessageBox.

PowerBuilder 9.0 is a 32-bit development tool (more than 20 years old), will OnWebMessageReceived not receive a message have something to do with this?


RE: How to set cookie information for a page? - wila - 2022-09-23

Hi Leeson,

Yeah I looked up PowerBuilder 9.0 and also was curious when it was released. Around 2003 IIRC. That should be fine though, that by itself is not the limiting factor. My only problem with it so far was that I could not find a demo version or anything to try with and see if I can reproduce your issue.

OK.. so at least you have other events triggering, this at least indicates that PB9.0 does not have an issue with raising our ActiveX events.


Let me send you a private version of AntView by email that has an extra debug level.
Then we can see if the OnWebMessageReceived event is triggering at the AntView side of things.

--
Wil


RE: How to set cookie information for a page? - Leeson - 2022-09-23

Thank you very much, please send it to my email: 3311204@QQ.COM


RE: How to set cookie information for a page? - wila - 2022-09-23

Hi,

I had already send the link to that email address.
Hope you received it, otherwise please check if it didn't end up as spam.

--
Wil