D'oh, I just discoverd that the website I'd like to display has a
mobile version, which is already basic. I'll just display that instead
in my webView.  This only needs some minor tweaks to remove one or two
divs, and saves me a lot of headaches.

- Koen.




On Sun, Aug 19, 2012 at 10:00 AM, Koen van der Drift
<koenvanderdr...@gmail.com> wrote:
> 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 (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to