SHARE
TWEET

Untitled

a guest Mar 25th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func startHeadless(cookie: String, task: Task, row: IndexPath, sessionManager: SessionManager) {
  2.         guard let currentCell = self.tableView.cellForRow(at: row) as? TaskCell else {
  3.             print("error at row: \(row.row)")
  4.             return
  5.         }
  6.        
  7.         currentCell.statusLabel?.text = "Getting Checkout..."
  8.         self.clearCookiesBrowser()
  9.         let config = WKWebViewConfiguration()
  10.         //let headlessWebView = self.browserWebview
  11.         //self.centerBrowserWebViewConstraint.constant = 0
  12.         //self.browserWebview.isHidden = false
  13.         let headlessWebView = WKWebView(frame: .zero, configuration: config)
  14.         headlessWebView.isHidden = true
  15.         var request = URLRequest(url: URL(string: "https://www.supremenewyork.com/checkout")!)
  16.         request.setValue("Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", forHTTPHeaderField: "user-agent")
  17.         request.setValue("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", forHTTPHeaderField: "accept")
  18.         request.setValue("gzip, deflate, br", forHTTPHeaderField: "accept-encoding")
  19.         request.setValue("en-US,en;q=0.9", forHTTPHeaderField: "accept-language")
  20.         let newCookies = cookie.replacingOccurrences(of: "request_method=POST;", with: "").replacingOccurrences(of: "HttpOnly", with: "").replacingOccurrences(of: "-0000,", with: "-0000;")
  21.         request.setValue(newCookies, forHTTPHeaderField: "cookie")
  22.         headlessWebView.load(request)
  23.         headless.browsers.append(headlessWebView)
  24.         let indexOfBrowser = (headless.browsers.firstIndex(of: headlessWebView))
  25.         self.checkIfCheckoutHeadless(index: indexOfBrowser!, task: task, row: row, sessionManager: sessionManager)
  26.     }
  27.    
  28.     func checkIfCheckoutHeadless(index: Int, task : Task, row : IndexPath, sessionManager: SessionManager) {
  29.         let headlessWebView = headless.browsers[index]
  30.         if(task.isStopped){
  31.             self.clearCookiesBrowser()
  32.             headlessWebView.load(URLRequest(url: URL(string: "about:blank")!))
  33.             guard let currentCell = self.tableView.cellForRow(at: row) as? TaskCell else {
  34.                 print("error at row: \(row.row)")
  35.                 return
  36.             }
  37.             currentCell.statusLabel?.text = "Stopped"
  38.             return
  39.         }
  40.         guard let currentCell = self.tableView.cellForRow(at: row) as? TaskCell else {
  41.             print("error at row: \(row.row)")
  42.             return
  43.         }
  44.         currentCell.statusLabel?.text = "Getting Checkout..."
  45.         headlessWebView.evaluateJavaScript(
  46.             """
  47.             function getCheckout() {
  48.                 if(window.location.href == "https://www.supremenewyork.com/checkout") {
  49.                     return "true";
  50.                 } else {
  51.                     return window.location.href;
  52.                 }
  53.             }
  54.             getCheckout();
  55.             """)
  56.         { (res, error) in
  57.             guard let res = res as? String else {
  58.                 print("no response! retrying??")
  59.                 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
  60.                     self.checkIfCheckoutHeadless(index: index, task: task, row: row, sessionManager: sessionManager)
  61.                 })
  62.                 return
  63.             }
  64.             print(res)
  65.             if res.range(of: "true") != nil {
  66.                 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
  67.                     self.injectCaptchaHeadless(index: index, task: task, row: row, sessionManager: sessionManager)
  68.                 })
  69.             } else {
  70.                 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
  71.                     print("retrying")
  72.                     self.checkIfCheckoutHeadless(index: index, task: task, row: row, sessionManager: sessionManager)
  73.                 })
  74.             }
  75.         }
  76.     }
  77.  
  78.     func injectCaptchaHeadless(index: Int, task : Task, row : IndexPath, sessionManager: SessionManager) {
  79.         let headlessWebView = headless.browsers[index]
  80.         if(task.isStopped){
  81.             self.clearCookiesBrowser()
  82.             headlessWebView.load(URLRequest(url: URL(string: "about:blank")!))
  83.             guard let currentCell = self.tableView.cellForRow(at: row) as? TaskCell else {
  84.                 print("error at row: \(row.row)")
  85.                 return
  86.             }
  87.             currentCell.statusLabel?.text = "Stopped"
  88.             return
  89.         }
  90.         guard let currentCell = self.tableView.cellForRow(at: row) as? TaskCell else {
  91.             print("error at row: \(row.row)")
  92.             return
  93.         }
  94.         currentCell.statusLabel?.text = "Checkout out..."
  95.         headlessWebView.evaluateJavaScript(
  96.             """
  97.             function inject() {
  98.                 document.getElementById('checkout_form').getElementsByClassName('g-recaptcha')[0].remove()
  99.                 var x = document.createElement("input")
  100.                 x.setAttribute("type", "text");
  101.                 x.setAttribute("id","g-recaptcha-response");
  102.                 x.setAttribute("name", "g-recaptcha-response")
  103.                 x.setAttribute("value", "\(task.captchaToken!)")
  104.                 document.getElementById('checkout_form').appendChild(x)
  105.                 return;
  106.             }
  107.             inject();
  108.             """
  109.         ) { (response, error) in
  110.             var currentProfile : Profile!
  111.             for profile in self.profilesArray {
  112.                 if profile.profileName?.lowercased() == task.profileName?.lowercased() {
  113.                     currentProfile = profile
  114.                     break
  115.                 }
  116.             }
  117.             if(currentProfile.country!.uppercased() == "CANADA" || currentProfile.country!.uppercased() == "USA") {
  118.                 self.checkItem(index: index, task: task, row: row, sessionManager: sessionManager)
  119.                 return
  120.             } else {
  121.                 self.checkoutHeadlessEu(index: index, task: task, row: row, sessionManager: sessionManager)
  122.                 return
  123.             }
  124.         }
  125.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top