private async void LoginWebView_LoadCompleted(object sender, NavigationEventArgs e) { if (e.Uri.AbsoluteUri.Contains("passport.cnblogs.com")) { //若是登录页面,则设置用户名和密码 string js = @"var username =document.getElementById('input1'); if(username!= null && username!='undefined') username.value='" + _loginViewModel.UserName + "';" + @"var password =document.getElementById('input2'); if(password!= null && password!='undefined') password.value='" + _loginViewModel.Password + "';"; await LoginWebView.InvokeScriptAsync("eval", new string[] { js }); } LoginCompleted(); }
/// <summary> /// 检查登录状态 /// </summary> public async void CheckLoginStatus() { string login_js = "var o = document.getElementById('tip_btn');if(o) o.innerText;"; loginResult = await LoginWebView.InvokeScriptAsync("eval", new string[] { login_js }); if (loginResult.Contains("成功")) { return; } // 每隔100毫秒检测下状态 await Task.Delay(100); CheckLoginStatus(); }
/// <summary> /// 执行登录操作 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) { try { // 用户名 string js = "document.getElementById('input1').setAttribute('value', '" + UserInputTextBox.Text + "');"; // 密码 js += "document.getElementById('input2').setAttribute('value', '" + PasswordInptutBox.Password + "');"; // 点击登录 js += "document.getElementById('signin').click();"; // 执行脚本文件 await LoginWebView.InvokeScriptAsync("eval", new string[] { js }); CheckLoginStatus(); } catch (Exception ex) { await new MessageDialog(ex.Message).ShowAsync(); } }
private async Task <string> InvokeScriptAsync(string js) { return(await LoginWebView.InvokeScriptAsync("eval", new string[] { js })); }