Page 1 of 1

How to Get Js Result outside the ProcessHandler_OnCustomMessage

Posted: Thu Jul 13, 2017 4:50 am
by crystalxp
Sorry for boring you again

I use you suggestion to run js and get the js result in ProcessHandler_OnCustomMessage,but when I send the result string to memo in form ,noting append! What's wrong?

the demo code here:

procedure ProcessHandler_OnCustomMessage(const browser: ICefBrowser;
sourceProcess: TCefProcessId; const ProMsg: ICefProcessMessage);
var
JsCallStr, JsResultStr: string;
FrameID: NativeInt;
TempFrame: ICefFrame;
val: ICefV8Value;
context: ICefv8Context;
excp: ICefV8Exception;
begin
JsCallStr := ProMsg.ArgumentList.GetString(0);
FrameID := ProMsg.ArgumentList.GetInt(1);
if (browser <> nil) then
begin
if FrameID = -1 then
TempFrame := browser.MainFrame
else
TempFrame := browser.GetFrameByident(FrameID);
if (TempFrame <> nil) then
begin
context := TempFrame.GetV8Context;
context.Eval(JsCallStr, TempFrame.Url, 0, val, excp);
JsResultStr := val.GetStringValue;
// Application.MessageBox(PChar(JsResultStr), '', MB_OK);---> this could work perfectly!
// Workers.Signal('#JsResultReturn', TQJobExtData.Create(JsResultStr), -->this not work at all ,another method use globalvar outside the funtion are also not work too!
// jdfFreeAsObject);
end;
end;
end;

Also Another question is , If the ICefFrame is not mainfrain,The JsCode will let the page go blank! it's so strange to me....

btw:the jsCallStr is " $(document.body).html() "

Re: How to Get Js Result outside the ProcessHandler_OnCustomMessage

Posted: Fri Jul 14, 2017 8:47 am
by salvadordf
The page goes blank because the process crashes.

I've modified the FastDomVisitor code to send a message back to the browser process with customized parameters.
I'll upload it as soon as I fix the drag and drop problem in OSR mode.

Re: How to Get Js Result outside the ProcessHandler_OnCustomMessage

Posted: Fri Jul 14, 2017 9:05 am
by salvadordf
I forgot to add that the FastDomVisitor modification would enable you to get any result back to your main form when you visit the DOM.

In order to get JS results you would only have to modify the uTestExtension.pas file. Add a new class procedure that simply sends a text variable to the browser process. Something like this :

Code: Select all

class procedure TTestExtension.sendresulttobrowser(const msgtext, msgname : string);
var
  msg: ICefProcessMessage;
begin
  msg := TCefProcessMessageRef.New(msgname); // customized message name
  msg.ArgumentList.SetString(0, msgtext);

  TCefv8ContextRef.Current.Browser.SendProcessMessage(PID_BROWSER, msg);
end;
In your JS code you would call "myextension.sendresulttobrowser" the same way you call the "myextension.mouseover" function declared in uTestExtension.

You will also need to modify the Chromium1ProcessMessageReceived procedure to recognize the msgname parameter the same way it recognizes the "mouseover" message name.

I'll upload all the modifications as soon as I can.

Re: How to Get Js Result outside the ProcessHandler_OnCustomMessage

Posted: Sat Jul 15, 2017 8:52 am
by crystalxp
Cool! :D

Re: How to Get Js Result outside the ProcessHandler_OnCustomMessage

Posted: Tue Jul 18, 2017 4:51 pm
by salvadordf
Download the latest version of CEF4Delphi, build the MiniBrowser, right-click on a web page and select "Visit DOM in Javascript".
You'll see a new window with the HTML code inside the BODY element.

If you click on the "Visit DOM in CEF" you'll see the document title in the status bar.

Re: How to Get Js Result outside the ProcessHandler_OnCustomMessage

Posted: Fri Jul 21, 2017 7:41 am
by crystalxp
Great Big News,Very Very Thank you.!