How to sendmessage including return character to browser?
When I do this I have no idea at all, Please help me. Thank you very much!
--------
tempstr:='MyJavaExtension.sendresulttobrowser("1\n"+"2\r"+"3#10#13", "'+GETRECORD_MESSAGE_NAME+'");' ;
Chromium1.Browser.MainFrame.ExecuteJavaScript(tempstr, Chromium1.browser.MainFrame.GetURL (), 0 );
-------
tempstr:=message.ArgumentList.GetString(0);// the value is '1'#$A'2'#$D'3#10#13' in debug, but Delphi won't deal with '#$A' as return: '123#10#13'
Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
How to sendmessage including return character to browser
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to sendmessage including return character to browser
Try encoding (or escaping) that text before you use it in ExecuteJavaScript.
Re: How to sendmessage including return character to browser
Thank you very much!
If their were demos?
If their were demos?
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to sendmessage including return character to browser
I'm afraid there are no CEF4Delphi demos showing how to escape strings in JavaScript but if you search "javascript escape carriage return" in google you will find code examples like this one :
https://stackoverflow.com/questions/259 ... es-with-js
https://stackoverflow.com/questions/259 ... es-with-js
Re: How to sendmessage including return character to browser
Sorry to disturb you again.
The problem is:
'Java return character' cannot be recognized in message passing from Java to browser in delphi.
(java) : "1\n1"+"2\r2"+"3#10#133" -- message to browser -- (delphi): '11223#10#133' //'there are no return characters'
I wonder if this is a bug?
The problem is:
'Java return character' cannot be recognized in message passing from Java to browser in delphi.
(java) : "1\n1"+"2\r2"+"3#10#133" -- message to browser -- (delphi): '11223#10#133' //'there are no return characters'
I wonder if this is a bug?
- salvadordf
- Posts: 4564
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: How to sendmessage including return character to browser
I tested your string in the JSExtension demo.
I added this constant :
The new Chromium1BeforeContextMenu procedure is this :
An the new Chromium1ContextMenuCommand procedure is this :
When I run this demo and selected the "Test" context menu option the results in the simple text viewer were these :
Set a breakpoint in the Chromium1ProcessMessageReceived procedure after the FText assignment and you will see that the value of FText is this :
That value has all the line breaks and the TMemo in the simple text viewer handles them correctly.
Some other Delphi components may ignore line breaks.
I added this constant :
Code: Select all
MINIBROWSER_CONTEXTMENU_TEST = 2;
Code: Select all
procedure TJSExtensionFrm.Chromium1BeforeContextMenu(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; const model: ICefMenuModel);
begin
// Adding some custom context menu entries
model.AddSeparator;
model.AddItem(MINIBROWSER_CONTEXTMENU_SETJSEVENT, 'Set mouseover event');
model.AddItem(MINIBROWSER_CONTEXTMENU_JSVISITDOM, 'Visit DOM in JavaScript');
model.AddItem(MINIBROWSER_CONTEXTMENU_TEST, 'Test');
end;
Code: Select all
procedure TJSExtensionFrm.Chromium1ContextMenuCommand(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; commandId: Integer;
eventFlags: Cardinal; out Result: Boolean);
begin
Result := False;
// Here is the code executed for each custom context menu entry
case commandId of
MINIBROWSER_CONTEXTMENU_SETJSEVENT :
if (browser <> nil) and (browser.MainFrame <> nil) then
browser.MainFrame.ExecuteJavaScript(
'document.body.addEventListener("mouseover", function(evt){'+
'function getpath(n){'+
'var ret = "<" + n.nodeName + ">";'+
'if (n.parentNode){return getpath(n.parentNode) + ret} else '+
'return ret'+
'};'+
'myextension.mouseover(getpath(evt.target))}'+ // This is the call from JavaScript to the extension with DELPHI code in uTestExtensionHandler.pas
')', 'about:blank', 0);
MINIBROWSER_CONTEXTMENU_JSVISITDOM :
if (browser <> nil) and (browser.MainFrame <> nil) then
browser.MainFrame.ExecuteJavaScript(
'var testhtml = document.body.innerHTML;' +
'myextension.sendresulttobrowser(testhtml, ' + quotedstr(CUSTOMNAME_MESSAGE_NAME) + ');', // This is the call from JavaScript to the extension with DELPHI code in uTestExtensionHandler.pas
'about:blank', 0);
MINIBROWSER_CONTEXTMENU_TEST :
if (browser <> nil) and (browser.MainFrame <> nil) then
browser.MainFrame.ExecuteJavaScript(
'myextension.sendresulttobrowser("1\n"+"2\r"+"3#10#13", ' + quotedstr(CUSTOMNAME_MESSAGE_NAME) + ');',
'about:blank', 0);
end;
end;
Code: Select all
1
2
3#10#13
Code: Select all
'1'#$A'2'#$D'3#10#13'
Some other Delphi components may ignore line breaks.
Re: How to sendmessage including return character to browser
Thank you so much for your help!