这来自我从vb.net重写为c#的密码管理器程序,并包括自动登录。问题是,当添加了需要登录的新网页时,“ if”语句将需要更改,并重建卸载并重新安装该程序。 是否可以从外部源(文本文件,数据库等)填充此“ if”语句,或者有其他方法可以处理此问题。
尝试使用foreach从数据库进行填充,并尝试使用文本文件中的streamreader。
private void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement element in elements)
{
if (element.Name == "code" || element.Name ==
"ctl00$BodyMainContent$textboxBarcodeUsername"
|| element.Name == "ctl00$ContentMain$TextBoxUserId"
|| element.Name == "ctl00$cphMain$txtUserID"
|| element.Name ==
"ctl00$ctl00$MainContent$MainContent$Login1$Login1$UserName"
|| element.Name == "ctl00$MC$MemberLogOn$CurrentEmail"
|| element.Name == "ctl00$pagePlaceholder$txt_username"
|| element.Name == "ctl00$PlaceHolderMain$txtUserName"
|| element.Name == "email" || element.Name == "handle"
|| element.Name == "logonId" || element.Name ==
"member_login_number"
|| element.Name == "page:form:un"
|| element.Name == "txtEmailAddress" || element.Name == "user"
|| element.Name == "User ID" || element.Name == "userid"
|| element.Name == "UserID" || element.Name == "userID"
|| element.Name == "username" || element.Name == "Username"
|| element.Name == "UserName" || element.Name ==
"userNameSignInV2_input"
|| element.Name == "")
{
element.SetAttribute("value", txtUserName.Text);
}
if (element.Name == "ctl00$BodyMainContent$textboxPassword"
|| element.Name == "ctl00$ContentMain$TextBoxPassword"
|| element.Name == "ctl00$cphMain$txtPassword"
|| element.Name ==
"ctl00$ctl00$MainContent$MainContent$Login1$Login1$Password"
|| element.Name == "ctl00$MC$MemberLogOn$CurrentPassword"
|| element.Name == "ctl00$pagePlaceholder$txt_password"
|| element.Name == "ctl00$PlaceHolderMain$txtPassword"
|| element.Name == "logonPassword" || element.Name ==
"member_login_password"
|| element.Name == "member_password" || element.Name ==
"page:form:password"
|| element.Name == "pass" || element.Name == "passwd"
|| element.Name == "password"|| element.Name == "Password"
|| element.Name == "PASSWORD" || element.Name == "passwordSignInV2_input"
|| element.Name == "pin" || element.Name == "textuseriduserID"
|| element.Name == "txtPassword")
{
element.SetAttribute("value", txtPassword.Text);
}
}
}
答案 0 :(得分:0)
让我们假设您有两个文本文件,每行一个属性名称(文件名存储在UserNameAttributesFile
和PasswordAttributesFile
中)。然后将这些行读入HashSet<string>
。哈希集可以非常快速地测试元素是否存在。
var userNameAttributes = new HashSet<string>(File.ReadLines(UserNameAttributesFile));
var passwordAttributes = new HashSet<string>(File.ReadLines(PasswordAttributesFile));
HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement element in elements) {
if (userNameAttributes.Contains(element.Name)) {
element.SetAttribute("value", txtUserName.Text);
}
if (passwordAttributes.Contains(element.Name)) {
element.SetAttribute("value", txtPassword.Text);
}
}
如果您希望比较不区分大小写,请使用来初始化哈希集
var userNameAttributes = new HashSet<string>(
File.ReadLines(UserNameAttributesFile),
StringComparer.OrdinalIgnoreCase);
var passwordAttributes = new HashSet<string>(
File.ReadLines(PasswordAttributesFile),
StringComparer.OrdinalIgnoreCase);