• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: stripped down webview
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: stripped down webview


  • Subject: Re: stripped down webview
  • From: Koen van der Drift <email@hidden>
  • Date: Sun, 19 Aug 2012 10:00:13 -0400

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@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: stripped down webview
      • From: Koen van der Drift <email@hidden>
References: 
 >stripped down webview (From: Koen van der Drift <email@hidden>)
 >Re: stripped down webview (From: Kyle Sluder <email@hidden>)
 >Re: stripped down webview (From: Koen van der Drift <email@hidden>)
 >Re: stripped down webview (From: Matt Patenaude <email@hidden>)
 >Re: stripped down webview (From: Koen van der Drift <email@hidden>)
 >Re: stripped down webview (From: Matt Patenaude <email@hidden>)
 >Re: stripped down webview (From: Koen van der Drift <email@hidden>)
 >Re: stripped down webview (From: Koen van der Drift <email@hidden>)
 >Re: stripped down webview (From: Evadne Wu <email@hidden>)

  • Prev by Date: Re: stripped down webview
  • Next by Date: Re: Apple Aperture-like look & feel
  • Previous by thread: Re: stripped down webview
  • Next by thread: Re: stripped down webview
  • Index(es):
    • Date
    • Thread