简体   繁体   中英

Can I execute a Javascript function inside Spidermonkey and get the return value?

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.

> 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);

我对delphi一无所知,但听起来你想要设置某种类型的api或路由,以便在前端/后端系统之间进行传输。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM