Re: Intercepting JavaScript calls in WebViews
Re: Intercepting JavaScript calls in WebViews
- Subject: Re: Intercepting JavaScript calls in WebViews
- From: Patrick Machielse <email@hidden>
- Date: Mon, 15 Dec 2003 11:28:05 +0100
op 15-12-2003 00:47 schreef Jonathan Wight op email@hidden:
>
On Dec 14, 2003, at 14:55, Patrick Machielse wrote:
>
>
> I'm working on a project using a WebView. The html that is displayed
>
> contains JavaSctipt links like:
>
>
>
> <a href="javascript:function(parameter);">click me</a>
>
>
>
> When the user clicks on a link, I must intercept the JavaScript function call
>
> (which is implemented as an empty function in the html) and do something
>
> intelligent with the function parameter.
>
>
The easiest solution is a pure JavaScript one - but this depends on you
>
controlling the content: replace 'javascript:function(parameter)' with
>
something like 'javascript:'myintelligentfunction(function,
>
parameter)'.
I forgot to tell: I want to do something intelligent in my main program. I
can't just do some tricks in the JavaScript sandbox (I think...)
The situation is as follows: the html displays an array of thumbnails. When
the user clicks on a thumb I must create a custom URL and download a
resource from that URL to a specific location on disc. During the download I
have to give feedback in the gui. After it finishes I have to do some setup.
>
Another way (again if you control the content) then you can replace the
>
'javascript' URL scheme with something else ('x-javascript' for
>
example) then register your own NSURLProtocol (and maybe an NSURLHandle
>
depending on your needs) in which you do do your magic and pass it on
>
to stringByEvaluatingJavaScriptFromString.
I have been thinking in the same direction. Controlling the content is easy
of course:
NSString *html = [NSString stringWithContentsOfURL:htmlURL];
// replace javascript calls...
[[webView mainFrame] loadHTMLString:html baseURL:nil];
There are some subtle problems with string encoding and the fact that
stringWithContentsOfURL: is a blocking call, but I'll ignore those for now.
WebKit seems to be pretty much a black box as far as page content is
concerned. I can't find a 'hook' into JavaScript and delving into KJS would
be overkill for this project.
Patrick
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.