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: Tue, 06 Jan 2004 00:01:13 +0100
op 05-01-2004 16:40 schreef KLW op email@hidden:
>
Patrick Machielse wrote:
>
>
> This is quite 'easy' and does what it is supposed to, but won't work if the
>
> user clicks on a 'javascript' link before loading of the page is complete and
>
> the controler hasn't replaced the js calls yet.
>
>
>
> I also tried replacing the js code while the html is retreived, but then you
>
> have to register a URLProtocol for the html scheme. The problem here is that
>
> you want to build on "NSHTMLProtocol", but you can't since it is private.
>
> There may be a way around this, but I didn't investigate any further.
>
>
I'm probably missing something obvious, but why can't you load the
>
response to the get request into some kind of buffer or storage
>
container before throwing it into an NSTextView? Pop all the HTML into a
>
buffer, do whatever operations you need, and THEN display it. Also would
>
save the extra processing involved in displaying the content twice.
>
Forgive me if I'm speaking out of my gourd on this one.
You are forgiven. I'm not an expert on this either, and I would welcome
comment from someone _really_ knowledgable. The code I showed was just from
a few hours of experimenting. I haven't found any good concpetual
documentation on mechanics of the URL loading system. So 'what happens' and
in which sequence. I believe it works like this:
* when you click on a link the WebView (not NSTextView!) packages the URL in
a NSURLRequest and hands it of to a (subclass of) NSURLProtocol (which is a
class, not a protocol).
* well known URL schemes like http:// and ftp:// are by default handled by
private subclasses of NSURLProtocol, which you cannot subclass.
* you can register your own subclasses of NSURLProtocol to handle any
scheme. These will be tried before resorting to the build in ones.
To change the html before it gets displayed, I wanted to append the build-in
handler for http://, but it isn't documented / private. That's why I changed
the html after it was loaded. Performance is good in my case (not even a
screen flicker) since all resources are cached (I suppose that is the reason
anyway).
Thinking about it, if you want to change the html before it gets displayed,
you could conceivably register your own subclass of NSURLProtocol for the
http:// scheme, then, once it's - (void)startLoading method gets called by
the WebView _unregister_ it and let it initiate a NSURLConnection of it's
own, which will then be handled by the default handler for http://. Get the
html from it, process it, and finally pass it on to the eagerly waiting
WebView.
However, if you have more than 1 html request coming in at a time, you will
get in trouble.
I also thought about using curl in my http:// handler to retreive the raw
html, but I'm not sure how it compares feature wise with NSURLConnection /
NSURLDownload.
I hope I understood the documentation right and the text above makes some
sort of sense to anyone.
Are there any real pros in the room :-)
Groeten,
Patrick
PS. If anyone knows the meaning and purpose of 'canonicalRequestForRequest'
in NSURLProtocol, please let me know! According to the docs 'It is up to
each concrete protocol implementation to define what 3canonical2 means'.
???
_______________________________________________
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.