Re: stripped down webview
Re: stripped down webview
- Subject: Re: stripped down webview
- From: Koen van der Drift <email@hidden>
- Date: Mon, 20 Aug 2012 09:20:45 -0400
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
<email@hidden> 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 (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