<commit_before>//
//  driver.swift
//  MoniesMac
//
//  Created by <NAME> on 05/11/2014.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import WebKit

protocol WebViewDriverProgressDelegate {
    func webViewDriverProgress(progress: Bool)
}

class WebViewDriver : NSObject, WKNavigationDelegate {
    var webview : WKWebView
    var delegate : WebViewDriverProgressDelegate?
    
    init(webView: WKWebView) {
        self.webview = webView
        super.init()
        self.webview.navigationDelegate = self
    }
    
    func oneAtATime(closure: () -> ()) {
        objc_sync_enter(self)
        closure()
        objc_sync_exit(self)
    }
    
    func visit(location: String) {
        print("visiting \(location)")
        self.webview.loadRequest(NSURLRequest(URL: NSURL(string: location)!))
    }
    
    func labelFor(element:String, completion: ((String) -> Void)) {
        let js = "var answer; var mem = document.getElementById('\(element)'); if (mem) { answer = mem.parentElement.children[0].textContent }; mem && answer || 'null'"
        
        run(js) { result in
            if result != "null" {
                print("read label \(result)")
                completion(result)
            }
        }
    }

    func fillIn(field:String, with:String, completion: ((String) -> Void)) {
        var printValue = with
        if (field as NSString).containsString("pwd") { printValue = "[redacted]" }
        print("fill in value \(printValue)")
        
        run("document.getElementById('\(field)').value = \"\(with)\";") { result in
            completion(result)
        }
    }

    func click(field:String, completion: ((String) -> Void)) {
        print("click \(field)")
        run("document.getElementById('\(field)').click();") { (result) in
            completion(result)
            
            // for some reason, clicking sometimes doesn't work without this
            self.run("$('body').text()") { result in }
        }
    }
    <commit_msg>Add hack to make webview driving work even when off-screen<commit_after>//
//  driver.swift
//  MoniesMac
//
//  Created by <NAME> on 05/11/2014.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import WebKit

protocol WebViewDriverProgressDelegate {
    func webViewDriverProgress(progress: Bool)
}

class WebViewDriver : NSObject, WKNavigationDelegate {
    var webview : WKWebView
    var delegate : WebViewDriverProgressDelegate?
    
    init(webView: WKWebView) {
        self.webview = webView
        super.init()
        self.webview.navigationDelegate = self
    }
    
    func oneAtATime(closure: () -> ()) {
        objc_sync_enter(self)
        closure()
        objc_sync_exit(self)
    }
    
    func visit(location: String) {
        print("visiting \(location)")
        self.webview.loadRequest(NSURLRequest(URL: NSURL(string: location)!))
    }
    
    func labelFor(element:String, completion: ((String) -> Void)) {
        let js = "var answer; var mem = document.getElementById('\(element)'); if (mem) { answer = mem.parentElement.children[0].textContent }; mem && answer || 'null'"
        
        run(js) { result in
            if result != "null" {
                print("read label \(result)")
                completion(result)
            }
        }
    }

    func fillIn(field:String, with:String, completion: ((String) -> Void)) {
        var printValue = with
        if (field as NSString).containsString("pwd") { printValue = "[redacted]" }
        print("fill in value \(printValue)")
        
        run("document.getElementById('\(field)').value = \"\(with)\";") { result in
            completion(result)
        }
    }

    func click(field:String, completion: ((String) -> Void)) {
        print("click \(field)")
        run("document.getElementById('\(field)').click();") { (result) in
            completion(result)
            
            // for some reason, clicking sometimes doesn't work without this
            self.run("$('body').text()") { result in }

            // temporarily show the webview onscreen because annoyingly without 
            // doing this also sometimes hangs
            let window = UIApplication.sharedApplication().delegate!.window!
            if self.webview.superview == nil {
                print("WEBVIEW ADDED TO VIEW HIERARCHY")
                window?.addSubview(self.webview)
                Async.main(after: 2) {
                    print("WEBVIEW REMOVED FROM VIEW HIERARCHY")
                    self.webview.removeFromSuperview()
                }
            }

        }
    }
    
{'fraction_non_alphanumeric': 0.1303030303030303, 'fraction_numerical': 0.005844155844155844, 'mean_word_length': 2.141400407885792, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5293430', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1277, 'n_words': 302}