1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
local WebChromeClient = luajava.bindClass "android.webkit.WebChromeClient" local WebViewClient = luajava.bindClass "android.webkit.WebViewClient" local DownloadListener = luajava.bindClass "android.webkit.DownloadListener"
local maxPage=1 for i=0,maxPage-1 do local fragment = activity.getUiManager().getFragment(i) local webView = fragment.getWebView() webView.loadUrl("https://httpstat.us/404") local errView,errStatus webView.setWebViewClient(luajava.override(WebViewClient,{ shouldOverrideUrlLoading=function(superCall,view,webResourceRequest) local String = luajava.bindClass "java.lang.String" local Intent = luajava.bindClass "android.content.Intent" local Uri = luajava.bindClass "android.net.Uri" print("即将开始",webResourceRequest.getMethod(),"加载",webResourceRequest.getUrl().toString()) if not webResourceRequest.getUrl().toSafeString():find("^https?://") then print("阻止了"..webResourceRequest.getUrl().toSafeString().."的加载\n并尝试启动外部应用") local intent = Intent(Intent.ACTION_VIEW, Uri.parse(webResourceRequest.getUrl().toString())) pm = activity.getPackageManager() local componentName = intent.resolveActivity(pm) if componentName == nil then print("没有用于打开的应用哦") else local PackageManager = luajava.bindClass "android.content.pm.PackageManager" local pkgname = componentName.getPackageName() local appname = pm.getApplicationLabel(pm.getApplicationInfo(pkgname,PackageManager.GET_META_DATA)) print("有用于打开的应用哦:\n"..appname.."\n"..pkgname) end return true end return false end, shouldInterceptRequest=function(superCall,view,webResourceRequest) if webResourceRequest.getUrl().getPath():find("png") then local WebResourceResponse = luajava.bindClass "android.webkit.WebResourceResponse" local ByteArrayInputStream = luajava.bindClass "java.io.ByteArrayInputStream" local String = luajava.bindClass "java.lang.String" local FileInputStream = luajava.bindClass "java.io.FileInputStream" local imgDir = activity.getLoader().getImagesDir("add") return WebResourceResponse("image/png", "UTF-8", FileInputStream(imgDir)) end return nil end, onPageStarted=function(superCall,view,url,favicon) print(url.."开始加载") local ValueCallback = luajava.bindClass "android.webkit.ValueCallback" local css = "a" view.evaluateJavascript([[ setInterval(() => { elements = document.querySelectorAll("]]..css..[[") elements.forEach(e => e.style.display = "none") // if (elements.length > 0) alert("移除了" + elements.length + "个元素") },100 ) ]],nil) function copyText(text) local Context = luajava.bindClass "android.content.Context" activity.getSystemService(Context.CLIPBOARD_SERVICE).setText(tostring(text)) end view.evaluateJavascript([[ const oldWrite = navigator.clipboard.writeText navigator.clipboard.writeText = (text)=>{androlua.callLuaFunction("copyText",text)oldWrite(text)} ]],nil) end, onPageFinished=function(superCall,view,url) local ValueCallback = luajava.bindClass "android.webkit.ValueCallback" local Uri = luajava.bindClass "android.net.Uri" view.evaluateJavascript("((url)=>{alert(url);return url;})('"..url.."加载结束')", ValueCallback{ onReceiveValue=function(res) print("evaluateJavascript回调:"..res) end }) local CookieManager = luajava.bindClass "android.webkit.CookieManager" local cookieManager = CookieManager.getInstance() cookieManager.removeAllCookie() cookieManager.setCookie(url,"testCookie=this is a cookie") local testCookie = cookieManager.getCookie(url) print("获取"..Uri.parse(url).toSafeString().."的cookie:"..testCookie) if errView ~= nil and errStatus == nil then errView.getParent().removeView(errView) end end, onLoadResource=function(superCall,view,url) end, onReceivedSslError=function(superCall, view, sslErrorHandler, sslError) local sslErr = { [4] = "SSL_DATE_INVALID\n证书的日期无效", [1] = "SSL_EXPIRED\n证书已过期", [2] = "SSL_IDMISMATCH\n主机名称不匹配", [5] = "SSL_INVALID\n发生一般性错误", [6] = "SSL_MAX_ERROR\n此常量在API级别14中已弃用。此常数对于使用SslError API不是必需的,并且可以从发行版更改为发行版。", [0] = "SSL_NOTYETVALID\n证书尚未生效", [3] = "SSL_UNTRUSTED\n证书颁发机构不受信任" } print(sslError.getUrl().."遇到了SSL证书错误,错误类型:"..sslError.getPrimaryError().."\n"..sslErr[sslError.getPrimaryError()]) sslErrorHandler.proceed() return true end, onReceivedHttpError=function(superCall, view, webResourceRequest, webResourceResponse) print(webResourceRequest.getUrl().toString().."遇到HTTP错误码:",webResourceResponse.getStatusCode(),"原因:",webResourceResponse.getReasonPhrase(),"响应体:",webResourceResponse.getData()) if webResourceRequest.isForMainFrame() then errStatus = webResourceResponse.getStatusCode() local errPage = luajava.bindClass"net.fusionapp.core.R".layout.web_error_page local inflater = luajava.bindClass "android.view.LayoutInflater".from(activity) errView = inflater.inflate(errPage, nil) view.getParent().addView(errView) errView.setBackgroundColor(luajava.bindClass "android.graphics.Color".parseColor("#ffffff")) errView.setOnClickListener(function() errStatus = nil view.loadUrl("https://www.baidu.com") end) end end, onReceivedError=function(superCall, view, webResourceRequest,webResourceError) print(webResourceRequest.getUrl().toSafeString().."\n加载异常,原因为:\n"..webResourceError.getDescription()) end, }))
local fileRequestCode = 12345 local customView local orientation local uploadFile webView.setWebChromeClient(luajava.override(WebChromeClient,{ onJsTimeout=function(superCall) return true end, onJsAlert=function(superCall,view,url,message,result) result.confirm() print("Alert消息: "..message) return true end, onJsConfirm=function(superCall,view,url,message,result) result.confirm() print("Confirm消息: "..message) return true end, onJsPrompt=function(superCall,view,url,message,value,result) result.confirm("这里是输入的内容") print("Prompt消息: "..message) return true end, onConsoleMessage=function(superCall,message) return true end, onReceivedTitle=function(superCall,view,title) activity.uiManager.toolbar.titleText=title end, onReceivedIcon=function(superCall,view,bitmap) end, onProgressChanged=function(superCall,view,progress) view.parent.getChildAt(2).progress=progress end, onShowFileChooser=function(superCall, view, valueCallback, fileChooserParams) local Intent = luajava.bindClass "android.content.Intent" uploadFile=valueCallback local intent = fileChooserParams.createIntent() intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true) activity.startActivityForResult(intent, fileRequestCode) return true end, onGeolocationPermissionsShowPrompt=function(superCall, origin, callback) print("请求定位:"..origin) callback.invoke(origin,true,false) end, onPermissionRequest=function(superCall,permissionRequest) local Arrays = luajava.bindClass "java.util.Arrays" print("来自网页"..permissionRequest.getOrigin().toSafeString().."的权限请求:",Arrays.asList(permissionRequest.getResources())) permissionRequest.deny() end, onShowCustomView=function(superCall,view,callback) print("视频全屏") print(view.getClass()) orientation = activity.getRequestedOrientation() or 0 local ActivityInfo = luajava.bindClass "android.content.pm.ActivityInfo" activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR) customView = view local FrameLayout = luajava.bindClass "android.widget.FrameLayout" local Gravity = luajava.bindClass "android.view.Gravity" local lp = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) activity.addContentView(view,lp) end, onHideCustomView=function(superCall) print("退出全屏") activity.setRequestedOrientation(orientation) customView.getParent().removeView(customView) customView = nil orientation = nil end }))
onActivityResult=function(requestCode,resultCode,intent) local Activity = luajava.bindClass "android.app.Activity" local Uri = luajava.bindClass "android.net.Uri" if requestCode == fileRequestCode then if resultCode == Activity.RESULT_CANCELED then if uploadFile~=nil then uploadFile.onReceiveValue(nil) return end end local results if resultCode == Activity.RESULT_OK then if uploadFile==nil or type(uploadFile)=="number" then return end if intent ~= nil then local dataString = intent.getDataString() local clipData = intent.getClipData() if clipData ~= nil then results = Uri[clipData.getItemCount()] for i = 0,clipData.getItemCount()-1 do local item = clipData.getItemAt(i) results[i] = item.getUri() end end if dataString ~= nil then results = Uri[1] results[0]=Uri.parse(dataString) end end end if results~=nil then uploadFile.onReceiveValue(results) uploadFile = nil end end end
webView.setDownloadListener(DownloadListener{ onDownloadStart=function(url, userAgent, contentDisposition, mimetype, contentLength) print("发现下载行为,\n文件描述为: "..contentDisposition.."\n文件类型为: "..mimetype.."\n文件大小为"..contentLength.."\n下载链接是: "..url) end })
webView.onLongClick=function() local WebView = luajava.bindClass "android.webkit.WebView" local htr=webView.getHitTestResult() if htr.getType()==WebView.HitTestResult.IMAGE_TYPE then local imageName=htr.getExtra():match("^.+/(.-)$") print(htr.getExtra(),imageName) end end
local View = luajava.bindClass "android.view.View" webView.setOnKeyListener(View.OnKeyListener{ onKey=function(v, keyCode, event) local KeyEvent = luajava.bindClass "android.view.KeyEvent" if event.getAction() == KeyEvent.ACTION_DOWN then if keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack() then webView.goBack() return true end end return false end }) end
|