[英]Can I execute a Javascript function inside Spidermonkey and get the return value?
I'm just getting into using Delphi with Spidermonkey. 我刚刚开始使用Delphi和Spidermonkey。 Previously I would load a web page into a TWebBrowser component and interact with the Javascript code in the loaded web page.
以前我会将一个网页加载到TWebBrowser组件中,并与加载的网页中的Javascript代码进行交互。 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.
这很麻烦因为要将值返回给delphi我必须通过Javascript代码将它们加载到DOM对象中,然后从Delphi检查DOM以找到该对象并访问它的value属性。
With Spidermonkey, can I execute a specific Javascript function and get the return value easily and directly back into Delphi? 使用Spidermonkey,我可以执行特定的Javascript函数并轻松获得返回值并直接返回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.
Spidermonkey附带的3个样本似乎没有进入这个。
> With Spidermonkey, can I execute a specific Javascript function and get the return value easily and directly back into Delphi? >使用Spidermonkey,我可以执行特定的Javascript函数并轻松获得返回值并直接返回Delphi吗?
Yes, it possible. 是的,有可能。 Sample compatible with Delphi XE2/XE4.
样品与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或路由,以便在前端/后端系统之间进行传输。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.