login.mjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { chromium } from "playwright";
  2. import uniqueRandomArray from "unique-random-array";
  3. import userAgents from "top-user-agents";
  4. export async function login(email, password, proxy) {
  5. const browser = await chromium.launch({
  6. headless: true,
  7. devtools: false,
  8. });
  9. const options = {
  10. userAgent: uniqueRandomArray(userAgents)(),
  11. viewport: {
  12. width: parseInt(Math.random() * 100 + 1250),
  13. height: parseInt(Math.random() * 100 + 750),
  14. },
  15. locale: "en-CA",
  16. timezoneId: "America/Toronto",
  17. serviceWorkers: "block",
  18. };
  19. if (proxy) {
  20. options.proxy = proxy;
  21. }
  22. const context = await browser.newContext(options);
  23. const page = await context.newPage();
  24. let points = null;
  25. await page.route(/points$/, async (route, request) => {
  26. const response = await route.fetch();
  27. points = await response.text();
  28. await route.continue();
  29. });
  30. await page.route(
  31. /(\.png)|(\.jpg)|(\.svg)|(\.otf)|(\.woff2)|(\.ttf)|(\.mp3)/gi,
  32. (route) => route.abort()
  33. );
  34. await page.goto("https://www.pcoptimum.ca/");
  35. const link = await page.$('a > span:has-text("sign in")');
  36. link.click();
  37. await page.waitForURL("https://accounts.pcid.ca/login");
  38. await page.fill('input[id="email"]', email);
  39. await page.fill('input[id="password"]', password);
  40. try {
  41. await new Promise(async (resolve, reject) => {
  42. await page.route(/\/login$/, async (route, request) => {
  43. console.log(
  44. `${request.method()} ${request.resourceType()} ${request.url()}`
  45. );
  46. if (request.method() === "POST") {
  47. const response = await route.fetch();
  48. const status = response.status();
  49. console.log(status);
  50. if (status === 200) {
  51. await route.continue();
  52. resolve();
  53. } else if (status === 401) {
  54. reject(new Error("Invalid email or password"));
  55. } else if (status === 429) {
  56. reject(
  57. new Error(
  58. "Oops, something went wrong. Please try again later."
  59. )
  60. );
  61. } else {
  62. reject(
  63. new Error("Unknown error. Please try again later.")
  64. );
  65. }
  66. } else {
  67. await route.continue();
  68. }
  69. });
  70. await page.click('button:has-text("Sign in")');
  71. });
  72. } catch (error) {
  73. await browser.close();
  74. throw error;
  75. }
  76. try {
  77. await page.waitForURL("https://www.pcoptimum.ca/dashboard");
  78. for (let i = 0; i < 30; i++) {
  79. await page.waitForTimeout(1000);
  80. if (points) break;
  81. }
  82. } catch (error) {
  83. console.error(error.stack);
  84. }
  85. let cookies = null;
  86. try {
  87. cookies = JSON.stringify(await context.cookies());
  88. } catch (error) {}
  89. await browser.close();
  90. return { points, cookies, options };
  91. }