Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBP: Debug scan model implementation #2421

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Common

protocol CCFCommunicationDelegate: AnyObject {
func loadURL(url: URL) async
func extractedProfiles(profiles: [ExtractedProfile]) async
func extractedProfiles(profiles: [ExtractedProfile], meta: [String: Any]?) async
func captchaInformation(captchaInfo: GetCaptchaInfoResponse) async
func solveCaptcha(with response: SolveCaptchaResponse) async
func success(actionId: String, actionType: ActionType) async
Expand Down Expand Up @@ -101,7 +101,7 @@ struct DataBrokerProtectionFeature: Subfeature {
await delegate?.onError(error: DataBrokerProtectionError.malformedURL)
}
case .extract(let profiles):
await delegate?.extractedProfiles(profiles: profiles)
await delegate?.extractedProfiles(profiles: profiles, meta: success.meta)
case .getCaptchaInfo(let captchaInfo):
await delegate?.captchaInformation(captchaInfo: captchaInfo)
case .solveCaptcha(let response):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import Common
protocol WebViewHandler: NSObject {
func initializeWebView(showWebView: Bool) async
func load(url: URL) async throws
func takeSnaphost(path: String, fileName: String) async throws
func saveHTML(path: String, fileName: String) async throws
func waitForWebViewLoad(timeoutInSeconds: Int) async throws
func finish() async
func execute(action: Action, data: CCFRequestData) async
Expand Down Expand Up @@ -122,6 +124,75 @@ final class DataBrokerProtectionWebViewHandler: NSObject, WebViewHandler {
func evaluateJavaScript(_ javaScript: String) async throws {
_ = webView?.evaluateJavaScript(javaScript, in: nil, in: WKContentWorld.page)
}

func takeSnaphost(path: String, fileName: String) async throws {
let script = "document.body.scrollHeight"

let result = try await webView?.evaluateJavaScript(script)

if let height = result as? CGFloat {
webView?.frame = CGRect(origin: .zero, size: CGSize(width: 1024, height: height))
let configuration = WKSnapshotConfiguration()
configuration.rect = CGRect(x: 0, y: 0, width: webView?.frame.size.width ?? 0.0, height: height)
if let image = try await webView?.takeSnapshot(configuration: configuration) {
saveToDisk(image: image, path: path, fileName: fileName)
}
}
}

func saveHTML(path: String, fileName: String) async throws {
let result = try await webView?.evaluateJavaScript("document.documentElement.outerHTML")
let fileManager = FileManager.default

if let htmlString = result as? String {
do {
if !fileManager.fileExists(atPath: path) {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
}

let fileURL = URL(fileURLWithPath: "\(path)/\(fileName)")
try htmlString.write(to: fileURL, atomically: true, encoding: .utf8)
print("HTML content saved to file: \(fileURL)")
} catch {
print("Error writing HTML content to file: \(error)")
}
}
}

private func saveToDisk(image: NSImage, path: String, fileName: String) {
guard let tiffData = image.tiffRepresentation else {
// Handle the case where tiff representation is not available
return
}

// Create a bitmap representation from the tiff data
guard let bitmapImageRep = NSBitmapImageRep(data: tiffData) else {
// Handle the case where bitmap representation cannot be created
return
}

let fileManager = FileManager.default

if !fileManager.fileExists(atPath: path) {
do {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error creating folder: \(error)")
}
}

if let pngData = bitmapImageRep.representation(using: .png, properties: [:]) {
// Save the PNG data to a file
do {
let fileURL = URL(fileURLWithPath: "\(path)/\(fileName)")
try pngData.write(to: fileURL)
} catch {
print("Error writing PNG: \(error)")
}
} else {
print("Error png data was not respresented")
}
}
}

extension DataBrokerProtectionWebViewHandler: WKNavigationDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,42 @@ struct DataBrokerRunCustomJSONView: View {
if viewModel.results.isEmpty {
VStack(alignment: .leading) {
Text("macOS App version: \(viewModel.appVersion())")
Text("C-S-S version: \(viewModel.contentScopeScriptsVersion())")

Divider()

HStack {
TextField("First name", text: $viewModel.firstName)
.padding()
TextField("Last name", text: $viewModel.lastName)
.padding()
TextField("Middle", text: $viewModel.middle)
.padding()
ForEach(viewModel.names.indices, id: \.self) { index in
HStack {
TextField("First name", text: $viewModel.names[index].first)
.padding()
TextField("Middle", text: $viewModel.names[index].middle)
.padding()
TextField("Last name", text: $viewModel.names[index].last)
.padding()
}
}

Button("Add other name") {
viewModel.names.append(.empty())
}

Divider()

HStack {
TextField("City", text: $viewModel.city)
.padding()
TextField("State", text: $viewModel.state)
.padding()
ForEach(viewModel.addresses.indices, id: \.self) { index in
HStack {
TextField("City", text: $viewModel.addresses[index].city)
.padding()
TextField("State (two characters format)", text: $viewModel.addresses[index].state)
.onChange(of: viewModel.addresses[index].state) { newValue in
if newValue.count > 2 {
viewModel.addresses[index].state = String(newValue.prefix(2))
}
}
.padding()
}
}

Button("Add other address") {
viewModel.addresses.append(.empty())
}

Divider()
Expand Down Expand Up @@ -76,6 +92,14 @@ struct DataBrokerRunCustomJSONView: View {
Button("Run") {
viewModel.runJSON(jsonString: jsonText)
}

if viewModel.isRunningOnAllBrokers {
ProgressView("Scanning...")
} else {
Button("Run all brokers") {
viewModel.runAllBrokers()
}
}
}
.padding()
.frame(minWidth: 600, minHeight: 800)
Expand All @@ -88,19 +112,19 @@ struct DataBrokerRunCustomJSONView: View {
} else {
VStack {
VStack {
List(viewModel.results, id: \.name) { extractedProfile in
List(viewModel.results, id: \.id) { scanResult in
HStack {
Text(extractedProfile.name ?? "No name")
Text(scanResult.extractedProfile.name ?? "No name")
.padding(.horizontal, 10)
Divider()
Text(extractedProfile.addresses?.first?.fullAddress ?? "No address")
Text(scanResult.extractedProfile.addresses?.first?.fullAddress ?? "No address")
.padding(.horizontal, 10)
Divider()
Text(extractedProfile.relatives?.joined(separator: ",") ?? "No relatives")
Text(scanResult.extractedProfile.relatives?.joined(separator: ",") ?? "No relatives")
.padding(.horizontal, 10)
Divider()
Button("Opt-out") {
viewModel.runOptOut(extractedProfile: extractedProfile)
viewModel.runOptOut(scanResult: scanResult)
}
}
}.navigationTitle("Results")
Expand Down
Loading
Loading