5

I'm just getting into using Delphi with Spidermonkey. Previously I would load a web page into a TWebBrowser component and interact with the Javascript code in the loaded web page. This was messy because to return values back to delphi I had to load them into a DOM object via the Javascript code and then inspect the DOM from Delphi to find that object and access it's value property.

With Spidermonkey, can I execute a specific Javascript function and get the return value easily and directly back into Delphi? If so, please point me to a quick code example that would be helpful. The 3 samples that came with Spidermonkey don't seem to get into this.

Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
  • 1
    Don't know how in Delphi (I found only quite outdated [`bridge project`](http://delphi.mozdev.org/javascript_bridge/) and haven't tried it personally) but you're looking for the [`JS_CallFunctionName`](https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_Reference/JS_CallFunctionName) function, that can be used [`this way`](https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_User_Guide#Calling_functions). – TLama Sep 22 '12 at 07:21
  • 1
    @TLama - Thanks again for finding the javascript bridge project. I still can't figure out how to get the return value directly since there is a layer of bridge class objects between code that uses the engine object and calling JS_CallFunctionName(). JS_CallFunctionName() is called internally by the main engine object for other purposes but doing that in a straightforward manner to simply execute one Javascript function and get the return value from it is not clear to me yet. For now I am creating a hidden TLabel component and using it to transfer the value from Javascript back to Delphi. – Robert Oschler Sep 23 '12 at 17:42

2 Answers2

1

> With Spidermonkey, can I execute a specific Javascript function and get the return value easily and directly back into Delphi?

Yes, it possible. Sample compatible with Delphi XE2/XE4.

var
    recFunction,
    recReturnValue,
    recJSVar        : jsval;

........

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=- Find entry point to function.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

if JS_LookupProperty (TSMJSEngine.Context, TSMJSEngine.Global.JSObject, PAnsiChar (AnsiString (strFunctionToRun)), @recFunction) <> js_true then
begin
    //-=- Everything very bad :)
end;

if recFunction.asPtr = nil then
begin
    //-=- Everything very bad :)
end;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=- Call function 
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

if JS_CallFunctionValue (TSMJSEngine.Context, TSMJSEngine.Global.JSObject, recFunction, 0, nil, @recReturnValue) = Integer (false) then
begin
    //-=- Everything very bad :)
end;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=- Get returning result (type: string).
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

strResult := JSValToString (TSMJSEngine.Context, recReturnValue);
TLama
  • 75,147
  • 17
  • 214
  • 392
Zam
  • 2,880
  • 1
  • 18
  • 33
0

I know nothing about delphi but it sounds like you are going to want to setup some type of api or routes for transferring between a frontend / backend system.

Rummy
  • 105
  • 5