Mailing Lists: Apple Mailing Lists

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

Re: Dynamically reset the DOM?



scriptObject is a field instance of the delegate class. It's an NSObject subclass called LJSScriptObject.

LJSScriptObject * scriptObject;

I've added this in windowScriptObjectAvailable:
[wso setValue:scriptObject forKey:@"scrObject"];

but doing this creates no discernible difference.

I'm using pre-existing JavaScript, which cannot be modified. 'scrObject' is the name and parameter I have to use.

what confuses me is that using loadRequest:pathForResource: will allow me to get a result, "1000", while inserting a new string with loadHTMLString:baseURL: returns me a result(whether or not I have [wso setValue:scriptObject forKey:@"scrObject"]; in the webView:windowScriptObjectAvailable:. In both cases webView:windowScriptObjectAvailable: are called.

what if I generated scratch files in a temporary directory and reload them with loadRequest:pathForResource: ?

Michael

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



On 2-Dec-05, at 2:36 PM, Geoffrey Garen wrote:

What kind of object is scriptObject?

Consider this note from http://developer.apple.com/documentation/ AppleApplications/Reference/Dashboard_Ref/Plugin/ chapter_5_section_1.html


Note: The objects you expose to JavaScript using this technique may conform to the WebScripting interface of Web Kit, which is available in Mac OS X version 10.4. The Web Kit uses the methods of this interface to allow you to control which methods you expose from your Objective-C class. For more information, see Using Objective-C From JavaScript in Cocoa User Experience Documentation.




To test if an invalid argument is your problem, change cocoacalling to take no arguments and issue a simple alert and see if it executes.

Geoff


It seems OK, I added NSLog to both methods:

2005-12-02 13:26:08.000 DTRoot[19607] setJSInWebView: <HTML><HEAD><TITLE> </TITLE><SCRIPT type="text/javascript"><!-- function cocoacalling(scrObject) { scrObject.Score = 1000; document.write('cocoacalling'); return scrObject.Score;}// --></ SCRIPT></HEAD><BODY style="border: 0; margin: 0; padding: 0">loaded from -awakeFromNib</BODY></HTML>
2005-12-02 13:26:08.010 DTRoot[19607] windowScriptObjectAvailable
2005-12-02 13:26:08.016 DTRoot[19607] didFinishLoadForFrame
2005-12-02 13:26:08.016 DTRoot[19607] didFinishLoadForFrame: <HTML><HEAD><TITLE> </TITLE><SCRIPT type="text/javascript"><!-- function cocoacalling(scrObject) { scrObject.Score = 1000; document.write('cocoacalling'); return scrObject.Score;}// --></ SCRIPT></HEAD><BODY style="border: 0; margin: 0; padding: 0">loaded from -awakeFromNib</BODY></HTML>
2005-12-02 13:26:08.016 DataTypeRoot[19607] (null) result


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



On 2-Dec-05, at 11:50 AM, Geoffrey Garen wrote:

It would be interesting to NSLog the result of the outerHTML call to see exactly what HTML you end up reloading. I suspect the result does not include the script you've constructed, which may indicate a bug in WebKit.

Geoff

On Dec 2, 2005, at 11:21 AM, Michael Hanna wrote:

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/ggaren% 40apple.com


This email sent to email@hidden




_______________________________________________ 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
References: 
 >Dynamically reset the DOM? (From: Michael Hanna <email@hidden>)
 >Re: Dynamically reset the DOM? (From: Geoffrey Garen <email@hidden>)
 >Re: Dynamically reset the DOM? (From: Michael Hanna <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.