美文网首页
关于 Splash 3.2 的 jsfunc() 的一些基本操作

关于 Splash 3.2 的 jsfunc() 的一些基本操作

作者: 背把锄头去挖土 | 来源:发表于2018-11-03 15:17 被阅读0次

Splash 3.2 中的 jsfunc() 方法是直接可以调用JavaScript定义的方法,但是所调用的方法需要用双中括号包围,这相当于实现了JvaScript方法到Lua脚本的转换.示例如下:

function main(splash, args)
  local get_div_count = splash:jsfunc([[
    function () {
    var body = document.body;
    var divs = body.getElementsByTagName('div');
    return divs.length;
  }
  ]])
  splash:go('https://www.baidu.com')
  return ('There are %s DIVs'):format(
  get_div_count())
end

运行结果如下:

 Splash Response: "There are 22 DIVs" 

以上为参考<<Python3 网络开发实战>>,崔庆才 P270.

关于JavaScript到Lua脚本的更多转换细节,如下:

function main(splash, args)
  local vec_len = splash:jsfunc([[
      function(x, y) {
         return Math.sqrt(x*x + y*y)
      }
  ]])
  return {res=vec_len(5, 4)}
end

运行结果如下:

res: 6.4031242374328485

这是用JavaScript函数来传参的一个示例

更多细节可以参考:
https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-jsfunc

相关文章

网友评论

      本文标题:关于 Splash 3.2 的 jsfunc() 的一些基本操作

      本文链接:https://www.haomeiwen.com/subject/humkxqtx.html