Today's Menu  Portugal
journal and plan nutrition
Instance Methods | List of all members
Y3WebViewResponder Class Reference

#import <Y3WebView.h>

Inheritance diagram for Y3WebViewResponder:
Inheritance graph
[legend]
Collaboration diagram for Y3WebViewResponder:
Collaboration graph
[legend]

Instance Methods

(instancetype) - initWithFrame:configuration:andControllerDelegate:
 
(void) - copy:
 
(BOOL) - becomeFirstResponder [implementation]
 
(BOOL) - resignFirstResponder [implementation]
 
(id NSError *error) - provideQueryCopyBlock: [implementation]
 
(void) - exerciseJavascriptBasic: [implementation]
 
(void) - exerciseJavascriptSendSelection [implementation]
 
(void) - exerciseJavascriptScanForRecipe [implementation]
 
(void) - exerciseJavascriptScanForTitle [implementation]
 
(void) - exerciseJavascriptScanForIngredients [implementation]
 
(void) - exerciseJavascriptScanForInstruction [implementation]
 
(void) - showConsole [implementation]
 
(void) - userContentController:didReceiveScriptMessage: [implementation]
 

Detailed Description

This view simply sub-classes the WKWebView, to catch Copy/Paste and firstResponder notices.

Definition at line 18 of file Y3WebView.h.

Method Documentation

◆ becomeFirstResponder()

- (BOOL) becomeFirstResponder
implementation

Definition at line 62 of file Y3WebView.m.

63 {
64  NSLog(@"Y3WebView.becomeFirstResponder");
65  [y3rDelegate becomeResponder:YES];
66  BOOL ret = [super becomeFirstResponder];
67  return ret;
68 }

◆ copy:()

- (void) copy: (id)  sender

Definition at line 76 of file Y3WebView.m.

76  :(id)sender {
77  NSLog(@"Y3WebView.copyClicked: start:sender class: %@", NSStringFromClass([sender class]));
78 
79  [self exerciseJavascriptSendSelection];
80 }

◆ exerciseJavascriptBasic:()

- (void) exerciseJavascriptBasic: (NSString *)  js
implementation

Definition at line 119 of file Y3WebView.m.

119  : (NSString *)js
120 {
121  void (^queryCopyBlock)(id, NSError *error) = [self provideQueryCopyBlock:ReturnBasic];
122  [self evaluateJavaScript:js completionHandler:queryCopyBlock];
123 }

◆ exerciseJavascriptScanForIngredients()

- (void) exerciseJavascriptScanForIngredients
implementation

Definition at line 170 of file Y3WebView.m.

171 {
172  NSBundle *mb = [NSBundle mainBundle];
173  NSError *error;
174  NSString *jsPathStr = [mb pathForResource:@"scanForIngredients"
175  ofType:@"js"
176  inDirectory:@"webviewJS"];
177  NSString* content = [NSString stringWithContentsOfFile:jsPathStr
178  encoding:NSUTF8StringEncoding
179  error:&error];
180  if (nil == error) {
181  NSLog(@"Y3WebView.exerciseJavascriptScanForIngredients: content: %@", content);
182  void (^queryIngredientsCopyBlock)(id, NSError *error) = [self provideQueryCopyBlock:ReturnIngredients];
183  [self evaluateJavaScript:content completionHandler:queryIngredientsCopyBlock];
184  } else {
185  NSLog(@"Y3WebView.exerciseJavascriptScanForIngredients: error: %@", error);
186  }
187 }

◆ exerciseJavascriptScanForInstruction()

- (void) exerciseJavascriptScanForInstruction
implementation

Definition at line 188 of file Y3WebView.m.

189 {
190  NSBundle *mb = [NSBundle mainBundle];
191  NSString *jsPathStr = [mb pathForResource:@"scanForInstruction"
192  ofType:@"js"
193  inDirectory:@"webviewJS"];
194  NSString* content = [NSString stringWithContentsOfFile:jsPathStr
195  encoding:NSUTF8StringEncoding
196  error:NULL];
197  void (^queryInstructionCopyBlock)(id, NSError *error) = [self provideQueryCopyBlock:ReturnInstructions];
198  [self evaluateJavaScript:content completionHandler:queryInstructionCopyBlock];
199 }

◆ exerciseJavascriptScanForRecipe()

- (void) exerciseJavascriptScanForRecipe
implementation

Definition at line 135 of file Y3WebView.m.

136 {
137  NSBundle *mb = [NSBundle mainBundle];
138  NSString *jsPathStr = [mb pathForResource:@"scanForRecipe"
139  ofType:@"js"
140  inDirectory:@"webviewJS"];
141  NSString* content = [NSString stringWithContentsOfFile:jsPathStr
142  encoding:NSUTF8StringEncoding
143  error:NULL];
144  NSLog(@"Y3WebView.exerciseJavascriptScanForRecipe: content: %@", content);
145  void (^queryTitleCopyBlock)(id, NSError *error) = [self provideQueryCopyBlock:ReturnTitle];
146  [self evaluateJavaScript:content completionHandler:queryTitleCopyBlock];
147 
148 /*
149  void (^queryIngredientsCopyBlock)(id, NSError *error) = [self provideQueryCopyBlock:ReturnIngredients];
150  [self evaluateJavaScript:content completionHandler:queryIngredientsCopyBlock];
151 
152  void (^queryInstructionCopyBlock)(id, NSError *error) = [self provideQueryCopyBlock:ReturnInstructions];
153  [self evaluateJavaScript:content completionHandler:queryInstructionCopyBlock];
154  */
155 }

◆ exerciseJavascriptScanForTitle()

- (void) exerciseJavascriptScanForTitle
implementation

Definition at line 156 of file Y3WebView.m.

157 {
158  NSBundle *mb = [NSBundle mainBundle];
159  NSString *jsPathStr = [mb pathForResource:@"scanForTitle"
160  ofType:@"js"
161  inDirectory:@"webviewJS"];
162  NSString* content = [NSString stringWithContentsOfFile:jsPathStr
163  encoding:NSUTF8StringEncoding
164  error:NULL];
165  NSLog(@"Y3WebView.exerciseJavascriptScanForTitle: content: %@", content);
166  void (^queryTitleCopyBlock)(id, NSError *error) = [self provideQueryCopyBlock:ReturnTitle];
167  [self evaluateJavaScript:content completionHandler:queryTitleCopyBlock];
168 }

◆ exerciseJavascriptSendSelection()

- (void) exerciseJavascriptSendSelection
implementation

Definition at line 124 of file Y3WebView.m.

125 {
126  NSBundle *mb = [NSBundle mainBundle];
127  NSString *jsPathStr = [mb pathForResource:@"detectSelection"
128  ofType:@"js"];
129  NSString* content = [NSString stringWithContentsOfFile:jsPathStr
130  encoding:NSUTF8StringEncoding
131  error:NULL];
132 
133  [self exerciseJavascriptBasic:content];
134 }

◆ initWithFrame:configuration:andControllerDelegate:()

- (instancetype) initWithFrame: (CGRect)  frame
configuration: (WKWebViewConfiguration *)  configuration
andControllerDelegate: (id<Y3WebViewDelegate>)  delegate 

Definition at line 41 of file Y3WebView.m.

41  :(CGRect)frame
42  configuration:(WKWebViewConfiguration *)configuration
43  andControllerDelegate:(id<Y3WebViewDelegate>)delegate
44 {
45  self = [super initWithFrame:frame configuration:configuration];
46  if (self) {
48 
49  if (configuration.showConsole) {
50  WKUserContentController *userCC = configuration.userContentController;
51  [userCC addScriptMessageHandler:self name:@"log"];
52 
53  [userCC addScriptMessageHandler:self name:@"logging"];
54  [userCC addScriptMessageHandler:self name:@"newSelectionDetected"] ;
55 
56  [self showConsole];
57  }
58  }
59  return self;
60 }
id< Y3WebViewDelegate > y3rDelegate
Definition: Y3WebView.m:37
id< U7ParamEditControlDelegate > delegate

◆ provideQueryCopyBlock:()

- (id NSError *error) provideQueryCopyBlock: (MenuItemSegment)  itemSegment
implementation

Definition at line 82 of file Y3WebView.m.

82  :(MenuItemSegment) itemSegment
83 {
84  void (^queryCopyBlock)(id, NSError *error) = ^void(id jsReturn, NSError *error)
85  {
86  if (nil == error) {
87 
88  // [self->y3rDelegate jsReturn:jsReturn];
89  switch (itemSegment) {
90  case ReturnBasic:
91  [self->y3rDelegate jsReturnIndication:jsReturn];
92  NSLog(@"Y3WebView.provideQueryCopyBlock: ReturnBasic: %@", jsReturn);
93  break;
94  case ReturnTitle:
95  [self->y3rDelegate jsTitleReturn:jsReturn];
96  NSLog(@"Y3WebView.provideQueryCopyBlock: ReturnTitle: %@", jsReturn);
97  break;
98  case ReturnIngredients:
99  [self->y3rDelegate jsIngredientsReturn:jsReturn];
100  NSLog(@"Y3WebView.provideQueryCopyBlock: ReturnIngredients: %@", jsReturn);
101  break;
102  case ReturnInstructions:
103  [self->y3rDelegate jsInstructionReturn:jsReturn];
104  NSLog(@"Y3WebView.provideQueryCopyBlock: ReturnInstructions: %@", jsReturn);
105  break;
106  default:
107  NSLog(@"Y3WebView.provideQueryCopyBlock: default?: ret: %@", jsReturn);
108  break;
109  }
110  [self->y3rDelegate setControlbarColor:[UIColor greenColor]];
111  } else {
112  NSLog(@"Y3WebView.provideQueryCopyBlock: completion: ERROR: %@", error);
113  [self->y3rDelegate setControlbarColor:[UIColor redColor]];
114  }
115  };
116  return queryCopyBlock;
117 }

◆ resignFirstResponder()

- (BOOL) resignFirstResponder
implementation

Definition at line 69 of file Y3WebView.m.

70 {
71  NSLog(@"Y3WebView.resignFirstResponder");
72  [y3rDelegate becomeResponder:NO];
73  BOOL ret = [super resignFirstResponder];
74  return ret;
75 }

◆ showConsole()

- (void) showConsole
implementation

Definition at line 201 of file Y3WebView.m.

201  {
202  //rewrite the method of console.log
203  NSString *jsCode = @"console.log = (function(oriLogFunc){\
204  return function(str)\
205  {\
206  window.webkit.messageHandlers.log.postMessage(str);\
207  oriLogFunc.call(console,str);\
208  }\
209  })(console.log);";
210  //injected the method when H5 starts to create the DOM tree
211  [self.configuration.userContentController addUserScript:[[WKUserScript alloc] initWithSource:jsCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES]];
212 }

◆ userContentController:didReceiveScriptMessage:()

- (void) userContentController: (WKUserContentController *)  userContentController
didReceiveScriptMessage: (WKScriptMessage *)  message 
implementation

Definition at line 213 of file Y3WebView.m.

213  :(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
214  NSLog(@"OK> %@",message.body);
215 }

The documentation for this class was generated from the following files: