Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GeckoTestWebLogin
- {
- private readonly string _user;
- private readonly string _pass;
- public GeckoWebBrowser Gweb;
- public Uri LoginUri { get; } = new Uri("https://website.com/login/");
- public bool LoginCompleted { get; private set; } = false;
- public bool Loaded { get; private set; } = false;
- public GeckoTestWebLogin(string user, string pass)
- {
- _user = user;
- _pass = pass;
- Xpcom.EnableProfileMonitoring = false;
- Xpcom.Initialize("Firefox");
- //this code is for testing purposes, it will be removed upon project completion
- CookieManager.RemoveAll();
- Gweb = new GeckoWebBrowser();
- Gweb.DocumentCompleted += DocLoaded;
- //right about here is where I get lost, where can I set a callback method for the observer to report back to? Is this even how it works?
- MutationObserver mutationObserver = new MutationObserver(Gweb.Window.DomWindow, (nsISupports)Gweb.Document.DomObject);
- }
- private void TestObservedEvent(string parms, object[] objs)
- {
- MessageBox.Show("The page was changed @ " + DateTime.Now);
- }
- public void DocLoaded(object obj, GeckoDocumentCompletedEventArgs e)
- {
- Loaded = true;
- if (Gweb.Url != LoginUri) return;
- AttemptLogin();
- }
- private void AttemptLogin()
- {
- GeckoElementCollection elements = Gweb.Document.GetElementsByTagName("input");
- foreach (GeckoHtmlElement element in elements)
- {
- switch (element.Id)
- {
- case "username":
- element.SetAttribute("value", _user);
- break;
- case "password":
- element.SetAttribute("value", _pass);
- break;
- case "importantchangedinfo":
- GeckoHtmlElement authcodeModal =
- (GeckoHtmlElement)
- Gweb.Document.GetElementsByClassName("login_modal").First();
- if (authcodeModal.Attributes["style"].NodeValue != "display: none")
- {
- InputForm form = new InputForm { InputDescription = "Captcha Required!" };
- form.ShowDialog();
- elements.FirstOrDefault(x => x.Id == "captchabox")?.SetAttribute("value", form.Input);
- }
- break;
- }
- }
- elements.FirstOrDefault(x => x.Id == "Login")?.Click();
- }
- public void Login()
- {
- //this will cause the DocLoaded event to fire after completion
- Gweb.Navigate(LoginUri.ToString());
- }
- }
- MutationObserver mutationObserver = new MutationObserver(Gweb.Window.DomWindow, (nsISupports)Gweb.Document.DomObject);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement