Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Dynamically reset the DOM?



Hi I'm trying to inject JS into the DOM dynamically, but I'm having trouble getting a result from the injected JS which *would* give me a result when I loaded the standard way from a file.

The html code in the webview is like this at first:

-awakeFromNib:
[[webView mainFrame] loadHTMLString:@"<html><head><title> </title></ head><body style='border: 0; margin: 0; padding: 0'>loaded from - awakeFromNib</body></html>" baseURL:nil];


then, after didFinishLoadForFrame: is called, I try to dynamically reset the DOM:

-(IBAction)setJSInWebView:(id)sender
{
NSString * scriptPreamble = @"<!-- function cocoacalling(scrObject) { scrObject.Score = 1000; document.write('cocoacalling'); return scrObject.Score;}";
NSString * scriptPostamble = @"// -->";
NSString * scriptString = @"";

DOMDocument *doc = [[webView mainFrame] DOMDocument];
DOMNodeList *list = [doc getElementsByTagName:@"HEAD"];
assert([list length] > 0);
DOMNode *body = [list item:0];
DOMElement *el = [doc createElement:@"SCRIPT"];
[el setAttribute:@"type" :@"text/javascript"];
[body appendChild:el];

scriptString = [NSString stringWithFormat:@"%@%@%@", scriptString, scriptPreamble, scriptPostamble];

[el appendChild:[doc createTextNode:scriptString]];


[[webView mainFrame] loadHTMLString:[(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerHTML] baseURL:nil];
}


but once didFinishLoadForFrame: is called the result is null:

- (void)webView:(WebView *)webView didFinishLoadForFrame:(WebFrame *) frame
{
NSLog(@"didFinishLoadForFrame");
WebScriptObject * wso = [webView windowScriptObject];
id result;
NSArray * args = [NSArray arrayWithObjects:scriptObject, nil];
result = [wso callWebScriptMethod:@"cocoacalling" withArguments:args];
NSLog(@"%@ result", result);
}


I know my technique is in error in some way. Is there a way to dynamically inject JS like I'm trying to?

--
Michael Hanna
email@hidden
iChat: michaelkhanna(AIM)



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webkitsdk-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webkitsdk-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.