刚开始我是这样写的:
return { array_video, array_audio, array_iframe, array_img };
undefined inspect, 逗号的地方报错。
修改函数名也不行,反复思考,逗号报错终止解析,函数不完整,所以 undefined。
https://www.jb51.net/article/259022.htm
反复测试,返回 JSON 成功了!总结就是解析器受限,return 只能返回一个值,不能有逗号。
String js = "javascript: function inspect(){var videos=document.getElementsByTagName('video');var array_video=new Array();for(var i=0;i<videos.length;i++){array_video.push(videos[i].src);}var audios=document.getElementsByTagName('audio');var array_audio=new Array();for(var i=0;i<audios.length;i++){array_audio.push(audios[i].src);}var array_iframe=new Array();var iframes=document.getElementsByTagName('iframe');for(var i=0;i<iframes.length;i++){array_iframe.push(iframes[i].src);}var array_img=new Array();var imgs=document.getElementsByTagName('img');for(var i=0;i<imgs.length;i++){array_img.push(imgs[i].src);}var json={videos:array_video,audios:array_audio,iframes:array_iframe,imgs:array_img};return json;}";
list_webView.get(currentPage).loadUrl(js);
list_webView.get(currentPage).evaluateJavascript("javascript:inspect()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
//Log.e(Thread.currentThread().getStackTrace()[2] + "", value);
newWindow("");
String html = "<html>\n<head>\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>\n<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'/>\n<title>资源探查</title>\n<style>\nsummary{text-align:center;font-size:2em;font-weight:bold;}\na{text-decoration:none;}\nvideo,img{max-width:100%;}\n</style>\n</head>\n<body>\n<details id='details_video'>\n<summary id='summary_video'>视频</summary>\n</details>\n<details id='details_audio'>\n<summary id='summary_audio'>音频</summary>\n</details>\n<details id='details_iframe'>\n<summary id='summary_iframe'>框架</summary>\n</details>\n<details id='details_img'>\n<summary id='summary_img'>图片</summary>\n</details>\n<script>\nvar details_video=document.getElementById('details_video');\nvar details_audio=document.getElementById('details_audio');\nvar details_iframe=document.getElementById('details_iframe');\nvar details_img=document.getElementById('details_img');\nvar summary_video=document.getElementById('summary_video');\nvar summary_audio=document.getElementById('summary_audio');\nvar summary_iframe=document.getElementById('summary_iframe');\nvar summary_img=document.getElementById('summary_img');\nvar json=eval((" + value.replace("\"", "'") + "));\nvar videos=json.videos;\nsummary_video.innerText='视频'+videos.length;\nfor(var i=0; i<videos.length; i++){\nvar video=document.createElement('video');\nvideo.src=videos[i];\nvideo.controls='controls';\ndetails_video.append(video);\nvar a=document.createElement('a');\na.href=videos[i];\na.textContent=videos[i];\na.target='_blank';\ndetails_video.appendChild(a);\n}\nvar audios=json.audios;\nsummary_audio.innerText='音频'+audios.length;\nfor(var i=0; i<audios.length; i++){\nvar audio=document.createElement('audio');\naudio.src=audios[i];\ndetails_audio.appendChild(audio);\n}\nvar iframes=json.iframes;\nsummary_iframe.innerText='框架'+iframes.length;\nfor(var i=0; i<iframes.length; i++){\nvar p = document.createElement('p');\nvar a=document.createElement('a');\na.textContent=iframes[i];\na.href=iframes[i];\na.target='_blank';\np.appendChild(a);\ndetails_iframe.appendChild(p);\n}\nvar imgs=json.imgs;\nsummary_img.innerText='图片'+imgs.length;\nfor(var i=0; i<imgs.length; i++){\nvar img=document.createElement('img');\nimg.src=imgs[i];\ndetails_img.appendChild(img);\n}\n</script>\n</body>\n</html>";
//writeFile("inspect.htm", html);
list_webView.get(currentPage).loadData(html, "text/html; charset=UTF-8", null);
}
});