Advertisement
Guest User

Untitled

a guest
Feb 5th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. public class GeckoTestWebLogin
  2. {
  3. private readonly string _user;
  4. private readonly string _pass;
  5. public GeckoWebBrowser Gweb;
  6. public Uri LoginUri { get; } = new Uri("https://website.com/login/");
  7.  
  8. public bool LoginCompleted { get; private set; } = false;
  9. public bool Loaded { get; private set; } = false;
  10.  
  11. public GeckoTestWebLogin(string user, string pass)
  12. {
  13. _user = user;
  14. _pass = pass;
  15. Xpcom.EnableProfileMonitoring = false;
  16. Xpcom.Initialize("Firefox");
  17.  
  18. //this code is for testing purposes, it will be removed upon project completion
  19. CookieManager.RemoveAll();
  20.  
  21. Gweb = new GeckoWebBrowser();
  22. Gweb.DocumentCompleted += DocLoaded;
  23.  
  24. //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?
  25. MutationObserver mutationObserver = new MutationObserver(Gweb.Window.DomWindow, (nsISupports)Gweb.Document.DomObject);
  26. }
  27.  
  28. private void TestObservedEvent(string parms, object[] objs)
  29. {
  30. MessageBox.Show("The page was changed @ " + DateTime.Now);
  31. }
  32.  
  33. public void DocLoaded(object obj, GeckoDocumentCompletedEventArgs e)
  34. {
  35. Loaded = true;
  36. if (Gweb.Url != LoginUri) return;
  37. AttemptLogin();
  38. }
  39.  
  40. private void AttemptLogin()
  41. {
  42. GeckoElementCollection elements = Gweb.Document.GetElementsByTagName("input");
  43. foreach (GeckoHtmlElement element in elements)
  44. {
  45. switch (element.Id)
  46. {
  47. case "username":
  48. element.SetAttribute("value", _user);
  49. break;
  50. case "password":
  51. element.SetAttribute("value", _pass);
  52. break;
  53. case "importantchangedinfo":
  54. GeckoHtmlElement authcodeModal =
  55. (GeckoHtmlElement)
  56. Gweb.Document.GetElementsByClassName("login_modal").First();
  57. if (authcodeModal.Attributes["style"].NodeValue != "display: none")
  58. {
  59. InputForm form = new InputForm { InputDescription = "Captcha Required!" };
  60. form.ShowDialog();
  61. elements.FirstOrDefault(x => x.Id == "captchabox")?.SetAttribute("value", form.Input);
  62. }
  63. break;
  64. }
  65. }
  66. elements.FirstOrDefault(x => x.Id == "Login")?.Click();
  67. }
  68.  
  69. public void Login()
  70. {
  71. //this will cause the DocLoaded event to fire after completion
  72. Gweb.Navigate(LoginUri.ToString());
  73. }
  74. }
  75.  
  76. MutationObserver mutationObserver = new MutationObserver(Gweb.Window.DomWindow, (nsISupports)Gweb.Document.DomObject);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement