123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- -- fetch 远程请求模版
- local fetch_request_tpl = [[
- var %s=null;
- fetch("%s",{
- method: '%s',
- body: "%s",
- headers: { %s},
- })
- .then(response => {
- if (!response.ok) {
- throw new Error('Network response was not ok');
- }
- if("%s"=="text"){
- return response.text();
- }else if("%s"=="json"){
- return response.json();
- }else{
- return "我也不知道要返回什么格式";
- }
- })
- .then(data=>{
- %s = data
- })
- .catch(error => {
- console.error('Error:', error);
- });
- ""
- ]]
- --fetch异步请求
- --path:用于选择tab页
- --result_type:结果返回类型 0文本 1列表 2字典
- --method:POST /GET
- -- url: 请求URL
- --head:请求头
- --param:参数 POST请求下,有值
- --return_type:text/json
- --js_var:结果回写到js_var
- --retry_time:等待次数
- --sleep_timeout:等待间隔时间
- function fetch(path,result_type,method,url,head,param,return_type,js_var,retry_time,sleep_timeout)
- local head_str = ""
- for k,v in pairs(head) do
- head_str=head_str..string.format('"%s":"%s",',k,v)
- end
- local fetch_run_js = string.format(fetch_request_tpl,js_var,url,method,param,head_str,return_type,return_type,js_var)
- --print(fetch_run_js)
- local ok,ret = browser_executejs(path,1000*10,0,fetch_run_js)
- wait_jsvar_notnull(path,js_var,retry_time,sleep_timeout)
- return browser_executejs(path,1000*10,result_type,js_var)
- end
- --守候等待js变量不为null
- function wait_jsvar_notnull(path,js_var,retry_time,sleep_timeout)
- local flag=false
- local i=1
- local check_js_var_tpl = 'if(%s){"true"}else{"false"}'
- local run_js=string.format(check_js_var_tpl,js_var)
- repeat
- local ok,ret = browser_executejs(path,1000*2,0,run_js)
- if ok=="ok" and ret=="true" then
- flag=true
- else
- print("waitting")
- browser_sleep(sleep_timeout)
- end
- i=i+1
- until flag or i>retry_time
- end
|