30 #import <JavaScriptCore/JavaScriptCore.h> 34 @interface WKWebView () <WKScriptMessageHandler>
41 - (instancetype)initWithFrame:(CGRect)frame
42 configuration:(WKWebViewConfiguration *)configuration
43 andControllerDelegate:(
id<Y3WebViewDelegate>)delegate
45 self = [
super initWithFrame:frame configuration:configuration];
49 if (configuration.showConsole) {
50 WKUserContentController *userCC = configuration.userContentController;
51 [userCC addScriptMessageHandler:self name:@"log"];
53 [userCC addScriptMessageHandler:self name:@"logging"];
54 [userCC addScriptMessageHandler:self name:@"newSelectionDetected"] ;
62 - (BOOL) becomeFirstResponder
64 NSLog(
@"Y3WebView.becomeFirstResponder");
65 [y3rDelegate becomeResponder:YES];
66 BOOL ret = [
super becomeFirstResponder];
69 - (BOOL) resignFirstResponder
71 NSLog(
@"Y3WebView.resignFirstResponder");
72 [y3rDelegate becomeResponder:NO];
73 BOOL ret = [
super resignFirstResponder];
76 - (void)copy:(
id)sender {
77 NSLog(
@"Y3WebView.copyClicked: start:sender class: %@", NSStringFromClass([sender
class]));
82 - (
void (^)(id, NSError *error)) provideQueryCopyBlock:(MenuItemSegment) itemSegment
84 void (^queryCopyBlock)(id, NSError *error) = ^
void(
id jsReturn, NSError *error)
89 switch (itemSegment) {
91 [
self->y3rDelegate jsReturnIndication:jsReturn];
92 NSLog(
@"Y3WebView.provideQueryCopyBlock: ReturnBasic: %@", jsReturn);
95 [
self->y3rDelegate jsTitleReturn:jsReturn];
96 NSLog(
@"Y3WebView.provideQueryCopyBlock: ReturnTitle: %@", jsReturn);
98 case ReturnIngredients:
99 [
self->y3rDelegate jsIngredientsReturn:jsReturn];
100 NSLog(
@"Y3WebView.provideQueryCopyBlock: ReturnIngredients: %@", jsReturn);
102 case ReturnInstructions:
103 [
self->y3rDelegate jsInstructionReturn:jsReturn];
104 NSLog(
@"Y3WebView.provideQueryCopyBlock: ReturnInstructions: %@", jsReturn);
107 NSLog(
@"Y3WebView.provideQueryCopyBlock: default?: ret: %@", jsReturn);
110 [
self->y3rDelegate setControlbarColor:[UIColor greenColor]];
112 NSLog(
@"Y3WebView.provideQueryCopyBlock: completion: ERROR: %@", error);
113 [
self->y3rDelegate setControlbarColor:[UIColor redColor]];
116 return queryCopyBlock;
119 - (void) exerciseJavascriptBasic: (NSString *)js
121 void (^queryCopyBlock)(id, NSError *error) = [
self provideQueryCopyBlock:ReturnBasic];
122 [
self evaluateJavaScript:js completionHandler:queryCopyBlock];
124 - (void) exerciseJavascriptSendSelection
126 NSBundle *mb = [NSBundle mainBundle];
127 NSString *jsPathStr = [mb pathForResource:@"detectSelection"
129 NSString* content = [NSString stringWithContentsOfFile:jsPathStr
130 encoding:NSUTF8StringEncoding
135 - (void) exerciseJavascriptScanForRecipe
137 NSBundle *mb = [NSBundle mainBundle];
138 NSString *jsPathStr = [mb pathForResource:@"scanForRecipe"
140 inDirectory:@"webviewJS"];
141 NSString* content = [NSString stringWithContentsOfFile:jsPathStr
142 encoding:NSUTF8StringEncoding
144 NSLog(
@"Y3WebView.exerciseJavascriptScanForRecipe: content: %@", content);
145 void (^queryTitleCopyBlock)(id, NSError *error) = [
self provideQueryCopyBlock:ReturnTitle];
146 [
self evaluateJavaScript:content completionHandler:queryTitleCopyBlock];
156 - (void) exerciseJavascriptScanForTitle
158 NSBundle *mb = [NSBundle mainBundle];
159 NSString *jsPathStr = [mb pathForResource:@"scanForTitle"
161 inDirectory:@"webviewJS"];
162 NSString* content = [NSString stringWithContentsOfFile:jsPathStr
163 encoding:NSUTF8StringEncoding
165 NSLog(
@"Y3WebView.exerciseJavascriptScanForTitle: content: %@", content);
166 void (^queryTitleCopyBlock)(id, NSError *error) = [
self provideQueryCopyBlock:ReturnTitle];
167 [
self evaluateJavaScript:content completionHandler:queryTitleCopyBlock];
170 - (void) exerciseJavascriptScanForIngredients
172 NSBundle *mb = [NSBundle mainBundle];
174 NSString *jsPathStr = [mb pathForResource:@"scanForIngredients"
176 inDirectory:@"webviewJS"];
177 NSString* content = [NSString stringWithContentsOfFile:jsPathStr
178 encoding:NSUTF8StringEncoding
181 NSLog(
@"Y3WebView.exerciseJavascriptScanForIngredients: content: %@", content);
182 void (^queryIngredientsCopyBlock)(id, NSError *error) = [
self provideQueryCopyBlock:ReturnIngredients];
183 [
self evaluateJavaScript:content completionHandler:queryIngredientsCopyBlock];
185 NSLog(
@"Y3WebView.exerciseJavascriptScanForIngredients: error: %@", error);
188 - (void) exerciseJavascriptScanForInstruction
190 NSBundle *mb = [NSBundle mainBundle];
191 NSString *jsPathStr = [mb pathForResource:@"scanForInstruction"
193 inDirectory:@"webviewJS"];
194 NSString* content = [NSString stringWithContentsOfFile:jsPathStr
195 encoding:NSUTF8StringEncoding
197 void (^queryInstructionCopyBlock)(id, NSError *error) = [
self provideQueryCopyBlock:ReturnInstructions];
198 [
self evaluateJavaScript:content completionHandler:queryInstructionCopyBlock];
201 - (void)showConsole {
203 NSString *jsCode =
@"console.log = (function(oriLogFunc){\ 204 return function(str)\ 206 window.webkit.messageHandlers.log.postMessage(str);\ 207 oriLogFunc.call(console,str);\ 211 [
self.configuration.userContentController addUserScript:[[WKUserScript alloc] initWithSource:jsCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES]];
213 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
214 NSLog(
@"OK> %@",message.body);
220 id<Y3WebViewDelegate> y3Delegate;
226 - (instancetype) initWithFrame:(CGRect)frame andDelegate:(
id<Y3WebViewDelegate>)delegate
228 self = [
super initWithFrame:frame];
230 CGRect totalFrame =
self.frame;
232 CGRect controlsViewFrame = CGRectMake(10, 0, totalFrame.size.width - 20, 68.0);
233 _webControls = [[
U4WebControlsView alloc] initWithFrame:controlsViewFrame andDelegate:self];
234 [_webControls appear:controlsViewFrame];
235 [_webControls setBackgroundColor:[UIColor lightGrayColor]];
237 float controlsHeight = 70.0;
238 CGRect webViewFrame = CGRectMake(totalFrame.size.width * 0.01,
240 totalFrame.size.width * 0.98,
241 totalFrame.size.height - controlsHeight);
243 WKPreferences *prefers = [[WKPreferences alloc] init];
244 [prefers setJavaScriptEnabled:YES];
245 [prefers setJavaScriptCanOpenWindowsAutomatically:YES];
259 WKUserContentController *ucc = [[WKUserContentController alloc] init];
264 WKWebViewConfiguration *configur = [[WKWebViewConfiguration alloc] init];
265 [configur setPreferences:prefers];
266 [configur setUserContentController:ucc];
267 configur.showConsole = YES;
269 _webView = [[
Y3WebViewResponder alloc] initWithFrame:webViewFrame configuration:configur andControllerDelegate:delegate];
272 _webView.navigationDelegate =
delegate;
287 [
self addSubview:_webControls];
288 [
self addSubview:_webView];
292 - (void) updateWebControls
294 [_webControls update:_webView];
297 - (void) captureClicked:(
id)sender
299 NSLog(
@"captureClicked");
302 - (void) handleTapGesture:(UIGestureRecognizer *)gesture
304 NSLog(
@"Y3WebView.handleTapGesture: tap recognized");
306 CGPoint tapLocation = [gesture locationInView:_webView];
308 CGRect targetRect = CGRectMake(tapLocation.x, tapLocation.y, 0.0f, 0.0f);
309 [[UIMenuController sharedMenuController] setTargetRect:targetRect inView:_webView];
311 UIMenuItem *
menuItem = [[UIMenuItem alloc] initWithTitle:@"Capture" action:@selector(captureClicked:)];
312 [[UIMenuController sharedMenuController] setMenuItems:@[menuItem]];
314 [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
317 - (void) becomeResponder: (BOOL)flag {
319 [_webControls focusAvailable:flag];
321 - (void) reformForKeyboard
323 NSLog(
@"Y3WebView.reformForKeyboard: NO-OP");
343 NSLog(
@"Y3WebView.goBack");
348 NSLog(
@"Y3WebView.goForward");
349 [_webView goForward];
351 - (void) stirJavascript
353 NSLog(
@"Y3WebView.stirJavascript");
354 [_webView exerciseJavascriptSendSelection];
356 - (void) loadRequest: (NSURLRequest *)requestURL
358 NSLog(
@"Y3WebView.loadRequest");
359 [_webView loadRequest:requestURL];
363 NSLog(
@"Y3WebView.goEditCopy");
364 [y3Delegate fireSnippetScreen];
367 #pragma called from didFinishNavigation 368 - (void) scanPageForTitle
370 NSLog(
@"Y3WebView.scanPageForTitle");
371 [_webView exerciseJavascriptScanForTitle];
373 - (void) scanPageForIngredients
375 NSLog(
@"Y3WebView.scanPageForIngredients");
376 [_webView exerciseJavascriptScanForIngredients];
378 - (void) scanPageForInstruction
380 NSLog(
@"Y3WebView.scanPageForInstruction");
381 [_webView exerciseJavascriptScanForInstruction];
383 #pragma mark - WKUIDelegate 385 - (void)
webView:(WKWebView *)webViewHere didFinishNavigation:(WKNavigation *)navigation
387 NSLog(
@"Y3WebView.didFinishNavigation");
389 [_webControls update: webViewHere];
393 didCommitNavigation:(WKNavigation *)navigation
395 NSLog(
@"Y3WebView.didCommitNavigation: active URL: %@",
webView.URL.description);
398 - (void)
webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
400 NSLog(
@"Y3WebView.didStartProvisionalNavigation: active URL: %@",
webView.URL.description);
404 decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
405 decisionHandler:(
void (^)(WKNavigationResponsePolicy))decisionHandler{
407 NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
408 NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
409 for (NSHTTPCookie *cookie in cookies) {
410 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
413 decisionHandler(WKNavigationResponsePolicyAllow);
414 NSLog(
@"Y3WebView.decidePolicyForNavigationResponse: ALLOW");
420 decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
421 decisionHandler:(
void (^)(WKNavigationActionPolicy))decisionHandler {
423 NSURLRequest *request = navigationAction.request;
431 NSLog(
@"Y3WebView.decidePolicyForNavigationAction: ~~~~~~~~~~~~~~~~~~~~~~");
433 if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
437 if (decisionHandler) {
438 decisionHandler(WKNavigationActionPolicyAllow);
442 - (void) logNavigationAction: (WKNavigationAction *)navigationAction
444 NSLog(
@" request: %@", navigationAction.request);
445 NSLog(
@" source frame: %@", navigationAction.sourceFrame);
446 NSLog(
@" target frame: %@", navigationAction.targetFrame);
447 if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
448 NSLog(
@" WKNavigationTypeLinkActivated");
450 if (navigationAction.navigationType == WKNavigationTypeFormSubmitted) {
451 NSLog(
@" WKNavigationTypeFormSubmitted");
453 if (navigationAction.navigationType == WKNavigationTypeBackForward) {
454 NSLog(
@" WKNavigationTypeBackForward");
456 if (navigationAction.navigationType == WKNavigationTypeReload) {
457 NSLog(
@" WKNavigationTypeReload");
459 if (navigationAction.navigationType == WKNavigationTypeFormResubmitted) {
460 NSLog(
@" WKNavigationTypeFormResubmitted");
462 if (navigationAction.navigationType == WKNavigationTypeOther) {
463 NSLog(
@" WKNavigationTypeOther");
void exerciseJavascriptSendSelection()
id< Y3WebViewDelegate > y3rDelegate
id< U7ParamEditControlDelegate > delegate
void logNavigationAction:(WKNavigationAction *navigationAction)
void exerciseJavascriptBasic:(NSString *js)