141Getting Javascript Properties from Objective-C
Access Javascript properties from Objective-C...
NSString *href = [[webView windowScriptObject] evaluateWebScript:@"location.href"];
http://developer.apple.com/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/JavaScriptFromObjC.html
... and forwarding it to Quartz Composer:
[quarrzComposerView setValue:href forInputKey:@"location"];
HTML
<html>
<head>
<script language="JavaScript1.2">
document.captureEvents(Event.MOUSEMOVE); document.onmousemove = getMouseXY;
var tempX = 0; var tempY = 0;
function getMouseXY(e) { tempX = e.pageX; tempY = e.pageY;
if (tempX < 0) tempX = 0;
if (tempY < 0) tempY = 0;
document.Show.MouseX.value = tempX;
document.Show.MouseY.value = tempY;
console.log();
return true;
}
function getKey() { document.Show.keyOutput.value = document.Show.keyInput.value; keyconsole.key(); return true; } </script>
<style>
d {
//display: none;
//background: #f00;
} </style> </head> <body> <form name="Show" id="d"> X <input type="text" name="MouseX" value="0" size="4"><br> Y <input type="text" name="MouseY" value="0" size="4"><br> KeyInput <input type="text" name="keyInput" size="4" onChange="getKey()" ><br> KeyOutput<input type="text" name="keyOutput" size="4"><br> </form> </body> </html>
Obj-C
- (void)webView:(WebView )sender didClearWindowObject:(WebScriptObject )windowScriptObject forFrame:(WebFrame *)frame {
[windowScriptObject setValue:self forKey:@"console"];
[windowScriptObject setValue:self forKey:@"keyconsole"];
}
/* // deprechiated
-
(void)webView:(WebView )webView windowScriptObjectAvailable:(WebScriptObject )windowScriptObject {
[windowScriptObject setValue:self forKey:@"console"]; [windowScriptObject setValue:self forKey:@"keyconsole"]; } */
-
(BOOL)isSelectorExcludedFromWebScript:(SEL)selector { if (selector == @selector(doOutputToLog:) ) { return NO; } else if (selector == @selector(doOutputToKey:) ) { return NO; } return YES;
}
-
(NSString *) webScriptNameForSelector:(SEL)sel { if (sel == @selector(doOutputToLog:)) { return @"log"; } else if (sel == @selector(doOutputToKey:)) { return @"key"; } else { return nil; } }
-
(void) doOutputToLog: (NSString*) theMessage { int mx = [ [[theWebView windowScriptObject] evaluateWebScript:@"document.Show.MouseX.value"] intValue]; int my = [ [[theWebView windowScriptObject] evaluateWebScript:@"document.Show.MouseY.value"] intValue]; NSLog(@"%i, %i", mx, my ); }
-
(void) doOutputToKey: (NSString*) theMessage { key = [[theWebView windowScriptObject] evaluateWebScript:@"document.Show.keyOutput.value"]; NSLog(@"%@", key ); }