简体   繁体   English

我可以在Spidermonkey中执行Javascript函数并获取返回值吗?

[英]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.

相关问题 执行此javascript函数并获取返回值 - Execute this javascript function and get a return value 如何在循环内运行异步 function 并获取返回值(Node.js/Javascript) - How can I run an asynchronous function inside a loop and get a return value (Node.js/Javascript) 如何从javascript函数中的.then内部返回值? - How can I return a value from within a .then inside a javascript function? 从javascript函数返回一个值,然后在其中执行代码 - return a value from javascript function and then execute code inside it 如何获取Android调用的JavaScript函数的返回值? - How can I get the return value of a JavaScript function called by Android? 如何在jsp中获取Javascript函数的值? - How can i get value of Javascript function, inside jsp? 我们如何在Android应用程序中执行javascript函数并获取返回值? - How we can execute a javascript function and get a return value in our android application? 如何从我的reportError函数中获取SpiderMonkey(JSAPI)中的完整回溯? - How can I get the full backtrace in SpiderMonkey (JSAPI) from my reportError function? 我如何调用javascript函数并从javascript函数获取返回值 - How I can call javascript function and get the return value from javascript function 如何获取JavaScript以在Bootstrap弹出窗口中执行? - How can I get JavaScript to execute inside a Bootstrap popover?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM