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

Post Reply
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

How to sendmessage including return character to browser

Post 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'
User avatar
salvadordf
Posts: 4074
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to sendmessage including return character to browser

Post by salvadordf »

Try encoding (or escaping) that text before you use it in ExecuteJavaScript.
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to sendmessage including return character to browser

Post by coater »

Thank you very much!
If their were demos?
User avatar
salvadordf
Posts: 4074
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to sendmessage including return character to browser

Post 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
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to sendmessage including return character to browser

Post 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?
User avatar
salvadordf
Posts: 4074
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to sendmessage including return character to browser

Post 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.
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: How to sendmessage including return character to browser

Post by coater »

Thank you so much for your help!
Post Reply