Skip to content

Instantly share code, notes, and snippets.

@mrkev
Last active December 11, 2015 12:38
Show Gist options
  • Save mrkev/4602188 to your computer and use it in GitHub Desktop.
Save mrkev/4602188 to your computer and use it in GitHub Desktop.
Will intercept URLs called from WebView, looking for those starting with "osx" Uses selectors called through a url following the format "osx:selector:arg1:arg2:etc... (Example: "osx:beep") Requires the class to be the PolicyDelegate for the webView. Will work with any web request sent by the WebView, so in theory "<a href="osx:beep">Beep!</a>" c…
/*
Intercepts navigation actions in the html document and translates them to selectors when applicable
*/
- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
{
NSArray *call = [[[request URL] absoluteString] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":"]];
// Find OSX protocol.
if ([call[0] isEqualToString:@"osx"]) {
[listener ignore];
if ([call[1] isEqualToString:@"beep"]) {
NSBeep();
}
} else {[listener use];}
}
/*
Recieves Javascript alerts.
*/
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message {
NSLog(@"%@ received %@ with '%@'", self, NSStringFromSelector(_cmd), message);
}
/*
Helper method for running JavaScripts in a WebView
*/
- (NSString *)runWebScript:(NSString *)script inWebView:(WebView *)mWebView
{return [mWebView stringByEvaluatingJavaScriptFromString:script];}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment