diff --git a/app/Jasonette/Jason.h b/app/Jasonette/Jason.h index 2768b16d..27cfcbda 100644 --- a/app/Jasonette/Jason.h +++ b/app/Jasonette/Jason.h @@ -26,7 +26,7 @@ @import MediaPlayer; -@interface Jason : NSObject +@interface Jason : NSObject @property (strong, nonatomic) NSDictionary *parser; @property (strong, nonatomic) NSDictionary *data; diff --git a/app/Jasonette/Jason.m b/app/Jasonette/Jason.m index 2839ffbc..fdde44a6 100644 --- a/app/Jasonette/Jason.m +++ b/app/Jasonette/Jason.m @@ -1857,6 +1857,7 @@ - (void)drawAdvancedBackground:(NSDictionary*)bg{ VC.background = nil; } VC.background = [[UIWebView alloc] initWithFrame: [UIScreen mainScreen].bounds]; + ((UIWebView*)VC.background).delegate = self; // Need to make the background transparent so that it doesn't flash white when first loading VC.background.opaque = NO; @@ -1864,7 +1865,15 @@ - (void)drawAdvancedBackground:(NSDictionary*)bg{ } if(bg[@"text"]){ NSString *html = bg[@"text"]; - [((UIWebView*)VC.background) loadHTMLString:html baseURL:nil]; + if(VC.background.payload && VC.background.payload[@"html"] && [VC.background.payload[@"html"] isEqualToString:html]) { + // same html, no need to reload + } else { + // different html, reload + [((UIWebView*)VC.background) loadHTMLString:html baseURL:nil]; + } + + VC.background.payload = @{@"html": html}; + } // allow autoplay @@ -1890,6 +1899,28 @@ - (void)drawAdvancedBackground:(NSDictionary*)bg{ } }); } +- (void) webViewDidFinishLoad:(UIWebView *)webView +{ + NSString *summon = @"var JASON={call: function(e){var n=document.createElement(\"IFRAME\");n.setAttribute(\"src\",\"jason:\"+JSON.stringify(e)),document.documentElement.appendChild(n),n.parentNode.removeChild(n),n=null}};"; + [webView stringByEvaluatingJavaScriptFromString:summon]; +} +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { + + if ([[[request URL] absoluteString] hasPrefix:@"jason:"]) { + // Extract the selector name from the URL + NSString *json = [[[request URL] absoluteString] substringFromIndex:6]; + json = [json stringByRemovingPercentEncoding]; + + NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; + NSDictionary *action = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + [[Jason client] call: action]; + + return NO; + } + + return YES; +} + - (void)drawBackground:(NSString *)bg{ dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/app/Jasonette/JasonHtmlComponent.m b/app/Jasonette/JasonHtmlComponent.m index 79462e35..c9712c90 100644 --- a/app/Jasonette/JasonHtmlComponent.m +++ b/app/Jasonette/JasonHtmlComponent.m @@ -74,6 +74,26 @@ + (void)actionButtonClicked:(UIButton *)sender{ [[Jason client] call: sender.payload[@"action"]]; } } ++ (void) webViewDidFinishLoad:(UIWebView *)webView +{ + NSString *summon = @"var JASON={call: function(e){var n=document.createElement(\"IFRAME\");n.setAttribute(\"src\",\"jason:\"+JSON.stringify(e)),document.documentElement.appendChild(n),n.parentNode.removeChild(n),n=null}};"; + [webView stringByEvaluatingJavaScriptFromString:summon]; +} ++ (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { + + if ([[[request URL] absoluteString] hasPrefix:@"jason:"]) { + // Extract the selector name from the URL + NSString *json = [[[request URL] absoluteString] substringFromIndex:6]; + json = [json stringByRemovingPercentEncoding]; + NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; + NSDictionary *action = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + [[Jason client] call: action]; + + return NO; + } + + return YES; +} @end