So for future reference, I figured out how to add my local javascript.
This is what I do:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if (frame == [sender mainFrame])
{
NSError *error = nil;
// get the DOMDocument
DOMDocument* domDocument = [frame DOMDocument];
DOMElement* headElement= (DOMElement*)[[domDocument
getElementsByTagName: @"head"] item: 0];
// get the reference.js file
NSString *path = [[NSBundle mainBundle] pathForResource: @"test"
ofType: @"js"];
NSString *js = [NSString stringWithContentsOfFile: path encoding:
NSUTF8StringEncoding error: &error];
if (js && headElement && error = nil)
{
// create a script element
DOMElement* scriptElement = [domDocument createElement: @"script"];
[scriptElement setAttribute: @"type" value: @"text/javascript"];
// add the default css to it
DOMText *jsText = [domDocument createTextNode: js];
[scriptElement appendChild: jsText];
// add it to <head>
[headElement appendChild: scriptElement];
}
}
}
My test.js file looks like this:
function test()
{
document.write("<p>This is a test.</p>");
}
test();
And all my webview shows is "This is a test". It even picks up the css for the
<p> element I declared in my custom css class! I didn't have to use
stringByEvaluatingJavaScriptFromString, although it definitely was useful for
debugging purposes.
Next, changing the content by stripping out some divs.
- Koen.
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]