Page 1 of 1

How to sendmessage including return character to browser

Posted: Mon Oct 08, 2018 11:32 am
by coater
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'

Re: How to sendmessage including return character to browser

Posted: Mon Oct 08, 2018 12:20 pm
by salvadordf
Try encoding (or escaping) that text before you use it in ExecuteJavaScript.

Re: How to sendmessage including return character to browser

Posted: Mon Oct 08, 2018 2:38 pm
by coater
Thank you very much!
If their were demos?

Re: How to sendmessage including return character to browser

Posted: Mon Oct 08, 2018 6:46 pm
by salvadordf
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

Re: How to sendmessage including return character to browser

Posted: Tue Oct 09, 2018 3:05 am
by coater
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?

Re: How to sendmessage including return character to browser

Posted: Tue Oct 09, 2018 6:36 am
by salvadordf
I tested your string in the JSExtension demo.

I added this constant :

Code: Select all

MINIBROWSER_CONTEXTMENU_TEST = 2;
The new Chromium1BeforeContextMenu procedure is this :

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;
An the new Chromium1ContextMenuCommand procedure is this :

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;
When I run this demo and selected the "Test" context menu option the results in the simple text viewer were these :

Code: Select all

1
2
3#10#13
Set a breakpoint in the Chromium1ProcessMessageReceived procedure after the FText assignment and you will see that the value of FText is this :

Code: Select all

'1'#$A'2'#$D'3#10#13'
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.

Re: How to sendmessage including return character to browser

Posted: Wed Oct 10, 2018 11:23 am
by coater
Thank you so much for your help!