From 083f5ee0d411233ea06cf94f1d65856ea5443f8e Mon Sep 17 00:00:00 2001 From: Krisztian Szabo Date: Wed, 10 Apr 2024 22:14:47 +0200 Subject: [PATCH] Implement React compatible input value setting + format code Fix input value appending on repeated setting of values --- _Project/Browser/ViewController.m | 1030 +++++++++++++++-------------- 1 file changed, 525 insertions(+), 505 deletions(-) diff --git a/_Project/Browser/ViewController.m b/_Project/Browser/ViewController.m index f1ca354..b82fba0 100644 --- a/_Project/Browser/ViewController.m +++ b/_Project/Browser/ViewController.m @@ -99,14 +99,14 @@ -(void)initWebView { //[self.view addSubview: self.webview]; [self.browserContainerView addSubview: self.webview]; - + [self.webview setFrame:self.view.bounds]; [self.webview setDelegate:self]; [self.webview setLayoutMargins:UIEdgeInsetsZero]; UIScrollView *scrollView = [self.webview scrollView]; [scrollView setLayoutMargins:UIEdgeInsetsZero]; scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; - + NSNumber *showTopNavBar = [[NSUserDefaults standardUserDefaults] objectForKey:@"ShowTopNavigationBar"]; self.topMenuView.hidden = !(showTopNavBar ? showTopNavBar.boolValue : YES); [self updateTopNavAndWebView]; @@ -151,15 +151,15 @@ -(void)viewDidLoad { // Spinner now also in Storyboard. /*loadingSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; - loadingSpinner.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds)); - loadingSpinner.tintColor = [UIColor blackColor];*/ + loadingSpinner.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds)); + loadingSpinner.tintColor = [UIColor blackColor];*/ self.loadingSpinner.hidesWhenStopped = YES; //[loadingSpinner startAnimating]; //[self.view addSubview:loadingSpinner]; //[self.browserContainerView addSubview:loadingSpinner]; // Now in Storyboard - + //[self.view bringSubviewToFront:loadingSpinner]; //ENABLE CURSOR MODE INITIALLY self.cursorMode = YES; @@ -250,23 +250,23 @@ -(void)showAdvancedMenu UIAlertAction *topBarAction; if(self.topMenuShowing == YES) { - topBarAction = [UIAlertAction - actionWithTitle:@"Hide Top Navigation bar" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - [self hideTopNav]; - }]; + topBarAction = [UIAlertAction + actionWithTitle:@"Hide Top Navigation bar" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + [self hideTopNav]; + }]; } else { topBarAction = [UIAlertAction - actionWithTitle:@"Show Top Navigation bar" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - [self showTopNav]; - }]; + actionWithTitle:@"Show Top Navigation bar" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + [self showTopNav]; + }]; } UIAlertAction *loadHomePageAction = [UIAlertAction @@ -274,27 +274,27 @@ -(void)showAdvancedMenu style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self loadHomePage]; - }]; + [self loadHomePage]; + }]; UIAlertAction *setHomePageAction = [UIAlertAction actionWithTitle:@"Set Current Page As Home Page" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - NSURLRequest *request = [self.webview request]; - if (request != nil) { - if (![request.URL.absoluteString isEqual:@""]) { - [[NSUserDefaults standardUserDefaults] setObject:request.URL.absoluteString forKey:@"homepage"]; - } - } - }]; + NSURLRequest *request = [self.webview request]; + if (request != nil) { + if (![request.URL.absoluteString isEqual:@""]) { + [[NSUserDefaults standardUserDefaults] setObject:request.URL.absoluteString forKey:@"homepage"]; + } + } + }]; UIAlertAction *showHintsAction = [UIAlertAction actionWithTitle:@"Usage Guide" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self showHintsAlert]; - }]; + [self showHintsAlert]; + }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel @@ -304,306 +304,306 @@ -(void)showAdvancedMenu style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - NSArray *indexableArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"]; - UIAlertController *historyAlertController = [UIAlertController - alertControllerWithTitle:@"Favorites" - message:@"" - preferredStyle:UIAlertControllerStyleAlert]; - UIAlertAction *editFavoritesAction = [UIAlertAction - actionWithTitle:@"Delete a Favorite" - style:UIAlertActionStyleDestructive - handler:^(UIAlertAction *action) - { - NSArray *editingIndexableArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"]; - UIAlertController *editHistoryAlertController = [UIAlertController - alertControllerWithTitle:@"Delete a Favorite" - message:@"Select a Favorite to Delete" - preferredStyle:UIAlertControllerStyleAlert]; - if (editingIndexableArray != nil) { - for (int i = 0; i < [editingIndexableArray count]; i++) { - NSString *objectTitle = editingIndexableArray[i][1]; - NSString *objectSubtitle = editingIndexableArray[i][0]; - if (![[objectSubtitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { - if ([[objectTitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { - objectTitle = objectSubtitle; - } - UIAlertAction *favoriteItem = [UIAlertAction - actionWithTitle:objectTitle - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - NSMutableArray *editingArray = [editingIndexableArray mutableCopy]; - [editingArray removeObjectAtIndex:i]; - NSArray *toStoreArray = editingArray; - [[NSUserDefaults standardUserDefaults] setObject:toStoreArray forKey:@"FAVORITES"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - }]; - [editHistoryAlertController addAction:favoriteItem]; - } - } - } - [editHistoryAlertController addAction:cancelAction]; - [self presentViewController:editHistoryAlertController animated:YES completion:nil]; - - }]; - UIAlertAction *addToFavoritesAction = [UIAlertAction - actionWithTitle:@"Add Current Page to Favorites" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - NSString *theTitle=[self.webview stringByEvaluatingJavaScriptFromString:@"document.title"]; - NSURLRequest *request = [self.webview request]; - NSString *currentURL = request.URL.absoluteString; - UIAlertController *favoritesAddToController = [UIAlertController - alertControllerWithTitle:@"Name New Favorite" - message:currentURL - preferredStyle:UIAlertControllerStyleAlert]; - - [favoritesAddToController addTextFieldWithConfigurationHandler:^(UITextField *textField) - { - textField.keyboardType = UIKeyboardTypeDefault; - textField.placeholder = @"Name New Favorite"; - textField.text = theTitle; - textField.textColor = kTextColor(); - [textField setReturnKeyType:UIReturnKeyDone]; - [textField addTarget:self - action:@selector(alertTextFieldShouldReturn:) - forControlEvents:UIControlEventEditingDidEnd]; - - }]; - - UIAlertAction *saveAction = [UIAlertAction - actionWithTitle:@"Save" - style:UIAlertActionStyleDestructive - handler:^(UIAlertAction *action) - { - UITextField *titleTextField = favoritesAddToController.textFields[0]; - NSString *savedTitle = titleTextField.text; - if ([savedTitle isEqualToString:@""]) { - // Use raw URL if no title - savedTitle = currentURL; - } - NSArray *toSaveItem = [NSArray arrayWithObjects:currentURL, savedTitle, nil]; - NSMutableArray *historyArray = [NSMutableArray arrayWithObjects:toSaveItem, nil]; - if ([[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] != nil) { - historyArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] mutableCopy]; - [historyArray addObject:toSaveItem]; - } - NSArray *toStoreArray = historyArray; - [[NSUserDefaults standardUserDefaults] setObject:toStoreArray forKey:@"FAVORITES"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - - }]; - [favoritesAddToController addAction:saveAction]; - [favoritesAddToController addAction:cancelAction]; - [self presentViewController:favoritesAddToController animated:YES completion:nil]; - //UITextField *textFieldAlert = favoritesAddToController.textFields[0]; - //[textFieldAlert becomeFirstResponder]; - - }]; - if (indexableArray != nil) { - for (int i = 0; i < [indexableArray count]; i++) { - NSString *objectTitle = indexableArray[i][1]; - NSString *objectURL = indexableArray[i][0]; - if ([[objectTitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { - // Use raw URL if no title - objectTitle = objectURL; - } - UIAlertAction *favoriteItem = [UIAlertAction - actionWithTitle:objectTitle - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: objectURL]]]; - }]; - [historyAlertController addAction:favoriteItem]; - } - } - if ([[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] != nil) { - if ([[[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] count] > 0) { - [historyAlertController addAction:editFavoritesAction]; - } - } - [historyAlertController addAction:addToFavoritesAction]; - [historyAlertController addAction:cancelAction]; - [self presentViewController:historyAlertController animated:YES completion:nil]; - }]; + NSArray *indexableArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"]; + UIAlertController *historyAlertController = [UIAlertController + alertControllerWithTitle:@"Favorites" + message:@"" + preferredStyle:UIAlertControllerStyleAlert]; + UIAlertAction *editFavoritesAction = [UIAlertAction + actionWithTitle:@"Delete a Favorite" + style:UIAlertActionStyleDestructive + handler:^(UIAlertAction *action) + { + NSArray *editingIndexableArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"]; + UIAlertController *editHistoryAlertController = [UIAlertController + alertControllerWithTitle:@"Delete a Favorite" + message:@"Select a Favorite to Delete" + preferredStyle:UIAlertControllerStyleAlert]; + if (editingIndexableArray != nil) { + for (int i = 0; i < [editingIndexableArray count]; i++) { + NSString *objectTitle = editingIndexableArray[i][1]; + NSString *objectSubtitle = editingIndexableArray[i][0]; + if (![[objectSubtitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { + if ([[objectTitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { + objectTitle = objectSubtitle; + } + UIAlertAction *favoriteItem = [UIAlertAction + actionWithTitle:objectTitle + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + NSMutableArray *editingArray = [editingIndexableArray mutableCopy]; + [editingArray removeObjectAtIndex:i]; + NSArray *toStoreArray = editingArray; + [[NSUserDefaults standardUserDefaults] setObject:toStoreArray forKey:@"FAVORITES"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + }]; + [editHistoryAlertController addAction:favoriteItem]; + } + } + } + [editHistoryAlertController addAction:cancelAction]; + [self presentViewController:editHistoryAlertController animated:YES completion:nil]; + + }]; + UIAlertAction *addToFavoritesAction = [UIAlertAction + actionWithTitle:@"Add Current Page to Favorites" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + NSString *theTitle=[self.webview stringByEvaluatingJavaScriptFromString:@"document.title"]; + NSURLRequest *request = [self.webview request]; + NSString *currentURL = request.URL.absoluteString; + UIAlertController *favoritesAddToController = [UIAlertController + alertControllerWithTitle:@"Name New Favorite" + message:currentURL + preferredStyle:UIAlertControllerStyleAlert]; + + [favoritesAddToController addTextFieldWithConfigurationHandler:^(UITextField *textField) + { + textField.keyboardType = UIKeyboardTypeDefault; + textField.placeholder = @"Name New Favorite"; + textField.text = theTitle; + textField.textColor = kTextColor(); + [textField setReturnKeyType:UIReturnKeyDone]; + [textField addTarget:self + action:@selector(alertTextFieldShouldReturn:) + forControlEvents:UIControlEventEditingDidEnd]; + + }]; + + UIAlertAction *saveAction = [UIAlertAction + actionWithTitle:@"Save" + style:UIAlertActionStyleDestructive + handler:^(UIAlertAction *action) + { + UITextField *titleTextField = favoritesAddToController.textFields[0]; + NSString *savedTitle = titleTextField.text; + if ([savedTitle isEqualToString:@""]) { + // Use raw URL if no title + savedTitle = currentURL; + } + NSArray *toSaveItem = [NSArray arrayWithObjects:currentURL, savedTitle, nil]; + NSMutableArray *historyArray = [NSMutableArray arrayWithObjects:toSaveItem, nil]; + if ([[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] != nil) { + historyArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] mutableCopy]; + [historyArray addObject:toSaveItem]; + } + NSArray *toStoreArray = historyArray; + [[NSUserDefaults standardUserDefaults] setObject:toStoreArray forKey:@"FAVORITES"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + + }]; + [favoritesAddToController addAction:saveAction]; + [favoritesAddToController addAction:cancelAction]; + [self presentViewController:favoritesAddToController animated:YES completion:nil]; + //UITextField *textFieldAlert = favoritesAddToController.textFields[0]; + //[textFieldAlert becomeFirstResponder]; + + }]; + if (indexableArray != nil) { + for (int i = 0; i < [indexableArray count]; i++) { + NSString *objectTitle = indexableArray[i][1]; + NSString *objectURL = indexableArray[i][0]; + if ([[objectTitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { + // Use raw URL if no title + objectTitle = objectURL; + } + UIAlertAction *favoriteItem = [UIAlertAction + actionWithTitle:objectTitle + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: objectURL]]]; + }]; + [historyAlertController addAction:favoriteItem]; + } + } + if ([[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] != nil) { + if ([[[NSUserDefaults standardUserDefaults] arrayForKey:@"FAVORITES"] count] > 0) { + [historyAlertController addAction:editFavoritesAction]; + } + } + [historyAlertController addAction:addToFavoritesAction]; + [historyAlertController addAction:cancelAction]; + [self presentViewController:historyAlertController animated:YES completion:nil]; + }]; UIAlertAction *viewHistoryAction = [UIAlertAction actionWithTitle:@"History" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - NSArray *indexableArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"HISTORY"]; - UIAlertController *historyAlertController = [UIAlertController - alertControllerWithTitle:@"History" - message:@"" - preferredStyle:UIAlertControllerStyleAlert]; - UIAlertAction *clearHistoryAction = [UIAlertAction - actionWithTitle:@"Clear History" - style:UIAlertActionStyleDestructive - handler:^(UIAlertAction *action) - { - [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"HISTORY"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - - }]; - if ([[NSUserDefaults standardUserDefaults] arrayForKey:@"HISTORY"] != nil) { - [historyAlertController addAction:clearHistoryAction]; - } - for (int i = 0; i < [indexableArray count]; i++) { - NSString *objectTitle = indexableArray[i][1]; - NSString *objectSubtitle = indexableArray[i][0]; - if (![[objectSubtitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { - if ([[objectTitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { - objectTitle = objectSubtitle; - } - else { - objectTitle = [NSString stringWithFormat:@"%@ - %@",objectTitle,objectSubtitle ]; - } - UIAlertAction *historyItem = [UIAlertAction - actionWithTitle:objectTitle - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: indexableArray[i][0]]]]; - }]; - [historyAlertController addAction:historyItem]; - } - } - [historyAlertController addAction:cancelAction]; - [self presentViewController:historyAlertController animated:YES completion:nil]; - }]; + NSArray *indexableArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"HISTORY"]; + UIAlertController *historyAlertController = [UIAlertController + alertControllerWithTitle:@"History" + message:@"" + preferredStyle:UIAlertControllerStyleAlert]; + UIAlertAction *clearHistoryAction = [UIAlertAction + actionWithTitle:@"Clear History" + style:UIAlertActionStyleDestructive + handler:^(UIAlertAction *action) + { + [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"HISTORY"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + + }]; + if ([[NSUserDefaults standardUserDefaults] arrayForKey:@"HISTORY"] != nil) { + [historyAlertController addAction:clearHistoryAction]; + } + for (int i = 0; i < [indexableArray count]; i++) { + NSString *objectTitle = indexableArray[i][1]; + NSString *objectSubtitle = indexableArray[i][0]; + if (![[objectSubtitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { + if ([[objectTitle stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { + objectTitle = objectSubtitle; + } + else { + objectTitle = [NSString stringWithFormat:@"%@ - %@",objectTitle,objectSubtitle ]; + } + UIAlertAction *historyItem = [UIAlertAction + actionWithTitle:objectTitle + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: indexableArray[i][0]]]]; + }]; + [historyAlertController addAction:historyItem]; + } + } + [historyAlertController addAction:cancelAction]; + [self presentViewController:historyAlertController animated:YES completion:nil]; + }]; UIAlertAction *mobileModeAction = [UIAlertAction actionWithTitle:@"Switch To Mobile Mode" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1", @"UserAgent", nil]; - [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MobileMode"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - NSURLRequest *request = [self.webview request]; - if (request != nil) { - if (![request.URL.absoluteString isEqual:@""]) { - [[NSUserDefaults standardUserDefaults] setObject:request.URL.absoluteString forKey:@"savedURLtoReopen"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - } - } - NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; - for (NSHTTPCookie *cookie in [storage cookies]) { - [storage deleteCookie:cookie]; - } - [[NSURLCache sharedURLCache] removeAllCachedResponses]; - [[NSUserDefaults standardUserDefaults] synchronize]; - [[NSURLSession sharedSession] resetWithCompletionHandler:^{ - dispatch_sync(dispatch_get_main_queue(), ^{ - [self.webview removeFromSuperview]; - [self initWebView]; - [self.view bringSubviewToFront:self.cursorView]; - //[self.view bringSubviewToFront:self->loadingSpinner]; - [self webViewDidAppear]; - - }); - }]; - }]; + NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1", @"UserAgent", nil]; + [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; + [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"MobileMode"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + NSURLRequest *request = [self.webview request]; + if (request != nil) { + if (![request.URL.absoluteString isEqual:@""]) { + [[NSUserDefaults standardUserDefaults] setObject:request.URL.absoluteString forKey:@"savedURLtoReopen"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + } + } + NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; + for (NSHTTPCookie *cookie in [storage cookies]) { + [storage deleteCookie:cookie]; + } + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + [[NSUserDefaults standardUserDefaults] synchronize]; + [[NSURLSession sharedSession] resetWithCompletionHandler:^{ + dispatch_sync(dispatch_get_main_queue(), ^{ + [self.webview removeFromSuperview]; + [self initWebView]; + [self.view bringSubviewToFront:self.cursorView]; + //[self.view bringSubviewToFront:self->loadingSpinner]; + [self webViewDidAppear]; + + }); + }]; + }]; UIAlertAction *desktopModeAction = [UIAlertAction actionWithTitle:@"Switch To Desktop Mode" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15", @"UserAgent", nil]; - [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; - [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"MobileMode"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - NSURLRequest *request = [self.webview request]; - if (request != nil) { - if (![request.URL.absoluteString isEqual:@""]) { - [[NSUserDefaults standardUserDefaults] setObject:request.URL.absoluteString forKey:@"savedURLtoReopen"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - } - } - NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; - for (NSHTTPCookie *cookie in [storage cookies]) { - [storage deleteCookie:cookie]; - } - [[NSURLCache sharedURLCache] removeAllCachedResponses]; - [[NSUserDefaults standardUserDefaults] synchronize]; - [[NSURLSession sharedSession] resetWithCompletionHandler:^{ - dispatch_sync(dispatch_get_main_queue(), ^{ - [self.webview removeFromSuperview]; - [self initWebView]; - [self.view bringSubviewToFront:self.cursorView]; - //[self.view bringSubviewToFront:self->loadingSpinner]; - [self webViewDidAppear]; - - }); - }]; - }]; + NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15", @"UserAgent", nil]; + [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; + [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"MobileMode"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + NSURLRequest *request = [self.webview request]; + if (request != nil) { + if (![request.URL.absoluteString isEqual:@""]) { + [[NSUserDefaults standardUserDefaults] setObject:request.URL.absoluteString forKey:@"savedURLtoReopen"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + } + } + NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; + for (NSHTTPCookie *cookie in [storage cookies]) { + [storage deleteCookie:cookie]; + } + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + [[NSUserDefaults standardUserDefaults] synchronize]; + [[NSURLSession sharedSession] resetWithCompletionHandler:^{ + dispatch_sync(dispatch_get_main_queue(), ^{ + [self.webview removeFromSuperview]; + [self initWebView]; + [self.view bringSubviewToFront:self.cursorView]; + //[self.view bringSubviewToFront:self->loadingSpinner]; + [self webViewDidAppear]; + + }); + }]; + }]; UIAlertAction *scalePageToFitAction = [UIAlertAction actionWithTitle:@"Scale Pages to Fit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ScalePagesToFit"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - [self.webview setScalesPageToFit:YES]; - [self.webview setContentMode:UIViewContentModeScaleAspectFit]; - [self.webview reload]; - }]; + [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ScalePagesToFit"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + [self.webview setScalesPageToFit:YES]; + [self.webview setContentMode:UIViewContentModeScaleAspectFit]; + [self.webview reload]; + }]; UIAlertAction *stopScalePageToFitAction = [UIAlertAction actionWithTitle:@"Stop Scaling Pages to Fit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"ScalePagesToFit"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - [self.webview setScalesPageToFit:NO]; - [self.webview reload]; - }]; + [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"ScalePagesToFit"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + [self.webview setScalesPageToFit:NO]; + [self.webview reload]; + }]; UIAlertAction *increaseFontSizeAction = [UIAlertAction actionWithTitle:@"Increase Font Size" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - self.textFontSize += 5; - [self updateTextFontSize]; - }]; + self.textFontSize += 5; + [self updateTextFontSize]; + }]; UIAlertAction *decreaseFontSizeAction = [UIAlertAction actionWithTitle:@"Decrease Font Size" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - self.textFontSize -= 5; - [self updateTextFontSize]; - }]; + self.textFontSize -= 5; + [self updateTextFontSize]; + }]; UIAlertAction *clearCacheAction = [UIAlertAction actionWithTitle:@"Clear Cache" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { - [[NSURLCache sharedURLCache] removeAllCachedResponses]; - [[NSUserDefaults standardUserDefaults] synchronize]; - self.previousURL = @""; - [self.webview reload]; - - }]; + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + [[NSUserDefaults standardUserDefaults] synchronize]; + self.previousURL = @""; + [self.webview reload]; + + }]; UIAlertAction *clearCookiesAction = [UIAlertAction actionWithTitle:@"Clear Cookies" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { - NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; - for (NSHTTPCookie *cookie in [storage cookies]) { - [storage deleteCookie:cookie]; - } - [[NSUserDefaults standardUserDefaults] synchronize]; - self.previousURL = @""; - [self.webview reload]; - - }]; + NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; + for (NSHTTPCookie *cookie in [storage cookies]) { + [storage deleteCookie:cookie]; + } + [[NSUserDefaults standardUserDefaults] synchronize]; + self.previousURL = @""; + [self.webview reload]; + + }]; /* @@ -622,7 +622,7 @@ -(void)showAdvancedMenu } } */ - + [alertController addAction:viewFavoritesAction]; [alertController addAction:viewHistoryAction]; [alertController addAction:loadHomePageAction]; @@ -671,15 +671,15 @@ -(void)showInputURLorSearchGoogle [alertController2 addTextFieldWithConfigurationHandler:^(UITextField *textField) { - textField.keyboardType = UIKeyboardTypeURL; - textField.placeholder = @"Enter URL or Search Terms"; - textField.textColor = kTextColor(); - [textField setReturnKeyType:UIReturnKeyDone]; - [textField addTarget:self - action:@selector(alertTextFieldShouldReturn:) - forControlEvents:UIControlEventEditingDidEnd]; - - }]; + textField.keyboardType = UIKeyboardTypeURL; + textField.placeholder = @"Enter URL or Search Terms"; + textField.textColor = kTextColor(); + [textField setReturnKeyType:UIReturnKeyDone]; + [textField addTarget:self + action:@selector(alertTextFieldShouldReturn:) + forControlEvents:UIControlEventEditingDidEnd]; + + }]; UIAlertAction *goAction = [UIAlertAction @@ -687,60 +687,60 @@ -(void)showInputURLorSearchGoogle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - UITextField *urltextfield = alertController2.textFields[0]; - NSString *toMod = urltextfield.text; - /* - if ([toMod containsString:@" "] || ![temporaryURL containsString:@"."]) { - toMod = [toMod stringByReplacingOccurrencesOfString:@" " withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"." withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; - toMod = [toMod stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; - if (toMod != nil) { - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/search?q=%@", toMod]]]]; - } - else { - [self requestURLorSearchInput]; - } - } - else { - */ - if (![toMod isEqualToString:@""]) { - if ([toMod containsString:@"http://"] || [toMod containsString:@"https://"]) { - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", toMod]]]]; - } - else { - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", toMod]]]]; - } - } - else { - [self requestURLorSearchInput]; - } - //} - - }]; + UITextField *urltextfield = alertController2.textFields[0]; + NSString *toMod = urltextfield.text; + /* + if ([toMod containsString:@" "] || ![temporaryURL containsString:@"."]) { + toMod = [toMod stringByReplacingOccurrencesOfString:@" " withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"." withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; + toMod = [toMod stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + if (toMod != nil) { + [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/search?q=%@", toMod]]]]; + } + else { + [self requestURLorSearchInput]; + } + } + else { + */ + if (![toMod isEqualToString:@""]) { + if ([toMod containsString:@"http://"] || [toMod containsString:@"https://"]) { + [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", toMod]]]]; + } + else { + [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", toMod]]]]; + } + } + else { + [self requestURLorSearchInput]; + } + //} + + }]; UIAlertAction *searchAction = [UIAlertAction actionWithTitle:@"Search Google" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - UITextField *urltextfield = alertController2.textFields[0]; - NSString *toMod = urltextfield.text; - toMod = [toMod stringByReplacingOccurrencesOfString:@" " withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"." withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; - toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; - toMod = [toMod stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; - if (toMod != nil) { - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/search?q=%@", toMod]]]]; - } - else { - [self requestURLorSearchInput]; - } - }]; + UITextField *urltextfield = alertController2.textFields[0]; + NSString *toMod = urltextfield.text; + toMod = [toMod stringByReplacingOccurrencesOfString:@" " withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"." withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; + toMod = [toMod stringByReplacingOccurrencesOfString:@"++" withString:@"+"]; + toMod = [toMod stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + if (toMod != nil) { + [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/search?q=%@", toMod]]]]; + } + else { + [self requestURLorSearchInput]; + } + }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" @@ -754,7 +754,7 @@ -(void)showInputURLorSearchGoogle [self presentViewController:alertController2 animated:YES completion:nil]; NSURLRequest *request = [self.webview request]; - + if (request == nil) { UITextField *loginTextField = alertController2.textFields[0]; @@ -787,12 +787,12 @@ -(void)requestURLorSearchInput UIAlertAction *forwardAction = [UIAlertAction - actionWithTitle:@"Go Forward" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - [self.webview goForward]; - }]; + actionWithTitle:@"Go Forward" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + [self.webview goForward]; + }]; UIAlertAction *reloadAction = [UIAlertAction @@ -800,9 +800,9 @@ -(void)requestURLorSearchInput style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - self.previousURL = @""; - [self.webview reload]; - }]; + self.previousURL = @""; + [self.webview reload]; + }]; UIAlertAction *cancelAction = [UIAlertAction @@ -815,10 +815,10 @@ -(void)requestURLorSearchInput style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - - [self showInputURLorSearchGoogle]; - - }]; + + [self showInputURLorSearchGoogle]; + + }]; if([self.webview canGoForward]) @@ -898,36 +898,36 @@ - (void)webView:(id)webView didFailLoadWithError:(NSError *)error { style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - if (self.requestURL != nil) { - if ([self.requestURL length] > 1) { - NSString *lastChar = [self.requestURL substringFromIndex: [self.requestURL length] - 1]; - if ([lastChar isEqualToString:@"/"]) { - NSString *newString = [self.requestURL substringToIndex:[self.requestURL length]-1]; - self.requestURL = newString; - } - } - self.requestURL = [self.requestURL stringByReplacingOccurrencesOfString:@"http://" withString:@""]; - self.requestURL = [self.requestURL stringByReplacingOccurrencesOfString:@"https://" withString:@""]; - self.requestURL = [self.requestURL stringByReplacingOccurrencesOfString:@"www." withString:@""]; - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/search?q=%@", self.requestURL]]]]; - } - - }]; + if (self.requestURL != nil) { + if ([self.requestURL length] > 1) { + NSString *lastChar = [self.requestURL substringFromIndex: [self.requestURL length] - 1]; + if ([lastChar isEqualToString:@"/"]) { + NSString *newString = [self.requestURL substringToIndex:[self.requestURL length]-1]; + self.requestURL = newString; + } + } + self.requestURL = [self.requestURL stringByReplacingOccurrencesOfString:@"http://" withString:@""]; + self.requestURL = [self.requestURL stringByReplacingOccurrencesOfString:@"https://" withString:@""]; + self.requestURL = [self.requestURL stringByReplacingOccurrencesOfString:@"www." withString:@""]; + [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/search?q=%@", self.requestURL]]]]; + } + + }]; UIAlertAction *reloadAction = [UIAlertAction actionWithTitle:@"Reload Page" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - self.previousURL = @""; - [self.webview reload]; - }]; + self.previousURL = @""; + [self.webview reload]; + }]; UIAlertAction *newurlAction = [UIAlertAction actionWithTitle:@"Enter a URL or Search" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self requestURLorSearchInput]; - }]; + [self requestURLorSearchInput]; + }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel @@ -986,23 +986,23 @@ - (void)showHintsAlert style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"DontShowHintsOnLaunch"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - }]; + [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"DontShowHintsOnLaunch"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + }]; UIAlertAction *showForeverAction = [UIAlertAction actionWithTitle:@"Always Show On Launch" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { - [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"DontShowHintsOnLaunch"]; - [[NSUserDefaults standardUserDefaults] synchronize]; - }]; + [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"DontShowHintsOnLaunch"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { - }]; + }]; if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DontShowHintsOnLaunch"]) { [alertController addAction:showForeverAction]; } @@ -1081,9 +1081,9 @@ -(void)pressesEnded:(NSSet *)presses withEvent:(UIPressesEvent *)even [self presentViewController:alert animated:YES completion:nil]; } /* - else { - [self requestURLorSearchInput]; - }*/ + else { + [self requestURLorSearchInput]; + }*/ } else if (presses.anyObject.type == UIPressTypeUpArrow) @@ -1108,7 +1108,7 @@ -(void)pressesEnded:(NSSet *)presses withEvent:(UIPressesEvent *)even // Handle the virtual cursor - + CGPoint point = [self.view convertPoint:self.cursorView.frame.origin toView:self.webview]; if(point.y < 0) @@ -1118,7 +1118,7 @@ -(void)pressesEnded:(NSSet *)presses withEvent:(UIPressesEvent *)even CGRect backBtnFrameExtra = self.btnImageBack.frame; backBtnFrameExtra.origin.y = 0; // Enable cursor in upper right corner backBtnFrameExtra.size.height = backBtnFrameExtra.size.height+ 8;// Enable cursor in upper right corner - + if(CGRectContainsPoint(backBtnFrameExtra, point)) { @@ -1140,7 +1140,7 @@ -(void)pressesEnded:(NSSet *)presses withEvent:(UIPressesEvent *)even { [self showInputURLorSearchGoogle]; } - + else if(CGRectContainsPoint(self.btnImageFullScreen.frame, point)) { @@ -1156,139 +1156,159 @@ -(void)pressesEnded:(NSSet *)presses withEvent:(UIPressesEvent *)even menuBtnFrameExtra.origin.y = 0; // Enable cursor in upper right corner menuBtnFrameExtra.size.width = menuBtnFrameExtra.size.width + 100; // Enable cursor in upper right corner menuBtnFrameExtra.size.height = menuBtnFrameExtra.size.height+ 100;// Enable cursor in upper right corner - + if(CGRectContainsPoint(menuBtnFrameExtra, point)) { // Show advanced menu: [self showAdvancedMenu]; } - - - + + + } else // Handle Press in the Browser view { - - int displayWidth = [[self.webview stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] intValue]; - CGFloat scale = [self.webview frame].size.width / displayWidth; - - point.x /= scale; - point.y /= scale; - - [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).click()", (int)point.x, (int)point.y]]; - // Make the UIWebView method call - NSString *fieldType = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).type;", (int)point.x, (int)point.y]]; - /* - if (fieldType == nil) { - NSString *contentEditible = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).getAttribute('contenteditable');", (int)point.x, (int)point.y]]; - NSLog(contentEditible); - if ([contentEditible isEqualToString:@"true"]) { - fieldType = @"text"; - } - } - else if ([[fieldType stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { - NSString *contentEditible = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).getAttribute('contenteditable');", (int)point.x, (int)point.y]]; - NSLog(contentEditible); - if ([contentEditible isEqualToString:@"true"]) { - fieldType = @"text"; - } - } - NSLog(fieldType); - */ - fieldType = fieldType.lowercaseString; - if ([fieldType isEqualToString:@"date"] || [fieldType isEqualToString:@"datetime"] || [fieldType isEqualToString:@"datetime-local"] || [fieldType isEqualToString:@"email"] || [fieldType isEqualToString:@"month"] || [fieldType isEqualToString:@"number"] || [fieldType isEqualToString:@"password"] || [fieldType isEqualToString:@"search"] || [fieldType isEqualToString:@"tel"] || [fieldType isEqualToString:@"text"] || [fieldType isEqualToString:@"time"] || [fieldType isEqualToString:@"url"] || [fieldType isEqualToString:@"week"]) { - NSString *fieldTitle = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).title;", (int)point.x, (int)point.y]]; - if ([fieldTitle isEqualToString:@""]) { - fieldTitle = fieldType; - } - NSString *placeholder = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).placeholder;", (int)point.x, (int)point.y]]; - if ([placeholder isEqualToString:@""]) { - if (![fieldTitle isEqualToString:fieldType]) { - placeholder = [NSString stringWithFormat:@"%@ Input", fieldTitle]; + + int displayWidth = [[self.webview stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] intValue]; + CGFloat scale = [self.webview frame].size.width / displayWidth; + + point.x /= scale; + point.y /= scale; + + [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).click()", (int)point.x, (int)point.y]]; + // Make the UIWebView method call + NSString *fieldType = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).type;", (int)point.x, (int)point.y]]; + /* + if (fieldType == nil) { + NSString *contentEditible = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).getAttribute('contenteditable');", (int)point.x, (int)point.y]]; + NSLog(contentEditible); + if ([contentEditible isEqualToString:@"true"]) { + fieldType = @"text"; + } + } + else if ([[fieldType stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString: @""]) { + NSString *contentEditible = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).getAttribute('contenteditable');", (int)point.x, (int)point.y]]; + NSLog(contentEditible); + if ([contentEditible isEqualToString:@"true"]) { + fieldType = @"text"; + } + } + NSLog(fieldType); + */ + fieldType = fieldType.lowercaseString; + if ([fieldType isEqualToString:@"date"] || [fieldType isEqualToString:@"datetime"] || [fieldType isEqualToString:@"datetime-local"] || [fieldType isEqualToString:@"email"] || [fieldType isEqualToString:@"month"] || [fieldType isEqualToString:@"number"] || [fieldType isEqualToString:@"password"] || [fieldType isEqualToString:@"search"] || [fieldType isEqualToString:@"tel"] || [fieldType isEqualToString:@"text"] || [fieldType isEqualToString:@"time"] || [fieldType isEqualToString:@"url"] || [fieldType isEqualToString:@"week"]) { + NSString *fieldTitle = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).title;", (int)point.x, (int)point.y]]; + if ([fieldTitle isEqualToString:@""]) { + fieldTitle = fieldType; } - else { - placeholder = @"Text Input"; + NSString *placeholder = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).placeholder;", (int)point.x, (int)point.y]]; + if ([placeholder isEqualToString:@""]) { + if (![fieldTitle isEqualToString:fieldType]) { + placeholder = [NSString stringWithFormat:@"%@ Input", fieldTitle]; + } + else { + placeholder = @"Text Input"; + } } - } - NSString *testedFormResponse = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).form.hasAttribute('onsubmit') || document.elementFromPoint(%i, %i).form.hasAttribute('method');", (int)point.x, (int)point.y, (int)point.x, (int)point.y]]; - UIAlertController *alertController = [UIAlertController - alertControllerWithTitle:@"Input Text" - message: [fieldTitle capitalizedString] - preferredStyle:UIAlertControllerStyleAlert]; - - [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) - { - if ([fieldType isEqualToString:@"url"]) { - textField.keyboardType = UIKeyboardTypeURL; - } - else if ([fieldType isEqualToString:@"email"]) { - textField.keyboardType = UIKeyboardTypeEmailAddress; - } - else if ([fieldType isEqualToString:@"tel"] || [fieldType isEqualToString:@"number"] || [fieldType isEqualToString:@"date"] || [fieldType isEqualToString:@"datetime"] || [fieldType isEqualToString:@"datetime-local"]) { - textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; - } - else { - textField.keyboardType = UIKeyboardTypeDefault; - } - textField.placeholder = [placeholder capitalizedString]; - if ([fieldType isEqualToString:@"password"]) { - textField.secureTextEntry = YES; - } - textField.text = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).value;", (int)point.x, (int)point.y]]; - textField.textColor = kTextColor(); - [textField setReturnKeyType:UIReturnKeyDone]; - [textField addTarget:self - action:@selector(alertTextFieldShouldReturn:) - forControlEvents:UIControlEventEditingDidEnd]; - - }]; - UIAlertAction *inputAndSubmitAction = [UIAlertAction - actionWithTitle:@"Submit" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - UITextField *inputViewTextField = alertController.textFields[0]; - NSString *javaScript = [NSString stringWithFormat:@"var textField = document.elementFromPoint(%i, %i);" - "textField.value = '%@';" - "textField.form.submit();" - //"var ev = document.createEvent('KeyboardEvent');" - //"ev.initKeyEvent('keydown', true, true, window, false, false, false, false, 13, 0);" - //"document.body.dispatchEvent(ev);" - , (int)point.x, (int)point.y, inputViewTextField.text]; - [self.webview stringByEvaluatingJavaScriptFromString:javaScript]; - }]; - UIAlertAction *inputAction = [UIAlertAction - actionWithTitle:@"Done" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) - { - UITextField *inputViewTextField = alertController.textFields[0]; - NSString *javaScript = [NSString stringWithFormat:@"var textField = document.elementFromPoint(%i, %i);" - "textField.value = '%@';", (int)point.x, (int)point.y, inputViewTextField.text]; - [self.webview stringByEvaluatingJavaScriptFromString:javaScript]; - }]; - UIAlertAction *cancelAction = [UIAlertAction - actionWithTitle:@"Cancel" - style:UIAlertActionStyleCancel - handler:nil]; - [alertController addAction:inputAction]; - if (testedFormResponse != nil) { - if ([testedFormResponse isEqualToString:@"true"]) { - [alertController addAction:inputAndSubmitAction]; + NSString *testedFormResponse = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).form.hasAttribute('onsubmit') || document.elementFromPoint(%i, %i).form.hasAttribute('method');", (int)point.x, (int)point.y, (int)point.x, (int)point.y]]; + UIAlertController *alertController = [UIAlertController + alertControllerWithTitle:@"Input Text" + message: [fieldTitle capitalizedString] + preferredStyle:UIAlertControllerStyleAlert]; + + [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) + { + if ([fieldType isEqualToString:@"url"]) { + textField.keyboardType = UIKeyboardTypeURL; + } + else if ([fieldType isEqualToString:@"email"]) { + textField.keyboardType = UIKeyboardTypeEmailAddress; + } + else if ([fieldType isEqualToString:@"tel"] || [fieldType isEqualToString:@"number"] || [fieldType isEqualToString:@"date"] || [fieldType isEqualToString:@"datetime"] || [fieldType isEqualToString:@"datetime-local"]) { + textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; + } + else { + textField.keyboardType = UIKeyboardTypeDefault; + } + textField.placeholder = [placeholder capitalizedString]; + if ([fieldType isEqualToString:@"password"]) { + textField.secureTextEntry = YES; + } + textField.text = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).value;", (int)point.x, (int)point.y]]; + textField.textColor = kTextColor(); + [textField setReturnKeyType:UIReturnKeyDone]; + [textField addTarget:self + action:@selector(alertTextFieldShouldReturn:) + forControlEvents:UIControlEventEditingDidEnd]; + + }]; + UIAlertAction *inputAndSubmitAction = [UIAlertAction + actionWithTitle:@"Submit" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + UITextField *inputViewTextField = alertController.textFields[0]; + NSString *javaScript = [NSString stringWithFormat:@"var textField = document.elementFromPoint(%i, %i);" + "textField.value = '%@';" + "textField.form.submit();" + , (int)point.x, (int)point.y, inputViewTextField.text]; + [self.webview stringByEvaluatingJavaScriptFromString:javaScript]; + }]; + UIAlertAction *inputAction = [UIAlertAction + actionWithTitle:@"Done" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) + { + UITextField *inputViewTextField = alertController.textFields[0]; + NSString *inputValue = inputViewTextField.text; + NSMutableString *javaScript = [NSMutableString string]; + + [javaScript appendFormat:@"var textField = document.elementFromPoint(%i, %i);" + "var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;" + "textField.dispatchEvent(new Event('focus'));" + "nativeInputValueSetter.call(textField, '');" + , (int)point.x, (int)point.y]; + + for (NSUInteger i = 0; i < inputValue.length; i++) { + unichar character = [inputValue characterAtIndex:i]; + NSString *charString = [NSString stringWithFormat:@"%C", character]; + + [javaScript appendFormat:@"nativeInputValueSetter.call(textField, textField.value + '%@');" + "textField.dispatchEvent(new KeyboardEvent('keydown', {key: '%@', bubbles: true}));" + "textField.dispatchEvent(new KeyboardEvent('keypress', {key: '%@', bubbles: true}));" + "textField.dispatchEvent(new KeyboardEvent('keyup', {key: '%@', bubbles: true}));" + "textField.dispatchEvent(new Event('input', { bubbles: true}));" + , charString, charString, charString, charString]; + } + + [javaScript appendFormat:@"textField.dispatchEvent(new Event('blur'));" + "textField.dispatchEvent(new Event('change', { bubbles: true}));"]; + + [self.webview stringByEvaluatingJavaScriptFromString:javaScript]; + }]; + + UIAlertAction *cancelAction = [UIAlertAction + actionWithTitle:@"Cancel" + style:UIAlertActionStyleCancel + handler:nil]; + [alertController addAction:inputAction]; + if (testedFormResponse != nil) { + if ([testedFormResponse isEqualToString:@"true"]) { + [alertController addAction:inputAndSubmitAction]; + } + } + [alertController addAction:cancelAction]; + [self presentViewController:alertController animated:YES completion:nil]; + UITextField *inputViewTextField = alertController.textFields[0]; + if ([[inputViewTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) { + [inputViewTextField becomeFirstResponder]; } } - [alertController addAction:cancelAction]; - [self presentViewController:alertController animated:YES completion:nil]; - UITextField *inputViewTextField = alertController.textFields[0]; - if ([[inputViewTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) { - [inputViewTextField becomeFirstResponder]; + else { + //[self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).click()", (int)point.x, (int)point.y]]; } - } - else { - //[self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.elementFromPoint(%i, %i).click()", (int)point.x, (int)point.y]]; - } - //[self toggleMode]; + //[self toggleMode]; } }